Skip to content

Commit

Permalink
feat(command): add .getGlobalParent() method
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Jun 24, 2020
1 parent c30e474 commit a1d61c9
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/command/lib/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
// protected name: string = location.pathname.split( '/' ).pop() as string;
protected _name: string = 'COMMAND';
protected _parent?: BaseCommand;
protected _globalParent?: BaseCommand;
protected ver: string = '0.0.0';
protected desc: IDescription = '';
protected fn: IAction<O, A> | undefined;
Expand Down Expand Up @@ -544,6 +545,7 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
const subCommand = this.rawArgs.length > 0 && this.getCommand( this.rawArgs[ 0 ], true );

if ( subCommand ) {
subCommand._globalParent = this;
return await subCommand.parse( this.rawArgs.slice( 1 ), dry );
}

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

cmd._globalParent = this;

try {
await cmd.execute( options, ...args );
} catch ( e ) {
Expand Down Expand Up @@ -821,12 +825,21 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
}

/**
* Get parent command name.
* Get parent command.
*/
public getParent(): BaseCommand | undefined {
return this._parent;
}

/**
* Get parent command from global executed command.
* Be sure, to call this method only inside an action handler. Unless this or any child command was executed,
* this method returns always undefined.
*/
public getGlobalParent(): BaseCommand | undefined {
return this._globalParent;
}

/**
* Get main command.
*/
Expand Down

0 comments on commit a1d61c9

Please sign in to comment.