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
  • Loading branch information
aweebit committed Aug 5, 2023
1 parent 5085ddc commit aa280af
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 @@ -122,6 +122,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 @@ -853,7 +866,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 @@ -1256,7 +1269,7 @@ Call on top-level command instead`);
_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 @@ -1627,7 +1640,7 @@ Call on top-level command instead`);
*/
optsWithGlobals() {
// globals overwrite locals
return getCommandAndParents(this).reduce(
return this._getCommandAndAncestors().reduce(
(combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()),
{}
);
Expand Down Expand Up @@ -2072,7 +2085,7 @@ Call on top-level command instead`);
}
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 @@ -2086,7 +2099,7 @@ Call on top-level command instead`);

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 @@ -2229,18 +2242,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 aa280af

Please sign in to comment.