Refactor execa promise to use `async/await` (#653)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent cff6847 · Browse files · 3 files·+22 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+22 added−18 removed
- tests:+11 added−3 removed
- other:+11 added−15 removed
Provenance
- Commit
d69fd23- Tree
bc101f8- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
cff6847
3 files changed+22 added−18 removed
modified:lib/promise.js+6 added-15 removed
12345613 unmodified lines2021222322232425262728293031323334353624252627import {once} from 'node:events';
// eslint-disable-next-line unicorn/prefer-top-level-awaitconst nativePromisePrototype = (async () => {})().constructor.prototype;
const descriptors = ['then', 'catch', 'finally'].map(property => [13 unmodified lines }};
// Use promises instead of `child_process` eventsexport const getSpawnedPromise = spawned => new Promise((resolve, reject) => { spawned.on('exit', (exitCode, signal) => { resolve({exitCode, signal}); });
spawned.on('error', error => { reject(error); });
if (spawned.stdin) { spawned.stdin.on('error', error => { reject(error); }); }});export const getSpawnedPromise = async spawned => { const [exitCode, signal] = await once(spawned, 'exit'); return {exitCode, signal};};modified:lib/stream.js+5 added
76 unmodified lines77787980818283848586878825 unmodified lines11411511611711811912012112210 unmodified lines76 unmodified lines ? finished(value) : Promise.race([processDone, throwOnStreamError(value)]);};
const throwIfStreamError = (stream, processDone) => stream === null ? undefined : Promise.race([processDone, throwOnStreamError(stream)]);
const throwOnStreamError = async stream => { const [error] = await once(stream, 'error'); throw error;};25 unmodified lines processDone, stdoutPromise, stderrPromise, allPromise, throwIfStreamError(spawned.stdin, processDone), ...stdioStreams.map(stdioStream => waitForStreamEnd(stdioStream, processDone)), ]); } catch (error) { spawned.kill();10 unmodified linesmodified:test/test.js+11 added-3 removed
1 import path from 'node:path';2 import process from 'node:process';3+import {setTimeout} from 'node:timers/promises';4 import {fileURLToPath, pathToFileURL} from 'node:url';5 import test from 'ava';6 import isRunning from 'is-running';7 import getNode from 'get-node';116 unmodified lines124 125 test.serial('execPath option', testExecPath, identity);126 test.serial('execPath option can be a file URL', testExecPath, pathToFileURL);127 127-test('stdin errors are handled', async t => {128- const subprocess = execa('noop.js');128+const emitStdinError = async subprocess => {129+ await setTimeout(0);130 subprocess.stdin.emit('error', new Error('test'));130- await t.throwsAsync(subprocess, {message: /test/});131+};132+133+test('stdin errors are handled', async t => {134+ const subprocess = execa('forever.js');135+ await Promise.all([136+ t.throwsAsync(subprocess, {message: /test/}),137+ emitStdinError(subprocess),138+ ]);139 });140 141 test('child process errors are handled', async t => {142 const subprocess = execa('noop.js');171 unmodified lines