UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

134 lines (133 loc) 6.82 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 ansi_colors_1 = require("ansi-colors"); const path_1 = require("path"); const prompt_1 = __importDefault(require("../../../../prompt")); 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_1 = require("../../../../util_modules/logger/index"); const js_1 = require("../../../../util_modules/js"); const integ_1 = require("../../../../fn-utils/lib/integ"); const runtime_1 = __importDefault(require("../../../../util_modules/constants/lib/runtime")); const functions_1 = require("../../../util/functions"); const project_1 = require("../../../../util_modules/project"); const ensure_python_1 = require("../../../dependencies/python/ensure-python"); const error_1 = __importDefault(require("../../../../error")); const pip_install_1 = require("../../../dependencies/python/pip-install"); const cli_table_1 = require("../../../../cli_table"); const logger_1 = require("../../../../util_modules/logger"); exports.default = (stack) => () => __awaiter(void 0, void 0, void 0, function* () { const fnType = runtime_store_1.default.get('context.functions.type'); const fnDirPath = runtime_store_1.default.get('context.functions.dir_path'); yield fs_1.ASYNC.ensureDir(fnDirPath); const service = runtime_store_1.default.get('context.functions.integration.service', ''); const templatePath = fnType === constants_1.FN_TYPE.integration ? js_1.JS.get(constants_1.TEMPLATE.functions.python[fnType], service) : constants_1.TEMPLATE.functions.python[fnType]; const functionAns = yield prompt_1.default.ask(prompt_1.default.question('name', 'package name: ', { defaultAns: js_1.JS.snakeCase((0, project_1.getProjectName)('sample') + '_function'), validate: (ans) => { if (ans.match(constants_1.REGEX.folder_name)) { return true; } return 'Invalid package name'; } }), prompt_1.default.question('main', 'entry point: ', { defaultAns: 'main.py', validate: (ans) => { if (js_1.JS.endsWith(ans, '.py')) { return true; } return 'Invalid entry point'; } })); const targetPath = (0, path_1.join)(fnDirPath, functionAns.name); const dirExists = yield fs_1.ASYNC.dirExists(targetPath); const overwriteAns = dirExists ? yield prompt_1.default.ask(prompt_1.default.question('overwrite', 'Directory ' + (0, ansi_colors_1.underline)(functionAns.name) + ' already exists inside functions directory. Overwrite ?', { type: 'confirm', defaultAns: false })) : { overwrite: true }; if (!overwriteAns.overwrite) { (0, index_1.warning)('Skipping functions python setup...'); return; } yield fs_1.ASYNC.deleteDir(targetPath).catch(); if (fnType === constants_1.FN_TYPE.integration) { yield (0, integ_1.copyIntegHandlers)(templatePath, targetPath, runtime_1.default.language.python.value); } else { yield fs_1.ASYNC.copyDir(templatePath, targetPath); } const pyRespJson = fnType === constants_1.FN_TYPE.integration ? yield (0, pip_install_1.pypiRes)('zcatalyst-cliq') : yield (0, pip_install_1.pypiRes)('zcatalyst-sdk'); const latestVersion = (() => { const releases = pyRespJson.releases; const latest = Object.entries(releases).reduce((latest, [version, details]) => { var _a; const whlDetails = details.at(0); if (!(whlDetails === null || whlDetails === void 0 ? void 0 : whlDetails.upload_time)) { return latest; } const uploadTime = new Date(whlDetails.upload_time).getTime(); if (!((_a = latest.details) === null || _a === void 0 ? void 0 : _a.upload_time) || uploadTime > new Date(latest.details.upload_time).getTime()) { latest = { version, details: whlDetails }; } return latest; }, {}); return latest.version; })(); yield fs_1.ASYNC.findAndReplace(targetPath)([ constants_1.PLACEHOLDER.functions.python_package.name, constants_1.PLACEHOLDER.functions.python_package.main, constants_1.PLACEHOLDER.functions.python_package.sdkVersion ], [functionAns.name, functionAns.main, latestVersion]); yield fs_1.ASYNC.rename((0, path_1.join)(targetPath, constants_1.FILENAME.functions.python_main), () => functionAns.main); const integ_config = runtime_store_1.default.get('context.functions.integration.config', ''); yield fn_utils_1.fnUtils.common.findAndReplaceConfigProps(targetPath, { stack, type: fnType, integ_config, integ_handler: runtime_store_1.default.get('context.functions.integration.handlers', '') }); const stackVersion = stack.replace('python_', ''); try { yield (0, ensure_python_1.ensurePython)(stackVersion.replace('_', '.'), false, true); } catch (err) { const error = error_1.default.getErrorInstance(err); const table = (0, cli_table_1.getCustomColourTable)(ansi_colors_1.yellow); table.push([ (0, ansi_colors_1.yellow)(logger_1.CHAR.warning + ' ') + error.message + '\n' + `you can set it later with the command ` + `"${(0, ansi_colors_1.italic)((0, ansi_colors_1.bold)(`catalyst config:set python${stackVersion}.bin=<binary_path>`))}"` ]); (0, index_1.info)(table.toString()); } (0, functions_1.fillFunctionsInitPayload)(targetPath, functionAns.name, stack, fnType); return targetPath; });