@@ -13,6 +13,16 @@ import { CommandOptionInfo } from './CommandOptionInfo';
1313import Utils from '../Utils' ;
1414const packageJSON = require ( '../../package.json' ) ;
1515
16+ export interface CommandOutput {
17+ stdout : string ;
18+ stderr : string ;
19+ }
20+
21+ export interface CommandErrorWithOutput {
22+ error : CommandError ;
23+ stderr : string ;
24+ }
25+
1626export class Cli {
1727 public commands : CommandInfo [ ] = [ ] ;
1828 /**
@@ -163,8 +173,17 @@ export class Cli {
163173 logger . logToStderr ( `Executing command ${ command . name } with options ${ JSON . stringify ( args ) } ` ) ;
164174 }
165175
176+ // store the current command name, if any and set the name to the name of
177+ // the command to execute
178+ const cli = Cli . getInstance ( ) ;
179+ const parentCommandName : string | undefined = cli . currentCommandName ;
180+ cli . currentCommandName = command . getCommandName ( ) ;
181+
166182 command
167183 . action ( logger , args as any , ( err : any ) : void => {
184+ // restore the original command name
185+ cli . currentCommandName = parentCommandName ;
186+
168187 if ( err ) {
169188 return reject ( err ) ;
170189 }
@@ -174,31 +193,47 @@ export class Cli {
174193 } ) ;
175194 }
176195
177- public static executeCommandWithOutput ( command : Command , args : { options : minimist . ParsedArgs } ) : Promise < string > {
178- return new Promise ( ( resolve : ( result : string ) => void , reject : ( error : any ) => void ) : void => {
196+ public static executeCommandWithOutput ( command : Command , args : { options : minimist . ParsedArgs } ) : Promise < CommandOutput > {
197+ return new Promise ( ( resolve : ( result : CommandOutput ) => void , reject : ( error : any ) => void ) : void => {
179198 const log : string [ ] = [ ] ;
199+ const logErr : string [ ] = [ ] ;
180200 const logger : Logger = {
181201 log : ( message : any ) : void => {
182- log . push ( message ) ;
202+ log . push ( Cli . formatOutput ( message , args . options ) ) ;
183203 } ,
184204 logRaw : ( message : any ) : void => {
185- log . push ( message ) ;
205+ log . push ( Cli . formatOutput ( message , args . options ) ) ;
186206 } ,
187207 logToStderr : ( message : any ) : void => {
188- log . push ( message ) ;
208+ logErr . push ( message ) ;
189209 }
190210 } ;
191211
192212 if ( args . options . debug ) {
193213 Cli . log ( `Executing command ${ command . name } with options ${ JSON . stringify ( args ) } ` ) ;
194214 }
195215
216+ // store the current command name, if any and set the name to the name of
217+ // the command to execute
218+ const cli = Cli . getInstance ( ) ;
219+ const parentCommandName : string | undefined = cli . currentCommandName ;
220+ cli . currentCommandName = command . getCommandName ( ) ;
221+
196222 command . action ( logger , args as any , ( err : any ) : void => {
223+ // restore the original command name
224+ cli . currentCommandName = parentCommandName ;
225+
197226 if ( err ) {
198- return reject ( err ) ;
227+ return reject ( {
228+ error : err ,
229+ stderr : logErr . join ( os . EOL )
230+ } ) ;
199231 }
200232
201- resolve ( log . join ( ) ) ;
233+ resolve ( {
234+ stdout : log . join ( os . EOL ) ,
235+ stderr : logErr . join ( os . EOL )
236+ } ) ;
202237 } ) ;
203238 } ) ;
204239 }
0 commit comments