UNPKG

785 BJavaScriptView Raw
1// @ts-check
2
3import { ChildProcess } from "child_process";
4
5/**
6 * Promisifies a Node.js child process.
7 * @param {ChildProcess} childProcess Node.js child process.
8 * @returns {Promise<{
9 * exitCode: number | null,
10 * signal: NodeJS.Signals | null
11 * }>} Resolves the exit code if the child exited on its own, or the signal by
12 * which the child process was terminated.
13 */
14export default async function childProcessPromise(childProcess) {
15 if (!(childProcess instanceof ChildProcess))
16 throw new TypeError(
17 "Argument 1 `childProcess` must be a `ChildProcess` instance."
18 );
19
20 return new Promise((resolve, reject) => {
21 childProcess.once("error", reject);
22 childProcess.once("close", (exitCode, signal) =>
23 resolve({ exitCode, signal })
24 );
25 });
26}