@@ -3,14 +3,15 @@ import * as Bromise from 'bluebird'
33
44import originalSplit = require( 'split' )
55
6- import { Result , ResultSpecialValues } from './enums' ;
6+ import { Result , ResultSpecialValues } from './enums'
77import { defer } from './utils'
88
99export interface CmdOptions {
1010 rejectOnNonZeroExit : boolean
1111 silent ?: boolean
1212 collectLogs : boolean
1313 prefixer ?: ( basePath : string , pkg : string , line : string ) => string
14+ pathRewriter ?: ( basePath : string , pkg : string , line : string ) => string
1415 doneCriteria ?: string
1516 path : string
1617}
@@ -47,7 +48,7 @@ export class CmdProcess {
4748 }
4849
4950 get result ( ) {
50- return Bromise . race ( [ this . _exitCode . promise , this . _cancelled . promise ] ) ;
51+ return Bromise . race ( [ this . _exitCode . promise , this . _cancelled . promise ] )
5152 }
5253
5354 /**
@@ -82,14 +83,14 @@ export class CmdProcess {
8283 this . exitCode . then ( code => {
8384 if ( code > 0 ) {
8485 const msg = '`' + this . cmdString + '` failed with exit code ' + code
85- if ( ! this . opts . silent ) console . error ( this . autoPrefix ( msg ) )
86+ if ( ! this . opts . silent ) console . error ( this . autoAugmentLine ( msg ) )
8687 if ( this . opts . rejectOnNonZeroExit ) return this . _finished . reject ( new Error ( msg ) )
8788 }
8889 this . _finished . resolve ( )
8990 } )
9091
9192 // ignore if unhandled
92- this . _finished . promise . catch ( ( ) => { } )
93+ this . _finished . promise . catch ( ( ) => { } )
9394 }
9495
9596 stop ( ) {
@@ -98,13 +99,25 @@ export class CmdProcess {
9899 this . cp . removeAllListeners ( 'exit' )
99100 this . cp . kill ( 'SIGINT' )
100101 }
101- this . _cancelled . resolve ( ResultSpecialValues . Cancelled ) ;
102+ this . _cancelled . resolve ( ResultSpecialValues . Cancelled )
102103 }
103104
104105 private autoPrefix ( line : string ) {
105106 return this . opts . prefixer ? this . opts . prefixer ( this . opts . path , this . pkgName , line ) : line
106107 }
107108
109+ private autoPathRewrite ( line : string ) {
110+ return this . opts . pathRewriter
111+ ? this . opts . pathRewriter ( this . opts . path , this . pkgName , line )
112+ : line
113+ }
114+
115+ private autoAugmentLine ( line : string ) {
116+ line = this . autoPathRewrite ( line )
117+ line = this . autoPrefix ( line )
118+ return line
119+ }
120+
108121 private _start ( cmd : string [ ] ) {
109122 let sh : string
110123 let args : string [ ]
@@ -137,21 +150,21 @@ export class CmdProcess {
137150 if ( this . cp . stdout )
138151 this . cp . stdout . pipe ( split ( ) ) . on ( 'data' , ( line : string ) => {
139152 if ( this . opts . collectLogs ) stdOutBuffer . push ( line )
140- else console . log ( this . autoPrefix ( line ) )
153+ else console . log ( this . autoAugmentLine ( line ) )
141154 if ( this . doneCriteria && this . doneCriteria . test ( line ) ) this . _finished . resolve ( )
142155 } )
143156 if ( this . cp . stderr )
144157 this . cp . stderr . pipe ( split ( ) ) . on ( 'data' , ( line : string ) => {
145158 if ( this . opts . collectLogs ) stdErrBuffer . push ( line )
146- else console . error ( this . autoPrefix ( line ) )
159+ else console . error ( this . autoAugmentLine ( line ) )
147160 if ( this . doneCriteria && this . doneCriteria . test ( line ) ) this . _finished . resolve ( )
148161 } )
149162 if ( this . opts . collectLogs )
150163 this . _closed . promise . then ( ( ) => {
151164 if ( stdOutBuffer . length )
152- console . log ( stdOutBuffer . map ( line => this . autoPrefix ( line ) ) . join ( '\n' ) )
165+ console . log ( stdOutBuffer . map ( line => this . autoAugmentLine ( line ) ) . join ( '\n' ) )
153166 if ( stdErrBuffer . length )
154- console . error ( stdErrBuffer . map ( line => this . autoPrefix ( line ) ) . join ( '\n' ) )
167+ console . error ( stdErrBuffer . map ( line => this . autoAugmentLine ( line ) ) . join ( '\n' ) )
155168 } )
156169 }
157170}
0 commit comments