UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

265 lines (264 loc) 12.4 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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 }); exports.getCustomAppSailSource = exports.getContainerImageTag = exports.getAppSailName = exports.getAppSailZip = void 0; const image_1 = require("@zcatalyst/container-plugin/out/endpoints/image"); const ansi_colors_1 = require("ansi-colors"); const inquirer = __importStar(require("inquirer")); const path_1 = require("path"); const archiver_1 = __importDefault(require("../../../archiver")); const endpoints_1 = require("../../../endpoints"); const error_1 = __importDefault(require("../../../error")); const prompt_1 = __importDefault(require("../../../prompt")); const config_1 = require("../../../util_modules/config"); const constants_1 = require("../../../util_modules/constants"); const container_1 = require("../../../util_modules/container"); const fs_1 = require("../../../util_modules/fs"); const js_1 = require("../../../util_modules/js"); const logger_1 = require("../../../util_modules/logger"); 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.getAppSailZip = getAppSailZip; function getAppSailName() { return __awaiter(this, void 0, void 0, function* () { 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.appsail.name) ? true : 'Invalid AppSail name: Name must contain only alphanumeric and hyphen'; return typeof exists === 'string' ? exists : typeof match === 'string' ? match : true; } })); return appName.name; }); } exports.getAppSailName = getAppSailName; function getContainerImageTag() { return __awaiter(this, void 0, void 0, function* () { yield (0, container_1.isSocketAccessible)(); const images = (yield (0, image_1.getAllLocalImages)()).reduce((_images, img) => { var _a; (_a = img.RepoTags) === null || _a === void 0 ? void 0 : _a.forEach((tag) => _images.push({ imgName: tag, id: img.Id })); return _images; }, []); const searchImages = (filter) => { const filteredImages = []; images.forEach((img) => { var _a; if (filter === '' || img.id.includes(filter) || ((_a = img.imgName) === null || _a === void 0 ? void 0 : _a.toLowerCase().includes(filter.toLowerCase()))) { filteredImages.push({ name: `${img.imgName} ${(0, ansi_colors_1.dim)(img.id.substring(7, 17))}`, value: img.imgName || img.id }); return; } }); return filteredImages; }; yield prompt_1.default.register('search-box'); const image = yield inquirer.prompt([ { type: 'search-box', boxType: 'list', name: 'name', searchable: true, pageSize: 10, highlight: true, notFoundMsg: 'No Images found!', searchMsg: 'Searching images...', message: 'Please search and select the image to link:', source(_answersSoFar, input) { input || (input = ''); return Promise.resolve(searchImages(input)); }, validate: (ansArr) => { if (js_1.JS.isEmpty(ansArr)) { return 'Must select an Image'; } return true; } } ]); return `docker://${image.name}`; }); } exports.getContainerImageTag = getContainerImageTag; function getCustomAppSailSource() { return __awaiter(this, void 0, void 0, function* () { const dockerProtocol = yield prompt_1.default.ask(prompt_1.default.question('type', 'Please choose one of the supported protocols:', { type: 'list', choices: [ prompt_1.default.choice(`Docker image ${(0, ansi_colors_1.italic)('(docker://)')}`, { value: 'image', short: 'Docker image' }), prompt_1.default.choice(`Docker archive ${(0, ansi_colors_1.italic)('(docker-archive://)')}`, { value: 'docker-archive', short: 'Docker archive' }) ] })); switch (dockerProtocol.type) { case 'image': { return getContainerImageTag(); } case 'docker-archive': { const archivePath = yield prompt_1.default.ask(prompt_1.default.question('archive', 'Please provide the path to the docker archive file: ', { type: 'input', validate: (pth) => __awaiter(this, void 0, void 0, function* () { if (!(0, path_1.isAbsolute)(pth)) { return 'Please provide an absolute path'; } if (!(yield fs_1.ASYNC.fileExists(pth))) { return 'Provided path does not exists'; } if (!pth.endsWith('.tar')) { return 'Provided file is not a valid docker archive. Please provide a .tar file'; } return true; }) })); const path = (0, path_1.relative)((0, project_1.getProjectRoot)(), archivePath.archive); return `docker-archive://${path}`; } default: { throw new error_1.default('Unsupported protocol selected for AppSail source', { exit: 2 }); } } }); } exports.getCustomAppSailSource = getCustomAppSailSource;