@@ -17,7 +17,7 @@ export interface IExecOptions extends IExecSyncOptions {
17
17
18
18
/** optional. defaults to failing on non zero. ignore will not fail leaving it up to the caller */
19
19
ignoreReturnCode : boolean ;
20
- } ;
20
+ }
21
21
22
22
/**
23
23
* Interface for execSync options
@@ -38,7 +38,7 @@ export interface IExecSyncOptions {
38
38
39
39
/** optional. foo.whether to skip quoting/escaping arguments if needed. defaults to false. */
40
40
windowsVerbatimArguments : boolean ;
41
- } ;
41
+ }
42
42
43
43
/**
44
44
* Interface for exec results returned from synchronous exec functions
@@ -479,7 +479,8 @@ export class ToolRunner extends events.EventEmitter {
479
479
return result ;
480
480
}
481
481
482
- private _getSpawnOptions ( options : IExecOptions ) : child . SpawnOptions {
482
+ private _getSpawnOptions ( options ?: IExecOptions ) : child . SpawnOptions {
483
+ options = options || < IExecOptions > { } ;
483
484
let result = < child . SpawnOptions > { } ;
484
485
result . cwd = options . cwd ;
485
486
result . env = options . env ;
@@ -504,24 +505,29 @@ export class ToolRunner extends events.EventEmitter {
504
505
this . _debug ( ' ' + arg ) ;
505
506
} ) ;
506
507
508
+ // This is a private method, and must be called only when `this.pipeOutputToTool` is set
509
+ if ( ! this . pipeOutputToTool ) {
510
+ throw new Error ( 'You must call pipeExecOutputToTool before calling execWithPiping' ) ;
511
+ }
512
+ const pipeOutputToTool = this . pipeOutputToTool ;
513
+
507
514
let success = true ;
508
- options = this . _cloneExecOptions ( options ) ;
515
+ const optionsNonNull = this . _cloneExecOptions ( options ) ;
509
516
510
- if ( ! options . silent ) {
511
- options . outStream . write ( this . _getCommandString ( options ) + os . EOL ) ;
517
+ if ( ! optionsNonNull . silent ) {
518
+ optionsNonNull . outStream . write ( this . _getCommandString ( optionsNonNull ) + os . EOL ) ;
512
519
}
513
520
514
- let cp ;
515
- let toolPath : string = this . toolPath ;
521
+ let cp : child . ChildProcess ;
522
+ let toolPath : string = pipeOutputToTool . toolPath ;
516
523
let toolPathFirst : string ;
517
524
let successFirst = true ;
518
525
let returnCodeFirst : number ;
519
- let fileStream : fs . WriteStream ;
526
+ let fileStream : fs . WriteStream | null ;
520
527
let waitingEvents : number = 0 ; // number of process or stream events we are waiting on to complete
521
528
let returnCode : number = 0 ;
522
- let error ;
529
+ let error : any ;
523
530
524
- toolPath = this . pipeOutputToTool . toolPath ;
525
531
toolPathFirst = this . toolPath ;
526
532
527
533
// Following node documentation example from this link on how to pipe output of one process to another
@@ -531,18 +537,18 @@ export class ToolRunner extends events.EventEmitter {
531
537
waitingEvents ++ ;
532
538
var cpFirst = child . spawn (
533
539
this . _getSpawnFileName ( ) ,
534
- this . _getSpawnArgs ( options ) ,
535
- this . _getSpawnOptions ( options ) ) ;
536
-
540
+ this . _getSpawnArgs ( optionsNonNull ) ,
541
+ this . _getSpawnOptions ( optionsNonNull ) ) ;
542
+
537
543
waitingEvents ++ ;
538
544
cp = child . spawn (
539
- this . pipeOutputToTool . _getSpawnFileName ( ) ,
540
- this . pipeOutputToTool . _getSpawnArgs ( options ) ,
541
- this . pipeOutputToTool . _getSpawnOptions ( options ) ) ;
545
+ pipeOutputToTool . _getSpawnFileName ( ) ,
546
+ pipeOutputToTool . _getSpawnArgs ( optionsNonNull ) ,
547
+ pipeOutputToTool . _getSpawnOptions ( optionsNonNull ) ) ;
542
548
543
549
fileStream = this . pipeOutputToFile ? fs . createWriteStream ( this . pipeOutputToFile ) : null ;
544
550
if ( fileStream ) {
545
- waitingEvents ++ ;
551
+ waitingEvents ++ ;
546
552
fileStream . on ( 'finish' , ( ) => {
547
553
waitingEvents -- ; //file write is complete
548
554
fileStream = null ;
@@ -554,7 +560,7 @@ export class ToolRunner extends events.EventEmitter {
554
560
}
555
561
}
556
562
} ) ;
557
- fileStream . on ( 'error' , ( err ) => {
563
+ fileStream . on ( 'error' , ( err : Error ) => {
558
564
waitingEvents -- ; //there were errors writing to the file, write is done
559
565
this . _debug ( `Failed to pipe output of ${ toolPathFirst } to file ${ this . pipeOutputToFile } . Error = ${ err } ` ) ;
560
566
fileStream = null ;
@@ -565,7 +571,7 @@ export class ToolRunner extends events.EventEmitter {
565
571
defer . resolve ( returnCode ) ;
566
572
}
567
573
}
568
- } )
574
+ } ) ;
569
575
}
570
576
571
577
//pipe stdout of first tool to stdin of second tool
@@ -584,13 +590,13 @@ export class ToolRunner extends events.EventEmitter {
584
590
if ( fileStream ) {
585
591
fileStream . write ( data ) ;
586
592
}
587
- successFirst = ! options . failOnStdErr ;
588
- if ( ! options . silent ) {
589
- var s = options . failOnStdErr ? options . errStream : options . outStream ;
593
+ successFirst = ! optionsNonNull . failOnStdErr ;
594
+ if ( ! optionsNonNull . silent ) {
595
+ var s = optionsNonNull . failOnStdErr ? optionsNonNull . errStream : optionsNonNull . outStream ;
590
596
s . write ( data ) ;
591
597
}
592
598
} ) ;
593
- cpFirst . on ( 'error' , ( err ) => {
599
+ cpFirst . on ( 'error' , ( err : Error ) => {
594
600
waitingEvents -- ; //first process is complete with errors
595
601
if ( fileStream ) {
596
602
fileStream . end ( ) ;
@@ -601,9 +607,9 @@ export class ToolRunner extends events.EventEmitter {
601
607
defer . reject ( error ) ;
602
608
}
603
609
} ) ;
604
- cpFirst . on ( 'close' , ( code , signal ) => {
610
+ cpFirst . on ( 'close' , ( code : number , signal : any ) => {
605
611
waitingEvents -- ; //first process is complete
606
- if ( code != 0 && ! options . ignoreReturnCode ) {
612
+ if ( code != 0 && ! optionsNonNull . ignoreReturnCode ) {
607
613
successFirst = false ;
608
614
returnCodeFirst = code ;
609
615
returnCode = returnCodeFirst ;
@@ -626,8 +632,8 @@ export class ToolRunner extends events.EventEmitter {
626
632
cp . stdout . on ( 'data' , ( data : Buffer ) => {
627
633
this . emit ( 'stdout' , data ) ;
628
634
629
- if ( ! options . silent ) {
630
- options . outStream . write ( data ) ;
635
+ if ( ! optionsNonNull . silent ) {
636
+ optionsNonNull . outStream . write ( data ) ;
631
637
}
632
638
633
639
this . _processLineBuffer ( data , stdbuffer , ( line : string ) => {
@@ -639,9 +645,9 @@ export class ToolRunner extends events.EventEmitter {
639
645
cp . stderr . on ( 'data' , ( data : Buffer ) => {
640
646
this . emit ( 'stderr' , data ) ;
641
647
642
- success = ! options . failOnStdErr ;
643
- if ( ! options . silent ) {
644
- var s = options . failOnStdErr ? options . errStream : options . outStream ;
648
+ success = ! optionsNonNull . failOnStdErr ;
649
+ if ( ! optionsNonNull . silent ) {
650
+ var s = optionsNonNull . failOnStdErr ? optionsNonNull . errStream : optionsNonNull . outStream ;
645
651
s . write ( data ) ;
646
652
}
647
653
@@ -650,15 +656,15 @@ export class ToolRunner extends events.EventEmitter {
650
656
} ) ;
651
657
} ) ;
652
658
653
- cp . on ( 'error' , ( err ) => {
659
+ cp . on ( 'error' , ( err : Error ) => {
654
660
waitingEvents -- ; //process is done with errors
655
661
error = new Error ( toolPath + ' failed. ' + err . message ) ;
656
662
if ( waitingEvents == 0 ) {
657
663
defer . reject ( error ) ;
658
664
}
659
665
} ) ;
660
666
661
- cp . on ( 'close' , ( code , signal ) => {
667
+ cp . on ( 'close' , ( code : number , signal : any ) => {
662
668
waitingEvents -- ; //process is complete
663
669
this . _debug ( 'rc:' + code ) ;
664
670
returnCode = code ;
@@ -671,12 +677,12 @@ export class ToolRunner extends events.EventEmitter {
671
677
this . emit ( 'errline' , errbuffer ) ;
672
678
}
673
679
674
- if ( code != 0 && ! options . ignoreReturnCode ) {
680
+ if ( code != 0 && ! optionsNonNull . ignoreReturnCode ) {
675
681
success = false ;
676
682
}
677
683
678
684
this . _debug ( 'success:' + success ) ;
679
-
685
+
680
686
if ( ! successFirst ) { //in the case output is piped to another tool, check exit code of both tools
681
687
error = new Error ( toolPathFirst + ' failed with return code: ' + returnCodeFirst ) ;
682
688
} else if ( ! success ) {
@@ -788,26 +794,22 @@ export class ToolRunner extends events.EventEmitter {
788
794
this . _debug ( ' ' + arg ) ;
789
795
} ) ;
790
796
791
- options = this . _cloneExecOptions ( options ) ;
792
- if ( ! options . silent ) {
793
- options . outStream . write ( this . _getCommandString ( options ) + os . EOL ) ;
797
+ const optionsNonNull = this . _cloneExecOptions ( options ) ;
798
+ if ( ! optionsNonNull . silent ) {
799
+ optionsNonNull . outStream . write ( this . _getCommandString ( optionsNonNull ) + os . EOL ) ;
794
800
}
795
801
796
- let state = new ExecState ( options , this . toolPath ) ;
797
- state . on ( 'debug' , ( message ) => {
802
+ let state = new ExecState ( optionsNonNull , this . toolPath ) ;
803
+ state . on ( 'debug' , ( message : string ) => {
798
804
this . _debug ( message ) ;
799
805
} ) ;
800
806
801
- let cp = child . spawn ( this . _getSpawnFileName ( ) , this . _getSpawnArgs ( options ) , this . _getSpawnOptions ( options ) ) ;
807
+ let cp = child . spawn ( this . _getSpawnFileName ( ) , this . _getSpawnArgs ( optionsNonNull ) , this . _getSpawnOptions ( options ) ) ;
802
808
803
809
var stdbuffer : string = '' ;
804
810
cp . stdout . on ( 'data' , ( data : Buffer ) => {
805
811
this . emit ( 'stdout' , data ) ;
806
812
807
- if ( ! clonedOptions . silent ) {
808
- clonedOptions . outStream . write ( data ) ;
809
- }
810
-
811
813
this . _processLineBuffer ( data , stdbuffer , ( line : string ) => {
812
814
this . emit ( 'stdline' , line ) ;
813
815
} ) ;
@@ -818,8 +820,8 @@ export class ToolRunner extends events.EventEmitter {
818
820
state . processStderr = true ;
819
821
this . emit ( 'stderr' , data ) ;
820
822
821
- if ( ! options . silent ) {
822
- var s = options . failOnStdErr ? options . errStream : options . outStream ;
823
+ if ( ! optionsNonNull . silent ) {
824
+ var s = optionsNonNull . failOnStdErr ? optionsNonNull . errStream : optionsNonNull . outStream ;
823
825
s . write ( data ) ;
824
826
}
825
827
@@ -828,29 +830,29 @@ export class ToolRunner extends events.EventEmitter {
828
830
} ) ;
829
831
} ) ;
830
832
831
- cp . on ( 'error' , ( err ) => {
833
+ cp . on ( 'error' , ( err : Error ) => {
832
834
state . processError = err . message ;
833
835
state . processExited = true ;
834
836
state . processClosed = true ;
835
837
state . CheckComplete ( ) ;
836
838
} ) ;
837
839
838
- cp . on ( 'exit' , ( code , signal ) => {
840
+ cp . on ( 'exit' , ( code : number , signal : any ) => {
839
841
state . processExitCode = code ;
840
842
state . processExited = true ;
841
843
this . _debug ( `Exit code ${ code } received from tool '${ this . toolPath } '` ) ;
842
844
state . CheckComplete ( )
843
845
} ) ;
844
846
845
- cp . on ( 'close' , ( code , signal ) => {
847
+ cp . on ( 'close' , ( code : number , signal : any ) => {
846
848
state . processExitCode = code ;
847
849
state . processExited = true ;
848
850
state . processClosed = true ;
849
851
this . _debug ( `STDIO streams have closed for tool '${ this . toolPath } '` )
850
852
state . CheckComplete ( ) ;
851
853
} ) ;
852
854
853
- state . on ( 'done' , ( error , exitCode ) => {
855
+ state . on ( 'done' , ( error : Error , exitCode : number ) => {
854
856
if ( stdbuffer . length > 0 ) {
855
857
this . emit ( 'stdline' , stdbuffer ) ;
856
858
}
@@ -869,7 +871,7 @@ export class ToolRunner extends events.EventEmitter {
869
871
}
870
872
} ) ;
871
873
872
- return < Q . Promise < number > > defer . promise ;
874
+ return defer . promise ;
873
875
}
874
876
875
877
/**
@@ -883,8 +885,6 @@ export class ToolRunner extends events.EventEmitter {
883
885
* @returns IExecSyncResult
884
886
*/
885
887
public execSync ( options ?: IExecSyncOptions ) : IExecSyncResult {
886
- var defer = Q . defer ( ) ;
887
-
888
888
this . _debug ( 'exec tool: ' + this . toolPath ) ;
889
889
this . _debug ( 'arguments:' ) ;
890
890
this . args . forEach ( ( arg ) => {
@@ -942,7 +942,7 @@ class ExecState extends events.EventEmitter {
942
942
private delay = 10000 ; // 10 seconds
943
943
private done : boolean ;
944
944
private options : IExecOptions ;
945
- private timeout ;
945
+ private timeout : NodeJS . Timer | null = null ;
946
946
private toolPath : string ;
947
947
948
948
public CheckComplete ( ) : void {
@@ -958,13 +958,13 @@ class ExecState extends events.EventEmitter {
958
958
}
959
959
}
960
960
961
- private _debug ( message ) : void {
961
+ private _debug ( message : any ) : void {
962
962
this . emit ( 'debug' , message ) ;
963
963
}
964
964
965
965
private _setResult ( ) : void {
966
966
// determine whether there is an error
967
- let error : Error ;
967
+ let error : Error | undefined ;
968
968
if ( this . processExited ) {
969
969
if ( this . processError ) {
970
970
error = new Error ( im . _loc ( 'LIB_ProcessError' , this . toolPath , this . processError ) ) ;
0 commit comments