From aa2cba8e0379dcd27d5017507ba4c0d3165cbe8d Mon Sep 17 00:00:00 2001 From: Anton Frattaroli Date: Fri, 12 Jul 2024 17:07:48 -0400 Subject: [PATCH] feat: add watchdog pid as cleanup metadata arg PR-URL: https://github.com/tapjs/foreground-child/pull/59 Credit: @frattaro Close: #59 Reviewed-by: @isaacs --- CHANGELOG.md | 5 +++++ src/index.ts | 10 ++++++++-- test/normalize-fg-args.ts | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d284a9..649f270 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changes +## v3.3 + +- Include `watchdogPid` as a metadata argument to cleanup + function + ## v3.2 - Export `watchdog` and `proxySignals` functionality diff --git a/src/index.ts b/src/index.ts index 9e84fed..d807126 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import { Serializable, spawn as nodeSpawn, SpawnOptions, + ChildProcess, } from 'child_process' import crossSpawn from 'cross-spawn' import { onExit } from 'signal-exit' @@ -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 @@ -159,7 +163,7 @@ export function foregroundChild( const removeOnExit = onExit(childHangup) proxySignals(child) - watchdog(child) + const dog = watchdog(child) let done = false child.on('close', async (code, signal) => { @@ -167,7 +171,9 @@ export function foregroundChild( 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() diff --git a/test/normalize-fg-args.ts b/test/normalize-fg-args.ts index aee0280..698ad81 100644 --- a/test/normalize-fg-args.ts +++ b/test/normalize-fg-args.ts @@ -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])