UNPKG

@metacall/faas

Version:

Reimplementation of MetaCall FaaS platform written in TypeScript.

129 lines (128 loc) 5.24 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.fetchFileList = exports.fetchBranchList = exports.fetchFilesFromRepo = 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 catch_1 = require("./catch"); const dirName = (gitUrl) => String(gitUrl.split('/').pop()).replace('.git', ''); const deleteRepoFolderIfExist = async (path, url) => { const folder = dirName(url); const repoFilePath = path_1.join(path, folder); await fs_1.promises.rm(repoFilePath, { recursive: true, force: true }); }; const handleRunners = async (repoPath) => { const runners = []; const files = await fs_1.promises.readdir(repoPath); for (const file of files) { const fullPath = path_1.default.join(repoPath, file); const stat = await fs_1.promises.stat(fullPath); if (file === 'requirements.txt') runners.push('python'); if (file === 'package.json') runners.push('nodejs'); if (stat.isDirectory()) { await handleRunners(fullPath); } } return runners; }; exports.fetchFilesFromRepo = catch_1.catchAsync(async (req, res, next) => { const { branch, url } = req.body; const resource = { id: '', path: '', jsons: [], runners: [] }; try { await deleteRepoFolderIfExist(config_1.appsDirectory, url); } catch (err) { return next(new appError_1.default('error occurred in deleting repository directory', 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, dirName(url))}`); } catch (err) { return next(new appError_1.default('Error occurred while cloning the repository', 500)); } const id = dirName(req.body.url); resource.id = id; resource.path = path_1.join(config_1.appsDirectory, id); const detectedRunners = await handleRunners(resource.path); if (detectedRunners.length > 0) { resource.runners = detectedRunners; } else { console.warn('No runners detected'); } // 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 }); }); exports.fetchBranchList = 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) { next(new appError_1.default('Error fetching branch list', 500)); } }); exports.fetchFileList = catch_1.catchAsync(async (req, res, next) => { const { url, branch } = req.body; const repoDir = dirName(url); const repoPath = `${config_1.appsDirectory}/${repoDir}`; try { // Delete existing repo folder if it exists await deleteRepoFolderIfExist(config_1.appsDirectory, url); // Clone the repository await exec_1.exec(`git clone ${url} ${repoPath} --depth=1 --no-checkout`); // List files in the specified branch const { stdout } = await exec_1.exec(`cd ${repoPath} && git ls-tree -r ${branch} --name-only`); 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) { return next(new appError_1.default('Error fetching file list from repository', 500)); } });