@@ -67,6 +67,7 @@ export interface Options {
67
67
spawn : typeof spawn
68
68
spawnSync : typeof spawnSync
69
69
log : typeof log
70
+ kill : typeof kill
70
71
}
71
72
72
73
const storage = new AsyncLocalStorage < Options > ( )
@@ -95,6 +96,7 @@ export const defaults: Options = {
95
96
spawn,
96
97
spawnSync,
97
98
log,
99
+ kill,
98
100
}
99
101
const isWin = process . platform == 'win32'
100
102
try {
@@ -173,13 +175,6 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
173
175
}
174
176
)
175
177
176
- function substitute ( arg : ProcessPromise | any ) {
177
- if ( arg ?. stdout ) {
178
- return arg . stdout . replace ( / \n $ / , '' )
179
- }
180
- return `${ arg } `
181
- }
182
-
183
178
type Resolve = ( out : ProcessOutput ) => void
184
179
type IO = StdioPipe | StdioNull
185
180
@@ -412,15 +407,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
412
407
throw new Error ( 'Trying to kill a process without creating one.' )
413
408
if ( ! this . child . pid ) throw new Error ( 'The process pid is undefined.' )
414
409
415
- let children = await ps . tree ( { pid : this . child . pid , recursive : true } )
416
- for ( const p of children ) {
417
- try {
418
- process . kill ( + p . pid , signal )
419
- } catch ( e ) { }
420
- }
421
- try {
422
- process . kill ( - this . child . pid , signal )
423
- } catch ( e ) { }
410
+ return $ . kill ( this . child . pid , signal )
424
411
}
425
412
426
413
stdio ( stdin : IO , stdout : IO = 'pipe' , stderr : IO = 'pipe' ) : ProcessPromise {
@@ -573,6 +560,18 @@ export function cd(dir: string | ProcessOutput) {
573
560
$ [ processCwd ] = process . cwd ( )
574
561
}
575
562
563
+ export async function kill ( pid : number , signal ?: string ) {
564
+ let children = await ps . tree ( { pid, recursive : true } )
565
+ for ( const p of children ) {
566
+ try {
567
+ process . kill ( + p . pid , signal )
568
+ } catch ( e ) { }
569
+ }
570
+ try {
571
+ process . kill ( - pid , signal )
572
+ } catch ( e ) { }
573
+ }
574
+
576
575
export type LogEntry =
577
576
| {
578
577
kind : 'cmd'
0 commit comments