UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

224 lines (223 loc) 11 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 archiver_1 = __importDefault(require("../../../archiver")); const endpoints_1 = require("../../../endpoints"); const prompt_1 = __importDefault(require("../../../prompt")); const runtime_store_1 = __importDefault(require("../../../runtime-store")); const config_1 = require("../../../util_modules/config"); const constants_1 = require("../../../util_modules/constants"); const fs_1 = require("../../../util_modules/fs"); const logger_1 = require("../../../util_modules/logger"); const option_1 = require("../../../util_modules/option"); const common_1 = require("../../util/common"); const project_1 = require("../../../util_modules/project"); function getAppSailZip() { return __awaiter(this, void 0, void 0, function* () { const _gitHubAPI = yield (0, endpoints_1.gitHubAPI)(); const gitRepo = yield prompt_1.default.ask(prompt_1.default.question('repo', 'Please choose from the list of recommended projects to get started', { type: 'list', choices: [ prompt_1.default.choice(`appsail-nodejs - ${(0, ansi_colors_1.underline)('https://github.com/catalystbyzoho/appsail-nodejs')}`, { value: { api: _gitHubAPI.appsailNodejs.bind(_gitHubAPI), command: 'node index.js', source: (0, project_1.resolveProjectPath)('appsail-nodejs'), build: './', stack: { runtime: 'node16', lang: 'node' }, scripts: { preserve: 'npm install', predeploy: 'npm install' }, memory: 256 }, short: 'appsail-nodejs' }), prompt_1.default.choice(`appsail-java - ${(0, ansi_colors_1.underline)('https://github.com/catalystbyzoho/appsail-java')}`, { value: { api: _gitHubAPI.appsailJava.bind(_gitHubAPI), command: 'java -jar demo-0.0.1-SNAPSHOT.jar', source: (0, project_1.resolveProjectPath)('appsail-java'), build: (0, path_1.join)('target', 'demo-0.0.1-SNAPSHOT.jar'), stack: { runtime: 'java8', lang: 'java' }, scripts: { preserve: './mvnw clean install', predeploy: './mvnw clean install' }, memory: 512, platform: 'javase' }, short: 'appsail-java' }), prompt_1.default.choice(`appsail-python - ${(0, ansi_colors_1.underline)('https://github.com/catalystbyzoho/appsail-python')}`, { value: { api: _gitHubAPI.appsailPython.bind(_gitHubAPI), command: 'python3 app.py', source: (0, project_1.resolveProjectPath)('appsail-python'), build: './', stack: { runtime: 'python_3_9', lang: 'python' }, scripts: { preserve: 'python3 -m pip install -r ./requirements.txt -t ./ --upgrade', predeploy: 'python3 -m pip install -r ./requirements.txt -t ./ --upgrade' }, memory: 256 }, short: 'appsail-python' }) ] })); (0, logger_1.info)(); (0, logger_1.info)((0, ansi_colors_1.white)('=>') + ' Downloading code'); const code = (yield gitRepo.repo.api()); const repoDir = gitRepo.repo.source; yield fs_1.ASYNC.ensureDir(repoDir); const zip = new archiver_1.default(); zip.load(code); zip.extract(repoDir, '/', { isFolder: true, ignoreLevel: 1 }); yield zip.finalize(); (0, logger_1.success)(`Successfully downloaded the code to ${ansi_colors_1.bold.green(gitRepo.repo.source)}`); return { source: gitRepo.repo.source, build: gitRepo.repo.build, stack: gitRepo.repo.stack, command: gitRepo.repo.command, scripts: gitRepo.repo.scripts, memory: gitRepo.repo.memory, platform: gitRepo.repo.platform }; }); } exports.default = () => __awaiter(void 0, void 0, void 0, function* () { const curCommand = (0, option_1.getCurrentCommand)(); const gitPrompt = yield prompt_1.default.ask(prompt_1.default.question('gitClone', 'Do you want to get-started with a list of example AppSails?', { type: 'confirm', default: true, when: curCommand === 'init' })); const pulledSource = gitPrompt.gitClone && (yield getAppSailZip()); const sourcePath = yield prompt_1.default.ask(prompt_1.default.question('currentDir', 'Do you want to initialize an AppSail service in this directory?', { type: 'confirm', default: true, when: !pulledSource })); (0, logger_1.info)(); (0, logger_1.info)((0, ansi_colors_1.white)('=> ') + 'Configure AppSail service'); const _appSailConfig = sourcePath.currentDir ? { source: runtime_store_1.default.get('cwd') } : pulledSource || { source: (yield prompt_1.default.ask(prompt_1.default.question('source', "Please provide the exact path to your AppSail service's source directory: ", { type: 'input', validate: (pth) => __awaiter(void 0, void 0, void 0, function* () { return (0, path_1.isAbsolute)(pth) ? (yield fs_1.ASYNC.dirExists(pth)) ? true : 'Provided path does not exists' : 'Please provide provide an absolute path'; }) }))).source }; const existingSails = config_1.appSailConfig.raw() || []; const appName = yield prompt_1.default.ask(prompt_1.default.question('name', 'Please provide the name for your AppSail: ', { type: 'input', default: 'AppSail', validate: (name) => { const exists = existingSails.findIndex((appSail) => appSail.name === name) !== -1 ? 'AppSail already exists' : true; const match = name.match(constants_1.REGEX.functions.package.name) ? true : `Invalid AppSail name. It should be in the following format: ${constants_1.REGEX.functions.package.name}`; return typeof exists === 'string' ? exists : typeof match === 'string' ? match : true; } })); const payload = { source: (0, path_1.relative)((0, project_1.getProjectRoot)(), _appSailConfig.source) || '.', name: appName.name }; const appConfigPth = (0, path_1.join)(_appSailConfig.source, constants_1.FILENAME.app_config); const isAppConfig = yield fs_1.ASYNC.fileExists(appConfigPth); if (isAppConfig) { (0, logger_1.info)(); (0, logger_1.warning)(`The ${(0, ansi_colors_1.bold)(constants_1.FILENAME.app_config)} file is already present in the appsail directory`); } const overWrite = yield prompt_1.default.ask(prompt_1.default.question('appConfig', 'Do you wish to overwrite the ' + constants_1.FILENAME.app_config, { type: 'confirm', default: false, when: isAppConfig })); const activeWrite = overWrite.appConfig !== false; if (!activeWrite) { runtime_store_1.default.set('payload.appsail.targets', [payload]); return; } const buildPath = yield prompt_1.default.ask(prompt_1.default.question('path', 'Please provide the build path of your AppSail service: ', { type: 'input', rootPath: _appSailConfig.source, when: !_appSailConfig.build && activeWrite })); const appSailStack = _appSailConfig.stack || (yield (0, common_1.getRuntimePromptAnswer)('Please choose a stack for your AppSail: ')); const appConfig = { command: _appSailConfig.command || 'echo Please specify the start-up command in the app-config.json file under the key command for AppSail $X_ZC_RESOURCE_NAME', build_path: _appSailConfig.build || ((0, path_1.isAbsolute)(buildPath.path) ? (0, path_1.relative)(_appSailConfig.source, buildPath.path) || '.' : buildPath.path), stack: appSailStack.runtime, env_variables: {}, memory: _appSailConfig.memory || 256, scripts: _appSailConfig.scripts || {} }; if (appSailStack.lang === 'java') { const platform = yield prompt_1.default.ask(prompt_1.default.question('value', 'Please choose a platform for your AppSail: ', { type: 'list', choices: [ prompt_1.default.choice('JavaSE', { value: 'javase', short: 'JavaSE' }), prompt_1.default.choice('JavaWAR', { value: 'war', short: 'JavaWAR' }) ], when: !_appSailConfig.platform })); appConfig.platform = _appSailConfig.platform || platform.value; if (appConfig.platform === 'war') { delete appConfig.command; } } runtime_store_1.default.set('payload.appsail.targets', [payload]); activeWrite && (yield fs_1.ASYNC.writeJSONFile(appConfigPth, appConfig)); });