diff --git a/index.js b/index.js index effde7a56..ed3528a76 100644 --- a/index.js +++ b/index.js @@ -998,7 +998,7 @@ class Command extends EventEmitter { */ hook(event, listener) { - const allowedValues = ['beforeAction', 'afterAction']; + const allowedValues = ['beforeAction', 'afterAction', 'beforeExec']; if (!allowedValues.includes(event)) { throw new Error(`Unexpected value for event passed to hook : '${event}'. Expecting one of '${allowedValues.join("', '")}'`); @@ -1606,10 +1606,14 @@ Expecting one of '${allowedValues.join("', '")}'`); if (!subCommand) this.help({ error: true }); if (subCommand._executableHandler) { - this._executeSubCommand(subCommand, operands.concat(unknown)); - } else { - return subCommand._parseCommand(operands, unknown); - } + let execResult; + execResult = this._chainOrCallHooks(execResult, 'beforeExec'); + execResult = this._chainOrCall(execResult, () => this._executeSubCommand(subCommand, operands.concat(unknown))); + // 'afterExec' is tricky with subcommand close triggering exit + return execResult; + }; + + return subCommand._parseCommand(operands, unknown); }; /**