diff --git a/lib/command.js b/lib/command.js index 42fedcc23..7814ebe1e 100644 --- a/lib/command.js +++ b/lib/command.js @@ -109,6 +109,19 @@ class Command extends EventEmitter { return this; } + /** + * @returns {Command[]} + * @api private + */ + + _getCommandAndAncestors() { + const result = []; + for (let command = this; command; command = command.parent) { + result.push(command); + } + return result; + } + /** * Define a command. * @@ -828,7 +841,7 @@ Expecting one of '${allowedValues.join("', '")}'`); getOptionValueSourceWithGlobals(key) { // global overwrites local, like optsWithGlobals let source; - getCommandAndParents(this).forEach((cmd) => { + this._getCommandAndAncestors().forEach((cmd) => { if (cmd.getOptionValueSource(key) !== undefined) { source = cmd.getOptionValueSource(key); } @@ -1213,7 +1226,7 @@ Expecting one of '${allowedValues.join("', '")}'`); _chainOrCallHooks(promise, event) { let result = promise; const hooks = []; - getCommandAndParents(this) + this._getCommandAndAncestors() .reverse() .filter(cmd => cmd._lifeCycleHooks[event] !== undefined) .forEach(hookedCommand => { @@ -1582,7 +1595,7 @@ Expecting one of '${allowedValues.join("', '")}'`); */ optsWithGlobals() { // globals overwrite locals - return getCommandAndParents(this).reduce( + return this._getCommandAndAncestors().reduce( (combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()), {} ); @@ -2027,7 +2040,7 @@ Expecting one of '${allowedValues.join("', '")}'`); } const context = this._getHelpContext(contextOptions); - getCommandAndParents(this).reverse().forEach(command => command.emit('beforeAllHelp', context)); + this._getCommandAndAncestors().reverse().forEach(command => command.emit('beforeAllHelp', context)); this.emit('beforeHelp', context); let helpInformation = this.helpInformation(context); @@ -2043,7 +2056,7 @@ Expecting one of '${allowedValues.join("', '")}'`); this.emit(this._helpLongFlag); // deprecated } this.emit('afterHelp', context); - getCommandAndParents(this).forEach(command => command.emit('afterAllHelp', context)); + this._getCommandAndAncestors().forEach(command => command.emit('afterAllHelp', context)); } /** @@ -2186,18 +2199,4 @@ function incrementNodeInspectorPort(args) { }); } -/** - * @param {Command} startCommand - * @returns {Command[]} - * @api private - */ - -function getCommandAndParents(startCommand) { - const result = []; - for (let command = startCommand; command; command = command.parent) { - result.push(command); - } - return result; -} - exports.Command = Command;