@@ -29,8 +29,6 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
29
29
// Create a separate instance to prevent unintended global changes to the color configuration
30
30
const colors = ansiColors . create ( ) ;
31
31
32
- let stdout = '' ;
33
- let stderr = '' ;
34
32
const cwd = options . cwd ?? process . cwd ( ) ;
35
33
const env = options . env ?? process . env ;
36
34
console . log (
@@ -69,48 +67,65 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
69
67
}
70
68
71
69
const childProcess = child_process . spawn ( cmd , args , spawnOptions ) ;
72
- childProcess . stdout ! . on ( 'data' , ( data : Buffer ) => {
73
- stdout += data . toString ( 'utf-8' ) ;
74
- if ( options . silent ) {
75
- return ;
76
- }
77
- data
78
- . toString ( 'utf-8' )
79
- . split ( / [ \n \r ] + / )
80
- . filter ( ( line ) => line !== '' )
81
- . forEach ( ( line ) => console . log ( ' ' + line ) ) ;
82
- } ) ;
83
-
84
- childProcess . stderr ! . on ( 'data' , ( data : Buffer ) => {
85
- stderr += data . toString ( 'utf-8' ) ;
86
- if ( options . silent ) {
87
- return ;
88
- }
89
- data
90
- . toString ( 'utf-8' )
91
- . split ( / [ \n \r ] + / )
92
- . filter ( ( line ) => line !== '' )
93
- . forEach ( ( line ) => console . error ( colors . yellow ( ' ' + line ) ) ) ;
94
- } ) ;
95
70
96
71
_processes . push ( childProcess ) ;
97
72
98
73
// Create the error here so the stack shows who called this function.
99
74
const error = new Error ( ) ;
100
75
101
- // Return log info about the current process status
102
- function envDump ( ) {
103
- return [
104
- `ENV:${ JSON . stringify ( spawnOptions . env , null , 2 ) } ` ,
105
- `STDOUT:\n${ stdout } ` ,
106
- `STDERR:\n${ stderr } ` ,
107
- ] . join ( '\n\n' ) ;
108
- }
109
-
110
76
return new Promise < ProcessOutput > ( ( resolve , reject ) => {
77
+ let stdout = '' ;
78
+ let stderr = '' ;
111
79
let matched = false ;
112
80
113
- childProcess . on ( 'exit' , ( code : number ) => {
81
+ // Return log info about the current process status
82
+ function envDump ( ) {
83
+ return [
84
+ `ENV:${ JSON . stringify ( spawnOptions . env , null , 2 ) } ` ,
85
+ `STDOUT:\n${ stdout } ` ,
86
+ `STDERR:\n${ stderr } ` ,
87
+ ] . join ( '\n\n' ) ;
88
+ }
89
+
90
+ childProcess . stdout ! . on ( 'data' , ( data : Buffer ) => {
91
+ stdout += data . toString ( 'utf-8' ) ;
92
+
93
+ if ( options . waitForMatch && stdout . match ( options . waitForMatch ) ) {
94
+ resolve ( { stdout, stderr } ) ;
95
+ matched = true ;
96
+ }
97
+
98
+ if ( options . silent ) {
99
+ return ;
100
+ }
101
+
102
+ data
103
+ . toString ( 'utf-8' )
104
+ . split ( / [ \n \r ] + / )
105
+ . filter ( ( line ) => line !== '' )
106
+ . forEach ( ( line ) => console . log ( ' ' + line ) ) ;
107
+ } ) ;
108
+
109
+ childProcess . stderr ! . on ( 'data' , ( data : Buffer ) => {
110
+ stderr += data . toString ( 'utf-8' ) ;
111
+
112
+ if ( options . waitForMatch && stderr . match ( options . waitForMatch ) ) {
113
+ resolve ( { stdout, stderr } ) ;
114
+ matched = true ;
115
+ }
116
+
117
+ if ( options . silent ) {
118
+ return ;
119
+ }
120
+
121
+ data
122
+ . toString ( 'utf-8' )
123
+ . split ( / [ \n \r ] + / )
124
+ . filter ( ( line ) => line !== '' )
125
+ . forEach ( ( line ) => console . error ( colors . yellow ( ' ' + line ) ) ) ;
126
+ } ) ;
127
+
128
+ childProcess . on ( 'close' , ( code : number ) => {
114
129
_processes = _processes . filter ( ( p ) => p !== childProcess ) ;
115
130
116
131
if ( options . waitForMatch && ! matched ) {
@@ -134,24 +149,6 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
134
149
reject ( `Process error - "${ cmd } ${ args . join ( ' ' ) } ": ${ err } ...\n\n${ envDump ( ) } \n` ) ;
135
150
} ) ;
136
151
137
- if ( options . waitForMatch ) {
138
- const match = options . waitForMatch ;
139
-
140
- childProcess . stdout ! . on ( 'data' , ( data : Buffer ) => {
141
- if ( data . toString ( ) . match ( match ) ) {
142
- resolve ( { stdout, stderr } ) ;
143
- matched = true ;
144
- }
145
- } ) ;
146
-
147
- childProcess . stderr ! . on ( 'data' , ( data : Buffer ) => {
148
- if ( data . toString ( ) . match ( match ) ) {
149
- resolve ( { stdout, stderr } ) ;
150
- matched = true ;
151
- }
152
- } ) ;
153
- }
154
-
155
152
// Provide input to stdin if given.
156
153
if ( options . stdin ) {
157
154
childProcess . stdin ! . write ( options . stdin ) ;
@@ -192,14 +189,14 @@ export function waitForAnyProcessOutputToMatch(
192
189
193
190
childProcess . stdout ! . on ( 'data' , ( data : Buffer ) => {
194
191
stdout += data . toString ( ) ;
195
- if ( data . toString ( ) . match ( match ) ) {
192
+ if ( stdout . match ( match ) ) {
196
193
resolve ( { stdout, stderr } ) ;
197
194
}
198
195
} ) ;
199
196
200
197
childProcess . stderr ! . on ( 'data' , ( data : Buffer ) => {
201
198
stderr += data . toString ( ) ;
202
- if ( data . toString ( ) . match ( match ) ) {
199
+ if ( stderr . match ( match ) ) {
203
200
resolve ( { stdout, stderr } ) ;
204
201
}
205
202
} ) ;
0 commit comments