Skip to content

Commit aa280af

Browse files
committed
Introduce _getCommandAndAncestors()
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
1 parent 5085ddc commit aa280af

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

lib/command.js

+18-19
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ class Command extends EventEmitter {
122122
return this;
123123
}
124124

125+
/**
126+
* @returns {Command[]}
127+
* @api private
128+
*/
129+
130+
_getCommandAndAncestors() {
131+
const result = [];
132+
for (let command = this; command; command = command.parent) {
133+
result.push(command);
134+
}
135+
return result;
136+
}
137+
125138
/**
126139
* Define a command.
127140
*
@@ -853,7 +866,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
853866
getOptionValueSourceWithGlobals(key) {
854867
// global overwrites local, like optsWithGlobals
855868
let source;
856-
getCommandAndParents(this).forEach((cmd) => {
869+
this._getCommandAndAncestors().forEach((cmd) => {
857870
if (cmd.getOptionValueSource(key) !== undefined) {
858871
source = cmd.getOptionValueSource(key);
859872
}
@@ -1256,7 +1269,7 @@ Call on top-level command instead`);
12561269
_chainOrCallHooks(promise, event) {
12571270
let result = promise;
12581271
const hooks = [];
1259-
getCommandAndParents(this)
1272+
this._getCommandAndAncestors()
12601273
.reverse()
12611274
.filter(cmd => cmd._lifeCycleHooks[event] !== undefined)
12621275
.forEach(hookedCommand => {
@@ -1627,7 +1640,7 @@ Call on top-level command instead`);
16271640
*/
16281641
optsWithGlobals() {
16291642
// globals overwrite locals
1630-
return getCommandAndParents(this).reduce(
1643+
return this._getCommandAndAncestors().reduce(
16311644
(combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()),
16321645
{}
16331646
);
@@ -2072,7 +2085,7 @@ Call on top-level command instead`);
20722085
}
20732086
const context = this._getHelpContext(contextOptions);
20742087

2075-
getCommandAndParents(this).reverse().forEach(command => command.emit('beforeAllHelp', context));
2088+
this._getCommandAndAncestors().reverse().forEach(command => command.emit('beforeAllHelp', context));
20762089
this.emit('beforeHelp', context);
20772090

20782091
let helpInformation = this.helpInformation(context);
@@ -2086,7 +2099,7 @@ Call on top-level command instead`);
20862099

20872100
this.emit(this._helpLongFlag); // deprecated
20882101
this.emit('afterHelp', context);
2089-
getCommandAndParents(this).forEach(command => command.emit('afterAllHelp', context));
2102+
this._getCommandAndAncestors().forEach(command => command.emit('afterAllHelp', context));
20902103
}
20912104

20922105
/**
@@ -2229,18 +2242,4 @@ function incrementNodeInspectorPort(args) {
22292242
});
22302243
}
22312244

2232-
/**
2233-
* @param {Command} startCommand
2234-
* @returns {Command[]}
2235-
* @api private
2236-
*/
2237-
2238-
function getCommandAndParents(startCommand) {
2239-
const result = [];
2240-
for (let command = startCommand; command; command = command.parent) {
2241-
result.push(command);
2242-
}
2243-
return result;
2244-
}
2245-
22462245
exports.Command = Command;

0 commit comments

Comments
 (0)