UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

211 lines (210 loc) 10.5 kB
'use strict'; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); 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 path_1 = require("path"); const util_1 = require("util"); const repl_server_1 = __importDefault(require("../repl-server")); const ansi_colors_1 = require("ansi-colors"); const index_1 = __importDefault(require("../error/index")); const fn_utils_1 = require("../fn-utils"); const runtime_store_1 = __importDefault(require("../runtime-store")); const constants_1 = require("../util_modules/constants"); const fs_1 = require("../util_modules/fs"); const index_2 = require("../util_modules/logger/index"); const option_1 = require("../util_modules/option"); const http_functions_1 = __importDefault(require("./dependencies/http-functions")); const non_http_function_1 = __importDefault(require("./dependencies/non-http-function")); const prepare_1 = __importDefault(require("./prepare")); const fn_watcher_1 = __importDefault(require("../fn-watcher")); const tunnel_server_1 = __importDefault(require("./dependencies/tunnel-server")); const endpoints_1 = require("../endpoints"); const child_process_1 = require("child_process"); const container_plugin_1 = require("@zcatalyst/container-plugin"); const userConfig_1 = __importStar(require("../userConfig")); exports.default = () => __awaiter(void 0, void 0, void 0, function* () { var _a; const shellPrompt = userConfig_1.default.get(userConfig_1.CONFIG_KEYS.functionsShell_prompt); const replServer = new repl_server_1.default({ prompt: shellPrompt && typeof shellPrompt === 'string' ? shellPrompt : ansi_colors_1.bold.cyan('╭─ catalyst-functions-shell\n╰─> '), writer: (output) => { if (output !== undefined) { if (output instanceof Promise) { replServer.pause(); output.then((_res) => { if (_res instanceof child_process_1.ChildProcess) { _res.once('close', replServer.resume); } else if (_res instanceof container_plugin_1.ContainerEvents) { _res.once('exit', replServer.resume); _res.once('error', replServer.resume); } }); return '\n' + '[CLI] Function has been invoked' + '\n'; } if (output instanceof Error) { return ansi_colors_1.bold.red('Error: ') + output.message; } return (0, util_1.inspect)(output); } return ''; } }); const httpServer = new http_functions_1.default({ repl: replServer }); const tunnelServer = new tunnel_server_1.default(replServer); const projectRoot = runtime_store_1.default.get('project.root'); yield (0, prepare_1.default)([constants_1.FN_TYPE.basic, constants_1.FN_TYPE.cron, constants_1.FN_TYPE.event, constants_1.FN_TYPE.integration, constants_1.FN_TYPE.job], true); const targets = runtime_store_1.default .get('context.functions.targets', []) .filter((target) => { if (!target.valid) { (0, index_2.warning)('target [' + target.name + '] is not a valid one reason : ' + target.failure_reason); target.failure_reason instanceof Error && (0, index_2.debug)(`Invalid function target :: ${target.name} :: ${(0, util_1.inspect)(target.failure_reason)}`); if (target.additionalInfo && 'compilerOut' in target.additionalInfo) { Array.isArray(target.additionalInfo.compilerOut.error) && target.additionalInfo.compilerOut.error.forEach((er) => (0, index_2.error)(er)); Array.isArray(target.additionalInfo.compilerOut.warning) && target.additionalInfo.compilerOut.warning.forEach((warn) => (0, index_2.warning)(warn)); } } return target.valid; }); if (targets.length === 0) { throw new index_1.default('No functions are ready for serving', { exit: 0, errorId: 'SHELL-IDX-1' }); } try { replServer.start(); yield tunnelServer.startServer(); const tunnelUrl = runtime_store_1.default.get('context.fn_shell.enable_tunnel', undefined); const _tunnelAPI = yield (0, endpoints_1.tunnelAPI)(); if (tunnelUrl) { try { yield _tunnelAPI.enableTunnel(typeof tunnelUrl === 'string' ? tunnelUrl : undefined); } catch (er) { const _er = index_1.default.getErrorInstance(er); !replServer.closed && replServer.close(); yield tunnelServer.stopServer(); if (_er.status === 400) { const data = (_a = _er.context) === null || _a === void 0 ? void 0 : _a.body.data; throw new index_1.default('Unable to enable tunnel: ' + _er.message, { exit: 1, original: _er, errorId: 'SHELL-IDX-2', arg: [ansi_colors_1.italic.red(data.message + ''), ansi_colors_1.italic.red(data.error_code + '')] }); } throw new index_1.default('Unable to enable tunnel: ' + _er.message, { exit: 2, original: _er }); } replServer.write(ansi_colors_1.green.bold('Tunneling enabled')); } const watchOpt = (0, option_1.getOptionValue)('watch'); if (watchOpt === true) { (0, index_2.info)(); (0, index_2.labeled)('DEPRECATED', `The ${ansi_colors_1.italic.bold('--watch')} option is deprecated. \nThe watch mode will be enabled by default. If you wish to disable it use the ${ansi_colors_1.italic.bold('--no-watch')} option. \nPlease execute ${(0, ansi_colors_1.bold)('catalyst functions:shell ' + (0, ansi_colors_1.italic)('--help'))} command for more usage details.`).WARN(); } yield Promise.all(targets.map((target) => __awaiter(void 0, void 0, void 0, function* () { if (watchOpt !== false) { yield (0, fn_watcher_1.default)(target); } const localFn = new non_http_function_1.default(replServer, target); replServer.setContext(target.name, localFn.call()); tunnelServer.addContext(target.id || target.name, localFn); target.localFn = localFn; }))); if (runtime_store_1.default.get('context.port.http.' + constants_1.FN_TYPE.basic, false)) { yield httpServer.start(); } yield replServer.wait(); yield httpServer.stop(); yield tunnelServer.stopServer().finally(() => __awaiter(void 0, void 0, void 0, function* () { if (!tunnelUrl) { return; } try { const tunnelDetails = yield _tunnelAPI.getTunnel(); if (tunnelDetails.tunnel_status === 'Enabled') { yield _tunnelAPI.disableTunnel(); (0, index_2.info)(); (0, index_2.info)(ansi_colors_1.green.bold('Tunneling disabled')); } } catch (er) { (0, index_2.error)('Error disabling the tunnel'); (0, index_2.debug)(er); } })); yield Promise.all(targets.map((target) => __awaiter(void 0, void 0, void 0, function* () { if (target.localFn) { return target.localFn.slaveManager.shutdown(); } }))); yield fs_1.ASYNC.deleteDir((0, path_1.join)(projectRoot, constants_1.FOLDERNAME.build)).catch(); yield fn_utils_1.fnUtils.common.executeHook({ prefix: 'post', command: 'serve' }); (0, index_2.info)(); (0, index_2.success)('shell complete'); } catch (e) { !replServer.closed && replServer.close(); yield Promise.all([ fs_1.ASYNC.deleteDir((0, path_1.join)(projectRoot, constants_1.FOLDERNAME.build)).catch(), httpServer ? httpServer.stop() : undefined ]); throw e; } });