UNPKG

@fe-hl/command

Version:

common command class

46 lines (44 loc) 1.02 kB
class Command { constructor(commandInstance) { if (!commandInstance) { throw new Error("Commander实例不能为空"); } this.program = commandInstance; let cmd = this.program.command(this.command); cmd.hook("preAction", () => { this.preAction(); }); cmd.hook("postAction", () => { this.postAction(); }); cmd.description(this.description); if (this.options.length > 0) { this.options.forEach((option) => { // 注册option cmd.option(...option); }); } cmd.action((name, options) => { this.action(name, options); }); } get command() { throw new Error("command 方法必须实现"); } get description() { throw new Error("description 方法必须实现"); } get options() { return []; } action() { throw new Error("action 方法必须实现"); } preAction() { // empty } postAction() { // empty } } export default Command;