Skip to content

Commit a14f971

Browse files
committed
feat: export kill helper from core
1 parent fac8630 commit a14f971

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/core.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export interface Options {
6767
spawn: typeof spawn
6868
spawnSync: typeof spawnSync
6969
log: typeof log
70+
kill: typeof kill
7071
}
7172

7273
const storage = new AsyncLocalStorage<Options>()
@@ -95,6 +96,7 @@ export const defaults: Options = {
9596
spawn,
9697
spawnSync,
9798
log,
99+
kill,
98100
}
99101
const isWin = process.platform == 'win32'
100102
try {
@@ -412,15 +414,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
412414
throw new Error('Trying to kill a process without creating one.')
413415
if (!this.child.pid) throw new Error('The process pid is undefined.')
414416

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) {}
417+
return $.kill(this.child.pid, signal)
424418
}
425419

426420
stdio(stdin: IO, stdout: IO = 'pipe', stderr: IO = 'pipe'): ProcessPromise {
@@ -573,6 +567,18 @@ export function cd(dir: string | ProcessOutput) {
573567
$[processCwd] = process.cwd()
574568
}
575569

570+
export async function kill(pid: number, signal?: string) {
571+
let children = await ps.tree({ pid, recursive: true })
572+
for (const p of children) {
573+
try {
574+
process.kill(+p.pid, signal)
575+
} catch (e) {}
576+
}
577+
try {
578+
process.kill(-pid, signal)
579+
} catch (e) {}
580+
}
581+
576582
export type LogEntry =
577583
| {
578584
kind: 'cmd'

src/util.ts

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { promisify } from 'node:util'
1615
import { chalk } from './vendor.js'
1716

1817
export function noop() {}

0 commit comments

Comments
 (0)