UNPKG

@metacall/faas

Version:

Reimplementation of MetaCall FaaS platform written in TypeScript.

77 lines (76 loc) 2.96 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.autoDeployApps = void 0; const fs = __importStar(require("fs/promises")); const path = __importStar(require("path")); const app_1 = require("../app"); const deploy_1 = require("./deploy"); const isErrnoException = (err) => err instanceof Error && 'code' in err; const readEnvFile = async (envFilePath) => { try { const envFileContent = await fs.readFile(envFilePath, 'utf-8'); return envFileContent.split('\n').reduce((acc, line) => { const [name, value] = line.split('='); if (name?.trim()) { acc[name.trim()] = (value ?? '').trim(); } return acc; }, {}); } catch (err) { if (isErrnoException(err) && err.code === 'ENOENT') { return {}; } throw err; } }; const autoDeployApps = async (appsDir) => { const directories = (await fs.readdir(appsDir, { withFileTypes: true })).reduce((dirs, current) => current.isDirectory() ? [...dirs, current] : dirs, []); const resources = directories.map(dir => ({ id: dir.name, path: path.join(appsDir, dir.name), jsons: [], runners: [] })); let succeeded = 0; for (const resource of resources) { app_1.Applications[resource.id] = new app_1.Application(); app_1.Applications[resource.id].resource = Promise.resolve(resource); const envFilePath = path.join(resource.path, `.env`); const env = await readEnvFile(envFilePath); try { await deploy_1.deployProcess(resource, env); succeeded++; } catch (err) { delete app_1.Applications[resource.id]; // eslint-disable-next-line no-console console.warn(`Failed to load app "${resource.id}":`, err instanceof Error ? err.message : String(err)); } } if (succeeded > 0) { console.log('Previously deployed applications deployed successfully'.green); } }; exports.autoDeployApps = autoDeployApps;