zcatalyst-cli
Version:
Command Line Tool for CATALYST
48 lines (47 loc) • 4.49 kB
JavaScript
;
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 port_resolver_1 = __importDefault(require("../../../port-resolver"));
const constants_1 = require("../../../util_modules/constants");
const path_1 = require("path");
const shell_1 = require("../../../util_modules/shell");
const userConfig_1 = __importDefault(require("../../../userConfig"));
const project_1 = require("../../../util_modules/project");
const runtime_store_1 = __importDefault(require("../../../runtime-store"));
const logger_1 = require("../../../util_modules/logger");
exports.default = (fn) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
const slaveOptions = [];
const stackVersion = (_b = (_a = fn.target) === null || _a === void 0 ? void 0 : _a.stack) === null || _b === void 0 ? void 0 : _b.replace('python_', '');
const runtimesDir = constants_1.ENVPATH.runtimes.data;
const httpPort = parseInt(runtime_store_1.default.get('context.port.http.' + constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.basic], constants_1.DEFAULT.serve_port.http[(_c = fn.target) === null || _c === void 0 ? void 0 : _c.type] + ''), 10);
const runTimePort = yield port_resolver_1.default.getFreePort(httpPort, 20, false);
fn.httpPort = runTimePort;
slaveOptions.push((0, path_1.join)(runtimesDir, constants_1.RUNTIME.language.python.value, `zcatalyst_runtime_${stackVersion === null || stackVersion === void 0 ? void 0 : stackVersion.replace('_', '')}`, 'main.py'));
const debugConfig = (_d = fn.target.additionalInfo) === null || _d === void 0 ? void 0 : _d.debug;
if (debugConfig) {
const debugOpts = ['-m', debugConfig === null || debugConfig === void 0 ? void 0 : debugConfig.debugger, '--listen', debugConfig.port + ''];
((_e = fn.target) === null || _e === void 0 ? void 0 : _e.type) === constants_1.FN_TYPE.job && debugOpts.push('--wait-for-client');
slaveOptions.unshift(...debugOpts);
}
slaveOptions.unshift('-u');
(0, logger_1.debug)(`Python Spawn command: ${userConfig_1.default.get(`python${stackVersion}.bin`)} ${slaveOptions.join(' ')}`);
return (0, shell_1.spawn)(userConfig_1.default.get(`python${stackVersion}.bin`), slaveOptions, {
cwd: (_f = fn.target) === null || _f === void 0 ? void 0 : _f.build,
stdio: 'pipe',
env: Object.assign({ X_ZOHO_CATALYST_IS_LOCAL: 'true', X_ZOHO_CATALYST_FUNCTION_LOADED: 'true', X_ZOHO_CATALYST_ACCOUNTS_URL: constants_1.ORIGIN.auth, X_ZOHO_CATALYST_CONSOLE_URL: constants_1.ORIGIN.admin, CATALYST_PORTAL_DOMAIN: constants_1.ORIGIN.iamPortal, X_ZOHO_CATALYST_RESOURCE_ID: (_g = fn.target) === null || _g === void 0 ? void 0 : _g.id, X_ZOHO_STRATUS_RESOURCE_SUFFIX: constants_1.ORIGIN.stratusSuffix, X_ZOHO_CATALYST_CODE_LOCATION: (_h = fn.target) === null || _h === void 0 ? void 0 : _h.build, X_ZOHO_CATALYST_SERVER_LISTEN_PORT: runTimePort.toString(), X_ZOHO_DATA_URL: `http://localhost:${runTimePort}/data`, X_ZOHO_CALLBACK_URL: `http://localhost:${runTimePort}/callback`, CATALYST_PROJECT_TIMEZONE: (0, project_1.getProjectTimezone)(Intl.DateTimeFormat().resolvedOptions().timeZone), X_ZOHO_ADMIN_CRED_TOKEN: 'dummy', X_ZOHO_PROJECT_SECRET_KEY: 'dummy', X_ZOHO_CATALYST_ORG: (0, project_1.getEnvId)(), X_ZOHO_CATALYST_ENVIRONMENT: 'Development', X_ZOHO_JOBMETA_JOBID: 'dummy', CATALYST_FUNCTION_TYPE: constants_1.REMOTE_REF.functions.type[(_j = fn.target) === null || _j === void 0 ? void 0 : _j.type] }, (_k = fn.target) === null || _k === void 0 ? void 0 : _k.env_var)
})
.RAW()
.on('exit', () => port_resolver_1.default.freePort(httpPort));
});