@@ -62,7 +62,6 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
62
62
}
63
63
64
64
const childProcess = child_process . spawn ( cmd , args , spawnOptions ) ;
65
- // @ts -ignore
66
65
childProcess . stdout ! . on ( 'data' , ( data : Buffer ) => {
67
66
stdout += data . toString ( 'utf-8' ) ;
68
67
if ( options . silent ) {
@@ -74,7 +73,7 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
74
73
. filter ( ( line ) => line !== '' )
75
74
. forEach ( ( line ) => console . log ( ' ' + line ) ) ;
76
75
} ) ;
77
- // @ts -ignore
76
+
78
77
childProcess . stderr ! . on ( 'data' , ( data : Buffer ) => {
79
78
stderr += data . toString ( 'utf-8' ) ;
80
79
if ( options . silent ) {
@@ -121,14 +120,14 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
121
120
122
121
if ( options . waitForMatch ) {
123
122
const match = options . waitForMatch ;
124
- // @ts -ignore
123
+
125
124
childProcess . stdout ! . on ( 'data' , ( data : Buffer ) => {
126
125
if ( data . toString ( ) . match ( match ) ) {
127
126
resolve ( { stdout, stderr } ) ;
128
127
matched = true ;
129
128
}
130
129
} ) ;
131
- // @ts -ignore
130
+
132
131
childProcess . stderr ! . on ( 'data' , ( data : Buffer ) => {
133
132
if ( data . toString ( ) . match ( match ) ) {
134
133
resolve ( { stdout, stderr } ) ;
@@ -176,14 +175,14 @@ export function waitForAnyProcessOutputToMatch(
176
175
new Promise ( ( resolve ) => {
177
176
let stdout = '' ;
178
177
let stderr = '' ;
179
- // @ts -ignore
178
+
180
179
childProcess . stdout ! . on ( 'data' , ( data : Buffer ) => {
181
180
stdout += data . toString ( ) ;
182
181
if ( data . toString ( ) . match ( match ) ) {
183
182
resolve ( { stdout, stderr } ) ;
184
183
}
185
184
} ) ;
186
- // @ts -ignore
185
+
187
186
childProcess . stderr ! . on ( 'data' , ( data : Buffer ) => {
188
187
stderr += data . toString ( ) ;
189
188
if ( data . toString ( ) . match ( match ) ) {
@@ -197,22 +196,31 @@ export function waitForAnyProcessOutputToMatch(
197
196
}
198
197
199
198
export async function killAllProcesses ( signal = 'SIGTERM' ) : Promise < void > {
200
- await Promise . all (
201
- _processes . map (
202
- ( { pid } ) =>
203
- new Promise < void > ( ( resolve , reject ) => {
204
- treeKill ( pid , signal , ( err ) => {
205
- if ( err ) {
206
- reject ( err ) ;
207
- } else {
208
- resolve ( ) ;
209
- }
210
- } ) ;
211
- } ) ,
212
- ) ,
213
- ) ;
199
+ const processesToKill : Promise < void > [ ] = [ ] ;
200
+
201
+ while ( _processes . length ) {
202
+ const childProc = _processes . pop ( ) ;
203
+ if ( ! childProc ) {
204
+ continue ;
205
+ }
206
+
207
+ processesToKill . push (
208
+ new Promise < void > ( ( resolve , reject ) => {
209
+ treeKill ( childProc . pid , signal , ( err ) => {
210
+ if ( err && ! err . message . includes ( 'not found' ) ) {
211
+ // Ignore process not found errors.
212
+ // This is due to a race condition with the `waitForMatch` logic.
213
+ // where promises are resolved on matches and not when the process terminates.
214
+ reject ( err ) ;
215
+ } else {
216
+ resolve ( ) ;
217
+ }
218
+ } ) ;
219
+ } ) ,
220
+ ) ;
221
+ }
214
222
215
- _processes = [ ] ;
223
+ await Promise . all ( processesToKill ) ;
216
224
}
217
225
218
226
export function exec ( cmd : string , ...args : string [ ] ) {
0 commit comments