UNPKG

@stoked-cenv/cli

Version:

A well considered cli and Node.js library for helping manage application, infrastructure, and configuration management.

143 lines 5.37 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseCommand = void 0; const nest_commander_1 = require("nest-commander"); const lib_1 = require("@stoked-cenv/lib"); const utils_1 = require("../../utils"); class BaseCommand extends nest_commander_1.CommandRunner { constructor() { super(...arguments); this.config = new lib_1.CommandInfo(); this.packages = []; this.log = lib_1.CenvLog.single; } get options() { const opts = {}; const optVals = this.command.optsWithGlobals(); if (!this.command || !this.command.options || !optVals) { return opts; } for (const [key, value] of Object.entries(optVals)) { opts[key] = value; } return opts; } get optionsPrintable() { if (!this.command || !this.options) { return; } const opts = this.command.optsWithGlobals(); let keyValueOptions = ''; for (const [key, value] of Object.entries(opts)) { if (opts) { keyValueOptions += `\t\t${key}: ${value}\n`; } } return keyValueOptions; } get params() { return this.command.processedArgs; } get name() { return this.command.name(); } get fullName() { if (!this.command) { lib_1.CenvLog.single.catchLog('BaseCommand::fullName() called before BaseName.command has been initialized'); process.exit(772); } let cmd = this.command.parent; let result = this.command.name(); while (cmd) { if (cmd.name() === 'cenv') { break; } else { result = `${result}${cmd.name()[0].toUpperCase()}${cmd.name().substring(1)}`; cmd = cmd.parent; } } return result; } parseProfile(val) { return val; } parseLogLevel(val) { return val; } parseHelp(val) { return val; } async run(params, options) { this.processCommandConfig(); if (this.options['help']) { this.command.help(); process.exit(); } const optionVals = this.options; const initRes = await (0, utils_1.cenvSetup)(this.fullName, this.config, params, options); this.packages = initRes.packages ? initRes.packages : []; if (process.env.CENV_CLI_DEBUG) { console.log(`command: ${this.fullName}`); if (this.packages && this.packages.length) { console.log(`\tpackages: ${this.packages.map((p) => p.packageName).join(', ')}`); } if (this.params && this.params.length) { console.log(`\tparams: ${this.params.join(', ')}`); } const printableOptions = this.optionsPrintable; if (printableOptions) { console.log(`\toptions:`); console.log(this.optionsPrintable); } } await this.runCommand(initRes.params, Object.assign({}, initRes.options), initRes.packages); } processCommandConfig() { if (!this.config.allowPackages) { this.config.allowLocalPackage = false; this.config.packagesRequired = false; } if (this.config.allowLocalPackage) { this.config.allowPackages = true; } if (this.config.packagesRequired) { this.config.allowPackages = true; } } } exports.BaseCommand = BaseCommand; __decorate([ (0, nest_commander_1.Option)({ flags: '-p, --profile <profile>', description: `Cenv profile name`, env: 'CENV_PROFILE', }), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", String) ], BaseCommand.prototype, "parseProfile", null); __decorate([ (0, nest_commander_1.Option)({ flags: '-ll, --log-level, <logLevel>', description: `Logging mode`, env: 'CENV_LOG_LEVEL', }), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", String) ], BaseCommand.prototype, "parseLogLevel", null); __decorate([ (0, nest_commander_1.Option)({ flags: '-h, --help', description: 'Display help for command', }), __metadata("design:type", Function), __metadata("design:paramtypes", [Boolean]), __metadata("design:returntype", Boolean) ], BaseCommand.prototype, "parseHelp", null); //# sourceMappingURL=base.command.js.map