UNPKG

@metacall/faas

Version:

Reimplementation of MetaCall FaaS platform written in TypeScript.

140 lines (135 loc) 5.68 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.repositoryClone = exports.repositoryFileList = exports.repositoryBranchList = void 0; const fs_1 = require("fs"); const path_1 = __importStar(require("path")); const app_1 = require("../app"); const appError_1 = __importDefault(require("../utils/appError")); const config_1 = require("../utils/config"); const exec_1 = require("../utils/exec"); const install_1 = require("../utils/install"); const catch_1 = require("./catch"); const repositoryName = (url) => url .replace(/\.git$/, '') .split('/') .slice(-2) .join('-') .toLowerCase(); const repositoryDelete = async (path, url) => { const folder = repositoryName(url); const repoFilePath = path_1.join(path, folder); await fs_1.promises.rm(repoFilePath, { recursive: true, force: true }); }; /* const handleRunners = async (repoPath: string): Promise<string[]> => { const runners: string[] = []; const files = await fs.readdir(repoPath); for (const file of files) { const fullPath = path.join(repoPath, file); const stat = await fs.stat(fullPath); if (file === 'requirements.txt') runners.push('python'); if (file === 'package.json') runners.push('nodejs'); if (stat.isDirectory()) { const subRunners = await handleRunners(fullPath); runners.push(...subRunners); } } return runners; }; */ exports.repositoryBranchList = catch_1.catchAsync(async (req, res, next) => { try { const { url } = req.body; // list remote branches for the repository const { stdout } = await exec_1.exec(`git ls-remote --heads ${url}`); // Parse branches from the command output const branches = stdout .trim() .split('\n') .map(line => line.split('refs/heads/')[1]) .filter(Boolean); return res.status(200).json({ branches }); } catch (err) { const message = err instanceof Error ? err.message : String(err); next(new appError_1.default(`Error fetching branch list: ${message}`, 500)); } }); exports.repositoryFileList = catch_1.catchAsync(async (req, res, next) => { const { url, branch } = req.body; const repoDir = repositoryName(url); const repoPath = path_1.default.join(config_1.appsDirectory, repoDir); try { // Delete existing repo folder if it exists await repositoryDelete(config_1.appsDirectory, url); // Clone the repository with the requested branch so ls-tree can resolve it await exec_1.exec(`git clone --depth=1 --no-checkout --branch ${branch} ${url} ${repoPath}`); // List files in the specified branch const { stdout } = await exec_1.exec(`git ls-tree -r ${branch} --name-only`, { cwd: repoPath }); const files = stdout.trim().split('\n').filter(Boolean); // Clean up the cloned repository await fs_1.promises.rm(repoPath, { recursive: true, force: true }); return res.status(200).json({ files }); } catch (err) { const message = err instanceof Error ? err.message : String(err); return next(new appError_1.default(`Error fetching file list from repository: ${message}`, 500)); } }); exports.repositoryClone = catch_1.catchAsync(async (req, res, next) => { const { branch, url } = req.body; const resource = { id: '', path: '', jsons: [], runners: [] }; try { await repositoryDelete(config_1.appsDirectory, url); } catch (err) { const message = err instanceof Error ? err.message : String(err); return next(new appError_1.default(`Error deleting repository directory: ${message}`, 500)); } try { // Clone the repository into the specified directory await exec_1.exec(`git clone --single-branch --depth=1 --branch ${branch} ${url} ${path_1.join(config_1.appsDirectory, repositoryName(url))}`); } catch (err) { const message = err instanceof Error ? err.message : String(err); return next(new appError_1.default(`Error cloning repository: ${message}`, 500)); } const id = repositoryName(req.body.url); resource.id = id; resource.path = path_1.join(config_1.appsDirectory, id); resource.runners = await install_1.findRunners(resource.path); // Create a new Application instance and assign the resource to it const application = new app_1.Application(); application.resource = Promise.resolve(resource); app_1.Applications[id] = application; return res.status(201).send({ id }); });