UNPKG

@metacall/faas

Version:

Reimplementation of MetaCall FaaS platform written in TypeScript.

96 lines (95 loc) 3.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.installDependencies = exports.findRunners = void 0; const fs_1 = require("fs"); const path_1 = __importDefault(require("path")); const exec_1 = require("./exec"); // TODO: Unify this with metacall/protocol const runnerList = ['nodejs', 'python', 'ruby', 'csharp']; // TODO: Unify this with metacall/protocol const targetFiles = { nodejs: 'package.json', python: 'requirements.txt', ruby: 'Gemfile', csharp: 'project.json' }; const installCommand = { python: 'metacall pip3 install -r requirements.txt', nodejs: 'metacall npm i', ruby: 'metacall bundle install', csharp: 'metacall dotnet restore && metacall dotnet release' }; // TODO: Unify this with metacall/protocol const isRunner = (runner) => { return runnerList.includes(runner); }; // TODO: Unify this with metacall/protocol const findFilesRecursively = async (dirPattern, filePattern, depthLimit = Infinity) => { const stack = [ { dir: dirPattern, depth: 0 } ]; const files = []; const dirRegex = new RegExp(dirPattern); const fileRegex = new RegExp(filePattern); while (stack.length > 0) { const { dir, depth } = stack.pop() || { dir: '', depth: depthLimit }; try { if (!dirRegex.test(dir)) { continue; } if (depth > depthLimit) { continue; } const items = await fs_1.promises.readdir(dir); for (const item of items) { const fullPath = path_1.default.join(dir, item); const stat = await fs_1.promises.stat(fullPath); if (stat.isDirectory()) { stack.push({ dir: fullPath, depth: depth + 1 }); } else if (stat.isFile() && fileRegex.test(item)) { files.push(fullPath); } } } catch (err) { console.error(`Error reading directory ${dir}:`, err); } } return files; }; // TODO: Unify this with metacall/protocol const findDependencies = async (dir, runners) => { const dependencies = {}; for (const runner of runners) { if (isRunner(runner)) { dependencies[runner] = await findFilesRecursively(dir, targetFiles[runner]); } } return dependencies; }; // TODO: Unify this with metacall/protocol const findRunners = async (dir) => { const dependencies = await findDependencies(dir, runnerList); return Object.keys(dependencies); }; exports.findRunners = findRunners; const installDependencies = async (resource) => { const runnerDeps = await findDependencies(resource.path, resource.runners); for (const [runner, deps] of Object.entries(runnerDeps)) { const command = installCommand[runner]; for (const dependency of deps) { const cwd = path_1.default.dirname(dependency); try { await exec_1.exec(command, { cwd }); } catch (err) { console.error(`Failed to install dependencies for runner ${runner}:`, err); } } } }; exports.installDependencies = installDependencies;