Skip to content

Commit a1d61c9

Browse files
committed
feat(command): add .getGlobalParent() method
1 parent c30e474 commit a1d61c9

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

packages/command/lib/base-command.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
3232
// protected name: string = location.pathname.split( '/' ).pop() as string;
3333
protected _name: string = 'COMMAND';
3434
protected _parent?: BaseCommand;
35+
protected _globalParent?: BaseCommand;
3536
protected ver: string = '0.0.0';
3637
protected desc: IDescription = '';
3738
protected fn: IAction<O, A> | undefined;
@@ -544,6 +545,7 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
544545
const subCommand = this.rawArgs.length > 0 && this.getCommand( this.rawArgs[ 0 ], true );
545546

546547
if ( subCommand ) {
548+
subCommand._globalParent = this;
547549
return await subCommand.parse( this.rawArgs.slice( 1 ), dry );
548550
}
549551

@@ -612,6 +614,8 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
612614
throw this.error( new Error( `Default command '${ this.defaultCommand }' not found.` ) );
613615
}
614616

617+
cmd._globalParent = this;
618+
615619
try {
616620
await cmd.execute( options, ...args );
617621
} catch ( e ) {
@@ -821,12 +825,21 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
821825
}
822826

823827
/**
824-
* Get parent command name.
828+
* Get parent command.
825829
*/
826830
public getParent(): BaseCommand | undefined {
827831
return this._parent;
828832
}
829833

834+
/**
835+
* Get parent command from global executed command.
836+
* Be sure, to call this method only inside an action handler. Unless this or any child command was executed,
837+
* this method returns always undefined.
838+
*/
839+
public getGlobalParent(): BaseCommand | undefined {
840+
return this._globalParent;
841+
}
842+
830843
/**
831844
* Get main command.
832845
*/

0 commit comments

Comments
 (0)