Skip to content

Commit

Permalink
feat: add watchdog pid as cleanup metadata arg
Browse files Browse the repository at this point in the history
PR-URL: tapjs#59
Credit: @frattaro
Close: tapjs#59
Reviewed-by: @isaacs
  • Loading branch information
frattaro authored and isaacs committed Aug 7, 2024
1 parent 132a017 commit 2939728
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Serializable,
spawn as nodeSpawn,
SpawnOptions,
ChildProcess,
} from 'child_process'
import crossSpawn from 'cross-spawn'
import { onExit } from 'signal-exit'
Expand Down Expand Up @@ -37,6 +38,9 @@ const spawn = process?.platform === 'win32' ? crossSpawn : nodeSpawn
export type Cleanup = (
code: number | null,
signal: null | NodeJS.Signals,
processInfo: {
watchdogPid: ChildProcess['pid']
},
) =>
| void
| undefined
Expand Down Expand Up @@ -159,15 +163,17 @@ export function foregroundChild(
const removeOnExit = onExit(childHangup)

proxySignals(child)
watchdog(child)
const dog = watchdog(child)

let done = false
child.on('close', async (code, signal) => {
/* c8 ignore start */
if (done) return
/* c8 ignore stop */
done = true
const result = cleanup(code, signal)
const result = cleanup(code, signal, {
watchdogPid: dog.pid,
})
const res = isPromise(result) ? await result : result
removeOnExit()

Expand Down

0 comments on commit 2939728

Please sign in to comment.