UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

136 lines (135 loc) 7.16 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 path_1 = require("path"); const appsail_utils_1 = require("../../appsail-utils"); const error_1 = __importDefault(require("../../error")); const java_1 = require("../../fn-utils/lib/java"); const node_1 = require("../../fn-utils/lib/node"); const ensure_python_1 = require("../../init/dependencies/python/ensure-python"); const port_resolver_1 = __importDefault(require("../../port-resolver")); const runtime_store_1 = __importDefault(require("../../runtime-store")); const config_1 = require("../../util_modules/config"); const constants_1 = require("../../util_modules/constants"); const index_1 = require("../../util_modules/logger/index"); exports.default = () => __awaiter(void 0, void 0, void 0, function* () { const allTargets = yield config_1.appSailConfig.getAllTargetDetails(); const optionTargets = (0, appsail_utils_1.filterTargets)(allTargets || []); if (optionTargets.length === 0) { (0, index_1.debug)('AppSail targets are empty'); return []; } const validated = yield (0, appsail_utils_1.validateAppSail)(optionTargets); const binPathMap = {}; yield Promise.all(validated.map((target) => __awaiter(void 0, void 0, void 0, function* () { var _a; try { const stack = (_a = target.config) === null || _a === void 0 ? void 0 : _a.stack; if (!stack) { throw new error_1.default(`Stack is not defined for AppSail target ${target.name}`, { exit: 2 }); } if (binPathMap[stack]) { return; } switch (true) { case stack.startsWith('node'): { binPathMap[stack] = yield (0, node_1.ensureNodeVersion)(stack, true); break; } case stack.startsWith('java'): { const javaBinPath = yield (0, java_1.ensureJava)(stack); if (!javaBinPath) { throw new error_1.default(`Invalid bin path for ${stack}`, { exit: 1 }); } binPathMap[stack] = javaBinPath; break; } case stack.startsWith('python'): { const stackVersion = stack.replace('python_', ''); const pyExe = yield (0, ensure_python_1.ensurePython)(stackVersion.replace('_', '.'), false, true); binPathMap[stack] = (0, path_1.dirname)(pyExe); break; } } } catch (err) { const error = error_1.default.getErrorInstance(err); target.validity.valid = false; target.validity.reason = error.message; } }))); const filledTargets = yield Promise.all(validated.map((targ) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; if (!targ.validity.valid) { return targ; } const proxyPort = yield port_resolver_1.default.getPort('appsail', 'http', { name: targ.name + '_proxy', searchSpan: 10, server: 'master' }).catch((err) => { throw new error_1.default('No free ports between 3000 and 3010. AppSail needs the ports within this range to function properly', { original: err }); }); const AppSailServerDetails = Object.assign(Object.assign({}, targ), { binPath: ((_a = targ.config) === null || _a === void 0 ? void 0 : _a.stack) ? binPathMap[targ.config.stack] : '', port: { appsail: { host: -1 }, proxy: proxyPort, debug: { host: runtime_store_1.default.get(`context.port.debug.appsail.${targ.name}`, -1), container: (_c = (_b = targ.config) === null || _b === void 0 ? void 0 : _b.port) === null || _c === void 0 ? void 0 : _c.debug } } }); if (targ.runtime === 'catalyst') { try { const appsailPort = ((_e = (_d = targ.config) === null || _d === void 0 ? void 0 : _d.port) === null || _e === void 0 ? void 0 : _e.http) ? yield port_resolver_1.default.getFreePort(targ.config.port.http) : yield port_resolver_1.default.getPort('appsail', 'http', { name: targ.name + '_proxy', server: 'service', searchSpan: constants_1.DEFAULT.serve_port.search_span.http_port }); AppSailServerDetails.port.appsail.host = appsailPort; } catch (er) { throw new error_1.default(((_g = (_f = targ.config) === null || _f === void 0 ? void 0 : _f.port) === null || _g === void 0 ? void 0 : _g.http) ? `The port ${(_j = (_h = targ.config) === null || _h === void 0 ? void 0 : _h.port) === null || _j === void 0 ? void 0 : _j.http} configured for the AppSail ${targ.name} is not available` : 'Unable to find a free port to serve the AppSail: ' + targ.name, { exit: 1, original: er }); } } else { try { const hostBindPort = yield port_resolver_1.default.getPort('appsail', 'http', { name: targ.name + '_proxy', server: 'service', searchSpan: constants_1.DEFAULT.serve_port.search_span.http_port }); AppSailServerDetails.port.appsail.host = hostBindPort; AppSailServerDetails.port.appsail.container = (_l = (_k = targ.config) === null || _k === void 0 ? void 0 : _k.port) === null || _l === void 0 ? void 0 : _l.http; } catch (er) { throw new error_1.default('Unable to find a host port to bind with the AppSail container for serving the AppSail: ' + targ.name, { exit: 1, original: er }); } } return AppSailServerDetails; }))); runtime_store_1.default.set('context.appsail.targets', filledTargets); return filledTargets; });