UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

111 lines (110 loc) 5.91 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../util_modules/logger/index"); const index_js_1 = require("../fn-utils/index.js"); const index_js_2 = __importDefault(require("../error/index.js")); const index_js_3 = require("../util_modules/constants/index.js"); const project_js_1 = require("../util_modules/project.js"); const index_js_4 = require("../util_modules/fs/index.js"); const utils_1 = require("../serve/server/lib/master/utils"); const ansi_colors_1 = require("ansi-colors"); const compile_js_1 = require("../fn-utils/lib/java/compile.js"); const index_js_5 = require("../shell/prepare/languages/index.js"); const caller_js_1 = require("./caller.js"); const runtime_store_1 = __importDefault(require("../runtime-store")); const stream_1 = require("stream"); const throbber_1 = __importDefault(require("../throbber")); const util_1 = require("util"); const pip_install_1 = require("../init/dependencies/python/pip-install"); exports.default = (target, input) => __awaiter(void 0, void 0, void 0, function* () { const buildFolder = (0, project_js_1.resolveProjectPath)(index_js_3.FOLDERNAME.build); try { (0, index_1.info)(); (0, index_1.info)((0, ansi_colors_1.bold)('=> Starting function execution')); (0, index_1.info)(` name: ${(0, ansi_colors_1.green)(target.name)} | type: ${(0, ansi_colors_1.magenta)(target.type ? index_js_3.REMOTE_REF.functions.type[target.type] : '')} | stack: ${(0, ansi_colors_1.cyan)(target.stack || '')}`); (0, index_1.info)(); if (yield index_js_4.ASYNC.isPathExists(buildFolder)) { try { (0, index_1.debug)('Build folder already exists, staring cleanup'); yield index_js_4.ASYNC.deleteDir(buildFolder); } catch (err) { throw new index_js_2.default('Error running pre cleanup', { original: err, exit: 1, skipHelp: true }); } } utils_1.serverEvent.emit('start'); yield index_js_1.fnUtils.common.executeHook({ prefix: 'pre', command: 'serve' }); throbber_1.default.getInstance().add('function_serve_' + target.name, { text: `preparing function [${target.name}]` }); const [_nodeFns, javaFns, _pythonFns] = yield (0, index_js_5.prepareFunctions)([target]); javaFns && (0, compile_js_1.printCompilationLog)(javaFns); const pyStacks = []; _pythonFns === null || _pythonFns === void 0 ? void 0 : _pythonFns.forEach((target) => { if (!pyStacks.includes(target.stack + '') && target.valid) { pyStacks.push(target.stack + ''); } }); yield Promise.all(pyStacks.map((stack) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b; yield (0, pip_install_1.ensurePyRuntime)(index_js_3.ENVPATH.runtimes.data, stack, (_b = (_a = _pythonFns === null || _pythonFns === void 0 ? void 0 : _pythonFns.at(0)) === null || _a === void 0 ? void 0 : _a.additionalInfo) === null || _b === void 0 ? void 0 : _b.binPath); }))); const debugPort = runtime_store_1.default.get(`context.port.debug.${target.type}.${target.name}`, -1); const preparedFn = [_nodeFns, javaFns, _pythonFns] .flat() .find((fn) => (fn === null || fn === void 0 ? void 0 : fn.name) === target.name); if ((preparedFn === null || preparedFn === void 0 ? void 0 : preparedFn.valid) === false) { throw new index_js_2.default(`Error preparing function ${(0, ansi_colors_1.bold)(target.name)} for execution because ${target.failure_reason}`, { exit: 1, skipHelp: true }); } const caller = new caller_js_1.Caller({ type: 'functions', target, httpPort: -1, debugPort, restarting: false, isAlive: null }, new stream_1.EventEmitter()); utils_1.serverEvent.once('stop', () => caller.kill()); yield caller.call(input); utils_1.serverEvent.emit('stop'); yield index_js_4.ASYNC.deleteDir(buildFolder).catch(); yield index_js_1.fnUtils.common.executeHook({ prefix: 'post', command: 'serve' }); (0, index_1.success)('execute complete'); (0, index_1.info)(); } catch (e) { utils_1.serverEvent.emit('error', e); index_js_4.ASYNC.deleteDir(buildFolder).catch(); if (typeof e === 'number') { (0, index_1.debug)('Execute function program termination by code: ', 130); } else { (0, index_1.info)(); (0, index_1.info)(ansi_colors_1.bold.red('Function execution failed')); (0, index_1.info)(ansi_colors_1.italic.red('Reason: ') + (e instanceof Error ? e.message : (0, util_1.inspect)(e))); } utils_1.serverEvent.emit('stop'); } finally { utils_1.serverEvent.emit('close'); } });