UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

99 lines (98 loc) 5.76 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 logger_1 = require("../../../../util_modules/logger"); const integ_1 = require("../../../../fn-utils/lib/integ"); const common_1 = require("../../../../fn-utils/lib/common"); const runtime_1 = __importDefault(require("../../../../util_modules/constants/lib/runtime")); const functions_1 = require("../../../util/functions"); const browserLogic_1 = require("../../../../fn-utils/lib/browserLogic"); const browserLogic_2 = __importDefault(require("../../../../util_modules/constants/lib/browserLogic")); const java_1 = require("../../../../fn-utils/lib/java"); exports.default = (stack) => () => __awaiter(void 0, void 0, void 0, function* () { const fnType = runtime_store_1.default.get('context.functions.type'); const fnDirPath = (0, path_1.join)(runtime_store_1.default.get('context.functions.dir_path')); yield fs_1.ASYNC.ensureDir(fnDirPath); const framework = fnType === constants_1.FN_TYPE.browserLogic ? yield (0, browserLogic_1.getBrowserLogicFramework)('java') : undefined; const functionAns = yield prompt_1.default.ask(prompt_1.default.question('name', 'What will be the name of the java function? This will also be the folder name. ', { defaultAns: 'sample', validate: (ans) => { if (ans.match(constants_1.REGEX.folder_name)) { return true; } return 'Invalid name'; }, suffix: 'NOTE: "This is not the class name"' }), prompt_1.default.question('class', 'What will be the main class name of the java function ?', { defaultAns: 'Sample', validate: (ans) => { if (fn_utils_1.fnUtils.java.isValidClassName(ans)) { return true; } return 'Invalid class name'; } })); 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, logger_1.warning)('Skipping functions java setup...'); return; } yield fs_1.ASYNC.deleteDir(targetPath).catch(); const service = runtime_store_1.default.get('context.functions.integration.service', ''); const templatePath = (0, java_1.getTemplatePath)(fnType, service || framework || ''); if (fnType === constants_1.FN_TYPE.integration) { yield (0, integ_1.copyIntegHandlers)(templatePath, targetPath, runtime_1.default.language.java.value); } else { yield fs_1.ASYNC.copyDir(templatePath, targetPath); } yield fs_1.ASYNC.findAndReplace(targetPath)([constants_1.PLACEHOLDER.functions.java_class, constants_1.PLACEHOLDER.functions.java_name], [functionAns.class, functionAns.name]); 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 }); yield fs_1.ASYNC.rename((0, path_1.join)(fnDirPath, functionAns.name, constants_1.FILENAME.functions.java_main), () => functionAns.class + '.java'); if (fnType === constants_1.FN_TYPE.browserLogic && framework) { (0, logger_1.info)(); (0, logger_1.info)((0, ansi_colors_1.white)('==> ') + 'Downloading dependencies'); const dependencies = browserLogic_2.default.java[framework]; yield (0, browserLogic_1.downloadJavaDependencies)((0, path_1.join)(fnDirPath, functionAns.name, constants_1.FOLDERNAME.java_fn_lib), dependencies, false); } yield (0, common_1.getSDK)(fnType, (0, path_1.join)(fnDirPath, functionAns.name, constants_1.FOLDERNAME.java_fn_lib), 'java', { service }); (0, logger_1.info)(); const userFnClassPath = (0, path_1.join)(fnDirPath, functionAns.name, constants_1.FILENAME.functions.java_classpath); const userFnLibPath = (0, path_1.join)(fnDirPath, functionAns.name, constants_1.FOLDERNAME.java_fn_lib); yield fn_utils_1.fnUtils.java.rewriteClasspath(userFnClassPath, userFnLibPath); return (0, functions_1.fillFunctionsInitPayload)(targetPath, functionAns.name, stack, fnType); });