-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchildProcessPromise.mjs
More file actions
26 lines (23 loc) · 790 Bytes
/
childProcessPromise.mjs
File metadata and controls
26 lines (23 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// @ts-check
import { ChildProcess } from "node:child_process";
/**
* Promisifies a Node.js child process.
* @param {ChildProcess} childProcess Node.js child process.
* @returns {Promise<{
* exitCode: number | null,
* signal: NodeJS.Signals | null
* }>} Resolves the exit code if the child exited on its own, or the signal by
* which the child process was terminated.
*/
export default async function childProcessPromise(childProcess) {
if (!(childProcess instanceof ChildProcess))
throw new TypeError(
"Argument 1 `childProcess` must be a `ChildProcess` instance."
);
return new Promise((resolve, reject) => {
childProcess.once("error", reject);
childProcess.once("close", (exitCode, signal) =>
resolve({ exitCode, signal })
);
});
}