UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

138 lines (137 loc) 5.79 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.Client = void 0; const commands_1 = require("./commands"); const commander_1 = require("commander"); const index_js_1 = __importDefault(require("./error/index.js")); const runtime_store_1 = __importDefault(require("./runtime-store")); const utils_js_1 = require("./error/utils.js"); const SYNC = __importStar(require("./util_modules/fs/lib/sync.js")); const path_1 = require("path"); const file_names_js_1 = __importDefault(require("./util_modules/constants/lib/file-names.js")); class Client { get cli() { return this._cli; } constructor(program) { this._cli = program; this.load = new Promise(() => { }); } init(command) { return __awaiter(this, void 0, void 0, function* () { if (command === undefined) { this.load = (0, commands_1.loadAllCommands)(this); } else { this.load = Promise.all([(0, commands_1.loadCommand)(this, command)]).catch((err) => { throw new index_js_1.default(`${command} is not a valid catalyst command`, { original: index_js_1.default.getErrorInstance(err, { skipHelp: true }), skipHelp: true }); }); } return this.load; }); } processArgs(...args) { return __awaiter(this, void 0, void 0, function* () { yield this.load; const origArgs = args.slice(2); if (!origArgs.length || (origArgs.length === 1 && origArgs.pop() === '--verbose')) { this.cli.outputHelp(); } else { yield this.cli.parseAsync(args); } return this.cli; }); } getCommand(name) { for (const command of this.cli.commands) { if (command.name() === name || command.alias() === name) { return command; } } return null; } getAllCommandNames() { return this.cli.commands.map((cmd) => { return cmd.name(); }); } convertOptions(options) { const optionsArr = []; Object.entries(options).forEach(([key, value]) => { if (typeof value === 'boolean') { value && optionsArr.push('--' + key); return; } optionsArr.push('--' + key); optionsArr.push(typeof value === 'string' ? value : value.join(',')); }); return optionsArr; } exec(command, projectRoot, cwd, { args = [], options = {}, inputs } = {}) { return __awaiter(this, void 0, void 0, function* () { runtime_store_1.default.set('context', {}); utils_js_1.runtime.set('context', {}); runtime_store_1.default.set('cwd', cwd); runtime_store_1.default.set('project.root', projectRoot); runtime_store_1.default.set('prompt', inputs); const programArgs = [...args, ...this.convertOptions(options)]; this._cli = new commander_1.Command(); yield this.init(command); yield this.processArgs('node', 'catalyst', command, ...programArgs); const code = runtime_store_1.default.get('exitCode', 2); try { const logFile = (0, path_1.join)(cwd, file_names_js_1.default.log); if (code < 2 && SYNC.fileExists(logFile)) { SYNC.deleteFile(logFile); } } catch (err) { console.error(`Unable to delete the ${file_names_js_1.default.log} file`, err); } return { exitCode: code, error: runtime_store_1.default.get('context.error.command', undefined) }; }); } } exports.Client = Client;