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