@@ -122,6 +122,19 @@ class Command extends EventEmitter {
122
122
return this ;
123
123
}
124
124
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
+
125
138
/**
126
139
* Define a command.
127
140
*
@@ -853,7 +866,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
853
866
getOptionValueSourceWithGlobals ( key ) {
854
867
// global overwrites local, like optsWithGlobals
855
868
let source ;
856
- getCommandAndParents ( this ) . forEach ( ( cmd ) => {
869
+ this . _getCommandAndAncestors ( ) . forEach ( ( cmd ) => {
857
870
if ( cmd . getOptionValueSource ( key ) !== undefined ) {
858
871
source = cmd . getOptionValueSource ( key ) ;
859
872
}
@@ -1256,7 +1269,7 @@ Call on top-level command instead`);
1256
1269
_chainOrCallHooks ( promise , event ) {
1257
1270
let result = promise ;
1258
1271
const hooks = [ ] ;
1259
- getCommandAndParents ( this )
1272
+ this . _getCommandAndAncestors ( )
1260
1273
. reverse ( )
1261
1274
. filter ( cmd => cmd . _lifeCycleHooks [ event ] !== undefined )
1262
1275
. forEach ( hookedCommand => {
@@ -1627,7 +1640,7 @@ Call on top-level command instead`);
1627
1640
*/
1628
1641
optsWithGlobals ( ) {
1629
1642
// globals overwrite locals
1630
- return getCommandAndParents ( this ) . reduce (
1643
+ return this . _getCommandAndAncestors ( ) . reduce (
1631
1644
( combinedOptions , cmd ) => Object . assign ( combinedOptions , cmd . opts ( ) ) ,
1632
1645
{ }
1633
1646
) ;
@@ -2072,7 +2085,7 @@ Call on top-level command instead`);
2072
2085
}
2073
2086
const context = this . _getHelpContext ( contextOptions ) ;
2074
2087
2075
- getCommandAndParents ( this ) . reverse ( ) . forEach ( command => command . emit ( 'beforeAllHelp' , context ) ) ;
2088
+ this . _getCommandAndAncestors ( ) . reverse ( ) . forEach ( command => command . emit ( 'beforeAllHelp' , context ) ) ;
2076
2089
this . emit ( 'beforeHelp' , context ) ;
2077
2090
2078
2091
let helpInformation = this . helpInformation ( context ) ;
@@ -2086,7 +2099,7 @@ Call on top-level command instead`);
2086
2099
2087
2100
this . emit ( this . _helpLongFlag ) ; // deprecated
2088
2101
this . emit ( 'afterHelp' , context ) ;
2089
- getCommandAndParents ( this ) . forEach ( command => command . emit ( 'afterAllHelp' , context ) ) ;
2102
+ this . _getCommandAndAncestors ( ) . forEach ( command => command . emit ( 'afterAllHelp' , context ) ) ;
2090
2103
}
2091
2104
2092
2105
/**
@@ -2229,18 +2242,4 @@ function incrementNodeInspectorPort(args) {
2229
2242
} ) ;
2230
2243
}
2231
2244
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
-
2246
2245
exports . Command = Command ;
0 commit comments