UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

148 lines (147 loc) 6.96 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 endpoints_1 = require("../../endpoints"); const iac_1 = __importDefault(require("../../endpoints/lib/iac")); const command_1 = __importDefault(require("../../internal/command")); const prompt_1 = __importDefault(require("../../prompt")); const runtime_store_1 = __importDefault(require("../../runtime-store")); const constants_1 = require("../../util_modules/constants"); const env_1 = require("../../util_modules/env"); const fs_1 = require("../../util_modules/fs"); const logger_1 = require("../../util_modules/logger"); const project_1 = require("../../util_modules/project"); const functions_1 = __importDefault(require("../../pull/features/functions")); const features_1 = require("../../init/features"); const error_1 = __importDefault(require("../../error")); const code_deck_1 = require("../../code-deck"); const option_1 = require("../../util_modules/option"); exports.default = new command_1.default('codelib:install [git-url]') .description('Install the code library functionalities into your project') .option('-c, --continue <install-id>', 'Continue CodeLib install') .needs('auth', [constants_1.SCOPE.project_import_create]) .needs('config', { optional: true }) .needs('rc') .action((gitUrl) => __awaiter(void 0, void 0, void 0, function* () { var _a; const continueOpt = (0, option_1.getOptionValue)('continue', false); let [_importId, _gitUrl] = ['', '']; if (continueOpt) { if (gitUrl) { throw new error_1.default('Cannot specify [git-url] when using --continue option'); } try { [_importId, _gitUrl] = code_deck_1.CodeDeckToken.getDetailsFromToken(continueOpt); } catch (err) { throw new error_1.default('Invalid token', { original: err }); } } const _gitHubAPI = yield (0, endpoints_1.gitHubAPI)(); gitUrl = gitUrl || _gitUrl; if (gitUrl) { const headRes = yield _gitHubAPI.validateUrl(gitUrl); if (headRes !== 200) { (0, logger_1.error)('Invalid GitHub URL: ' + ansi_colors_1.italic.red(`"${gitUrl}"`)); const listAns = yield prompt_1.default.ask(prompt_1.default.question('list', 'Do you wish to select from a list of available Code Library installations from GitHub?', { type: 'confirm', defaultAns: true })); if (!listAns.list) { return; } gitUrl = undefined; } } const code = gitUrl ? { github: { url: gitUrl } } : yield (0, code_deck_1.getDeckFromGit)(); (0, logger_1.info)(); !continueOpt && (0, logger_1.info)((0, ansi_colors_1.white)('==> ') + 'Downloading CodeLib'); let deckZip = yield _gitHubAPI.download(code.github.url, continueOpt ? null : code.title); if (!deckZip) { if (!gitUrl) { throw new error_1.default('Invalid code lib', { exit: 2 }); } throw new error_1.default(`Error while downloading codelib.`, { exit: 2, original: `Occurred while processing ${gitUrl}` }); } const templateFile = yield (0, code_deck_1.getTemplateFile)(deckZip); const templateJson = JSON.parse(templateFile || ''); const tmpFile = yield fs_1.ASYNC.tempFile('deck.zip', deckZip); deckZip = undefined; const templateComponents = Object.entries(templateJson.components).reduce((compRed, _comp) => { const [key, comp] = _comp; compRed[key] = (comp || []) .map((item) => { switch (key) { case 'Functions': { const props = item.properties; return `* ${item.name} (${props.stack}) (${props.type === 'applogic' ? 'advancedio' : props.type})`; } case 'Filestore': { if (item.type === 'folder') { return `* ${item.name} (${item.type})`; } return ''; } default: { return '* ' + item.name; } } }) .filter((item) => item) .join('\n\t\t'); return compRed; }, {}); if (!continueOpt) { (0, logger_1.info)(); (0, logger_1.info)(`The following components will be installed with ${code.title ? (0, ansi_colors_1.bold)(code.title) : 'this'} CodeLib`); (0, logger_1.info)(); (0, logger_1.info)(Object.entries(templateComponents) .map(([name, comp]) => (0, ansi_colors_1.bold)('\t' + name + ':') + '\n' + (0, ansi_colors_1.yellow)('\t\t' + comp)) .join('\n')); (0, logger_1.info)(); const install = yield prompt_1.default.ask(prompt_1.default.question('consent', 'Do you wish to continue? ', { type: 'confirm', default: true })); if (!install.consent) { (0, logger_1.message)('Aborted by user'); return; } const projectId = (0, project_1.getProjectId)(); const iacAPI = new iac_1.default(projectId); const schedulerRes = yield iacAPI.update(fs_1.SYNC.getReadStream(tmpFile)); _importId = schedulerRes.id; } if (!env_1.isPrimaryShell || !_importId) { return; } (0, logger_1.info)(); (0, logger_1.info)((0, ansi_colors_1.white)('==> ') + 'Installing CodeLib'); const importRes = yield (0, code_deck_1.listenDeploy)(_importId, code.github.url); if (!importRes) { return; } const templateFns = (((_a = templateJson.components) === null || _a === void 0 ? void 0 : _a.Functions) || []); if (templateFns.length > 0) { yield new Promise((res) => setTimeout(res, 5000)); yield (0, functions_1.default)(templateFns.map((fn) => fn.name)); (0, features_1.setCatalystConfig)('functions', true); const config = runtime_store_1.default.get('config'); yield config.save(); } (0, logger_1.success)('CodeLib successfully installed'); }));