UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

246 lines (245 loc) 9.51 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 }); const ansi_colors_1 = require("ansi-colors"); const strip_ansi_1 = __importDefault(require("strip-ansi")); const error_1 = __importDefault(require("../error")); const errorOut_1 = __importDefault(require("../errorOut")); const migration_1 = __importDefault(require("../migration")); const runtime_store_1 = __importDefault(require("../runtime-store")); const track_1 = __importDefault(require("../track")); const js_1 = require("../util_modules/js"); const logger_1 = require("../util_modules/logger"); const option_1 = require("../util_modules/option"); const project_1 = require("../util_modules/project"); const env_1 = require("../util_modules/env"); class Command { constructor(cmd) { this.subCommand = []; this.isSubCommand = false; this.isCICommand = true; this.command = cmd; this.cmdName = cmd.split(' ')[0]; this.aliasName = null; this.cmdDescription = null; this.cmdAction = () => Promise.resolve(); this.cmdOptions = []; this.beforeRunners = []; this.optIgnores = []; this.helpText = null; this.client = null; this.allowUnknownOptions = false; } description(description) { this.cmdDescription = description; return this; } alias(alias) { this.aliasName = alias; return this; } allowUnknownOpts() { this.allowUnknownOptions = true; return this; } option(...args) { this.cmdOptions.push(args); return this; } needs(fn, args) { this.beforeRunners.push({ fn, args }); return this; } help(text) { this.helpText = text; return this; } addSubCommand(...commands) { commands.forEach((command) => { this.subCommand.push(command); command.isSubCommand = true; }); return this; } ci(value) { this.isCICommand = value; return this; } helpConfig(config) { this._helpConfig = config; return this; } usage(cmdUsage) { this.cmdUsage = cmdUsage; return this; } action(fn) { this.cmdAction = fn; return this; } ignore(opts) { this.optIgnores = opts; return this; } register(client, cli) { var _a; this.client = client; const program = cli || client.cli; let opts = {}; if (env_1.isCI && !this.isCICommand) { opts = { hidden: true }; } const cmd = program.command(this.command, opts); if (this.aliasName !== null) { cmd.alias(this.aliasName); } if (this.cmdDescription !== null) { cmd.description(this.cmdDescription); } if (this.cmdUsage) { cmd.usage(this.cmdUsage); } if (this.helpText !== null) { cmd.on('--help', () => { (0, logger_1.info)(this.helpText); }); } if (this._helpConfig) { cmd.configureHelp(this._helpConfig); } cmd.allowUnknownOption(this.allowUnknownOptions); this.cmdOptions.forEach((args) => { if (args.length > 0) { cmd.option(...args); } }); cmd.action((...programArgs) => __awaiter(this, void 0, void 0, function* () { const runner = this.runner(); const start = Date.now(); const argCount = cmd._args.length; if (!this.isCICommand && env_1.isCI) { return (0, errorOut_1.default)(new error_1.default('Environment not supported', { exit: 1, errorId: 'CMD-2', arg: [(0, ansi_colors_1.bold)(cmd.name()), (0, ansi_colors_1.bold)('CI=true catalyst help')] })); } if (programArgs.slice(-1)[0].args.length > argCount && !this.allowUnknownOptions) { return (0, errorOut_1.default)(new error_1.default('Too many arguments', { exit: 1, errorId: 'CMD-1', arg: [(0, ansi_colors_1.bold)(cmd.name()), (0, ansi_colors_1.bold)('catalyst help ' + cmd.name())] })); } try { yield runner.apply(this, programArgs); const duration = Date.now() - start; (0, errorOut_1.default)(); if (!env_1.isCI) { yield (0, track_1.default)(this.cmdName, 'success', duration); } } catch (err) { (0, errorOut_1.default)(err); if (!env_1.isCI) { const duration = Date.now() - start; const errorEvent = err.exit === 1 ? 'Error (User)' : 'Error (Unexpected)'; const preppedMessage = (0, strip_ansi_1.default)(err.message || ''); yield (0, track_1.default)(errorEvent, preppedMessage, duration); } } })); if (this.subCommand) { (_a = this.subCommand) === null || _a === void 0 ? void 0 : _a.forEach((subCmd) => { subCmd.register(client, cmd); }); } return cmd; } _prepare(cmd) { var _a; const globalopts = ((_a = cmd.parent) === null || _a === void 0 ? void 0 : _a.opts()) || {}; Command.globalOpts.forEach((opts) => { const optsValue = globalopts[opts]; if (optsValue === undefined) { return; } if (js_1.JS.indexOf(this.optIgnores, opts) > -1) { (0, logger_1.warning)('Cannot use option ' + (0, ansi_colors_1.bold)(opts) + ' along with ' + (0, ansi_colors_1.bold)(this.command) + ' command. Hence ignored.'); return; } (0, option_1.setGlobalOption)(opts, optsValue); }); runtime_store_1.default.set('project.root', (0, project_1.getProjectRoot)()); runtime_store_1.default.set('cwd', process.cwd()); } runner() { return (...args) => __awaiter(this, void 0, void 0, function* () { if (args.length === 0) { args.push({}); } const [opts, command] = args.slice(-2); const unknownOpts = this.allowUnknownOptions ? command.parseOptions(process.argv).unknown : undefined; runtime_store_1.default.set('opts', Object.assign({ _name: command.name(), unknownOpts }, opts)); this._prepare(this.isSubCommand && command.parent ? command.parent : command); try { yield (0, migration_1.default)(runtime_store_1.default.get('context.cli.package.version')); } catch (e) { (0, logger_1.debug)(e); } yield Promise.all(js_1.JS.map(this.beforeRunners, (before) => __awaiter(this, void 0, void 0, function* () { var _a; const beforeModule = (yield (_a = '../command_needs/' + before.fn, Promise.resolve().then(() => __importStar(require(_a))))).default; return beforeModule.call(this, before.args); }))); return this.cmdAction.call(this, ...args); }); } } Command.globalOpts = ['project', 'token', 'dc', 'org', 'verbose']; exports.default = Command;