|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
| 11 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 12 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 13 | +}; |
| 14 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 15 | +const commander_1 = __importDefault(require("commander")); |
| 16 | +const events_1 = require("events"); |
| 17 | +const utils_1 = require("../utils"); |
| 18 | +const error_1 = require("../error"); |
| 19 | +const validOptions = options => { |
| 20 | + if (!options || !options.parent) { |
| 21 | + throw new error_1.CloudBaseError('参数异常,请检查您是否输入了正确的命令!'); |
| 22 | + } |
| 23 | +}; |
| 24 | +class Command extends events_1.EventEmitter { |
| 25 | + constructor(options) { |
| 26 | + super(); |
| 27 | + this.options = options; |
| 28 | + } |
| 29 | + on(event, listener) { |
| 30 | + super.on(event, listener); |
| 31 | + return this; |
| 32 | + } |
| 33 | + init() { |
| 34 | + const { cmd, options, desc, handler, requiredEnvId = true } = this.options; |
| 35 | + let instance = commander_1.default.command(cmd); |
| 36 | + options.forEach(option => { |
| 37 | + instance = instance.option(option.flags, option.desc); |
| 38 | + }); |
| 39 | + instance.description(desc); |
| 40 | + instance.action((...args) => __awaiter(this, void 0, void 0, function* () { |
| 41 | + var _a, _b, _c; |
| 42 | + const cmdOptions = (_a = args.splice(-1)) === null || _a === void 0 ? void 0 : _a[0]; |
| 43 | + const configPath = (_c = (_b = cmdOptions) === null || _b === void 0 ? void 0 : _b.parent) === null || _c === void 0 ? void 0 : _c.configFile; |
| 44 | + const config = yield utils_1.resolveCloudBaseConfig(configPath); |
| 45 | + const envId = yield utils_1.getEnvId(cmdOptions); |
| 46 | + if (!envId && requiredEnvId) { |
| 47 | + throw new error_1.CloudBaseError('未识别到有效的环境 Id 变量,请在项目根目录进行操作或通过 -e 参数指定环境 Id'); |
| 48 | + } |
| 49 | + validOptions(cmdOptions); |
| 50 | + const ctx = { |
| 51 | + cmd, |
| 52 | + envId, |
| 53 | + config, |
| 54 | + options: cmdOptions |
| 55 | + }; |
| 56 | + this.emit('pre-run', ctx, args); |
| 57 | + this.preRun(); |
| 58 | + handler(ctx, ...args); |
| 59 | + })); |
| 60 | + } |
| 61 | + preRun() { } |
| 62 | +} |
| 63 | +exports.Command = Command; |
0 commit comments