Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor getCommandAndParents() into Command._getCommandAndAncestors() and use consistently #1939

Merged
merged 3 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 22 additions & 23 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 @@ -1365,13 +1378,13 @@ Expecting one of '${allowedValues.join("', '")}'`);

_checkForMissingMandatoryOptions() {
// Walk up hierarchy so can call in subcommand after checking for displaying help.
for (let cmd = this; cmd; cmd = cmd.parent) {
this._getCommandAndAncestors().forEach((cmd) => {
cmd.options.forEach((anOption) => {
if (anOption.mandatory && (cmd.getOptionValue(anOption.attributeName()) === undefined)) {
cmd.missingMandatoryOptionValue(anOption);
}
});
}
});
}

/**
Expand Down Expand Up @@ -1412,9 +1425,9 @@ Expecting one of '${allowedValues.join("', '")}'`);
*/
_checkForConflictingOptions() {
// Walk up hierarchy so can call in subcommand after checking for displaying help.
for (let cmd = this; cmd; cmd = cmd.parent) {
this._getCommandAndAncestors().forEach((cmd) => {
cmd._checkForConflictingLocalOptions();
}
});
}

/**
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;
12 changes: 6 additions & 6 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ class Help {
if (!this.showGlobalOptions) return [];

const globalOptions = [];
for (let parentCmd = cmd.parent; parentCmd; parentCmd = parentCmd.parent) {
const visibleOptions = parentCmd.options.filter((option) => !option.hidden);
for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {
const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
globalOptions.push(...visibleOptions);
}
if (this.sortOptions) {
Expand Down Expand Up @@ -240,11 +240,11 @@ class Help {
if (cmd._aliases[0]) {
cmdName = cmdName + '|' + cmd._aliases[0];
}
let parentCmdNames = '';
for (let parentCmd = cmd.parent; parentCmd; parentCmd = parentCmd.parent) {
parentCmdNames = parentCmd.name() + ' ' + parentCmdNames;
let ancestorCmdNames = '';
for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {
ancestorCmdNames = ancestorCmd.name() + ' ' + ancestorCmdNames;
}
return parentCmdNames + cmdName + ' ' + cmd.usage();
return ancestorCmdNames + cmdName + ' ' + cmd.usage();
}

/**
Expand Down