Skip to content

Commit

Permalink
Introduce _getCommandAndAncestors()
Browse files Browse the repository at this point in the history
Replaces getCommandAndParents():
- turned into an instance method
- used "ancestors" instead of "parents" because it is more specific and
  to avoid confusion with the recently introduced parents array

(cherry picked from commit aa280af)
  • Loading branch information
aweebit committed Aug 5, 2023
1 parent d038570 commit ed2b8c5
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -825,7 +838,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);
}
Expand Down Expand Up @@ -1208,7 +1221,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 => {
Expand Down Expand Up @@ -1577,7 +1590,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()),
{}
);
Expand Down Expand Up @@ -2022,7 +2035,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);
Expand All @@ -2036,7 +2049,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));
}

/**
Expand Down Expand Up @@ -2179,18 +2192,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;

0 comments on commit ed2b8c5

Please sign in to comment.