Skip to content
Myr

Introduce _getCommandAndAncestors()

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Replaces getCommandAndParents(): - turned into an instance method - used "ancestors" instead of "parents" because it is more precise (cherry picked from commit aa280afbf8b0fde08566ebd9c20cc85df2a62c6a)

authored by Wee Bit · committed by Abe Tomoaki · parent 96f076d · Browse files · 1 file+18 added−19 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+18 added−19 removed

  • other:+18 added−19 removed

1 file+18 added−19 removed

modified:lib/command.js+18 added-19 removed
108 unmodified lines
109 return this;110 }111112 /**113 * @returns {Command[]}114 * @api private115 */116117 _getCommandAndAncestors() {118 const result = [];119 for (let command = this; command; command = command.parent) {120 result.push(command);121 }122 return result;123 }124125 /**126 * Define a command.127 *128 * There are two styles of command: pay attention to where to put the description.129 *
710 unmodified lines
840841 getOptionValueSourceWithGlobals(key) {842 // global overwrites local, like optsWithGlobals843 let source;831 getCommandAndParents(this).forEach((cmd) => {844 this._getCommandAndAncestors().forEach((cmd) => {845 if (cmd.getOptionValueSource(key) !== undefined) {846 source = cmd.getOptionValueSource(key);847 }848 });
376 unmodified lines
12251226 _chainOrCallHooks(promise, event) {1227 let result = promise;1228 const hooks = [];1216 getCommandAndParents(this)1229 this._getCommandAndAncestors()1230 .reverse()1231 .filter(cmd => cmd._lifeCycleHooks[event] !== undefined)1232 .forEach(hookedCommand => {1233 hookedCommand._lifeCycleHooks[event].forEach((callback) => {
360 unmodified lines
1594 * @return {Object}1595 */1596 optsWithGlobals() {1597 // globals overwrite locals1585 return getCommandAndParents(this).reduce(1598 return this._getCommandAndAncestors().reduce(1599 (combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()),1600 {}1601 );1602 }
436 unmodified lines
2039 contextOptions = undefined;2040 }2041 const context = this._getHelpContext(contextOptions);20422030 getCommandAndParents(this).reverse().forEach(command => command.emit('beforeAllHelp', context));2043 this._getCommandAndAncestors().reverse().forEach(command => command.emit('beforeAllHelp', context));2044 this.emit('beforeHelp', context);20452046 let helpInformation = this.helpInformation(context);2047 if (deprecatedCallback) {
7 unmodified lines
2055 if (this._helpLongFlag) {2056 this.emit(this._helpLongFlag); // deprecated2057 }2058 this.emit('afterHelp', context);2046 getCommandAndParents(this).forEach(command => command.emit('afterAllHelp', context));2059 this._getCommandAndAncestors().forEach(command => command.emit('afterAllHelp', context));2060 }20612062 /**2063 * You can pass in flags and a description to override the help
134 unmodified lines
2198 return arg;2199 });2200}22012189/**2190 * @param {Command} startCommand2191 * @returns {Command[]}2192 * @api private2193 */21942195function getCommandAndParents(startCommand) {2196 const result = [];2197 for (let command = startCommand; command; command = command.parent) {2198 result.push(command);2199 }2200 return result;2201}22022202exports.Command = Command;