Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add watchdog process info as cleanup argument #59

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changes

## v3.3

- Include `watchdogPid` as a metadata argument to cleanup
function

## v3.2

- Export `watchdog` and `proxySignals` functionality
Expand Down
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
2 changes: 1 addition & 1 deletion test/normalize-fg-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ t.plan(cases.length)
for (const c of cases) {
t.test(JSON.stringify(c), t => {
const norm = normalizeFgArgs(c)
norm[3](0, null)
norm[3](0, null, {})
t.equal(norm[0], 'cmd')
if (Array.isArray(c[1])) {
t.equal(norm[1], c[1])
Expand Down
Loading