@metacall/faas
Version:
Reimplementation of MetaCall FaaS platform written in TypeScript.
65 lines (64 loc) • 2.37 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.installDependencies = void 0;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const exec_1 = require("./exec");
const targetFiles = {
nodejs: 'package.json',
python: 'requirements.txt',
ruby: 'Gemfile',
csharp: 'project.json'
};
const isRunner = (runner) => {
return ['nodejs', 'python', 'ruby', 'csharp'].includes(runner);
};
const findDependencyFile = async (dir, runner) => {
const files = await fs_1.promises.readdir(dir);
for (const file of files) {
const fullPath = path_1.default.join(dir, file);
const stat = await fs_1.promises.stat(fullPath);
if (stat.isDirectory()) {
const result = await findDependencyFile(fullPath, runner);
if (result)
return result;
}
else if (file === targetFiles[runner]) {
return dir;
}
}
return null;
};
const createInstallDependenciesScript = async (runner, basePath) => {
const dependencyFilePath = await findDependencyFile(basePath, runner);
if (!dependencyFilePath) {
throw new Error(`No ${runner} dependencies file found`);
}
const installDependenciesScript = {
python: `cd ${dependencyFilePath} && metacall pip3 install -r requirements.txt`,
nodejs: `cd ${dependencyFilePath} && metacall npm i`,
ruby: `cd ${dependencyFilePath} && metacall bundle install`,
csharp: `cd ${dependencyFilePath} && metacall dotnet restore && metacall dotnet release`
};
return installDependenciesScript[runner];
};
// Todo: Async Error Handling
const installDependencies = async (resource) => {
if (!resource.runners)
return;
for (const runner of resource.runners) {
if (runner && isRunner(runner)) {
try {
const script = await createInstallDependenciesScript(runner, resource.path);
await exec_1.exec(script);
}
catch (err) {
console.error(`Failed to install dependencies for runner ${runner}:`, err);
}
}
}
};
exports.installDependencies = installDependencies;