Skip to content
Myr

async.js

This file

Path

lib/async.js

95 lines · 4.1 KiB · JavaScript

Last changed by

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

History of this file

Take it

Split files (#837)ehmickybefore adoption672c2d7

line 1why, one step back
1import {setMaxListeners} from 'node:events';

Improve spawning errors (#847)ehmickybefore adoption381dee2

line 2why, one step back
2import {spawn} from 'node:child_process';

Improve `verbose` option (#883)ehmickybefore adoption7d0943f

line 3why, one step back
3import {normalizeArguments, handleCommand, handleArguments} from './arguments/options.js';

Improve spawning errors (#847)ehmickybefore adoption381dee2

line 4why, one step back
4import {makeError, makeSuccessResult} from './return/error.js';

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

line 5why, one step back
5import {handleOutput, handleResult} from './return/output.js';

Improve spawning errors (#847)ehmickybefore adoption381dee2

lines 6–7why, one step back
6import {handleEarlyError} from './return/early-error.js';
7import {handleInputAsync, pipeOutputAsync} from './stdio/async.js';

Split files (#837)ehmickybefore adoption672c2d7

lines 8–10why, one step back
8import {spawnedKill} from './exit/kill.js';
9import {cleanupOnExit} from './exit/cleanup.js';10import {pipeToProcess} from './pipe/setup.js';

Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4

line 11why, one step back
11import {PROCESS_OPTIONS} from './pipe/validate.js';

Split files (#837)ehmickybefore adoption672c2d7

lines 12–16why, one step back
12import {makeAllStream} from './stream/all.js';
13import {getSpawnedResult} from './stream/resolve.js';14import {mergePromise} from './promise.js';1516export const execa = (rawFile, rawArgs, rawOptions) => {

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

lines 17–18why, one step back
17 const {file, args, command, escapedCommand, options, stdioStreamsGroups, stdioState} = handleAsyncArguments(rawFile, rawArgs, rawOptions);
18 const {spawned, promise} = spawnProcessAsync({file, args, options, command, escapedCommand, stdioStreamsGroups, stdioState});

Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86

line 19why, one step back
19 spawned.pipe = pipeToProcess.bind(undefined, {source: spawned, sourcePromise: promise, stdioStreamsGroups, destinationOptions: {}});

Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4

lines 20–24why, one step back
20 mergePromise(spawned, promise);
21 PROCESS_OPTIONS.set(spawned, options);22 return spawned;23};24

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

lines 25–26why, one step back
25const handleAsyncArguments = (rawFile, rawArgs, rawOptions) => {
26 [rawFile, rawArgs, rawOptions] = normalizeArguments(rawFile, rawArgs, rawOptions);

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

line 27why, one step back
27 const {command, escapedCommand, verboseInfo} = handleCommand(rawFile, rawArgs, rawOptions);

Improve `verbose` option (#883)ehmickybefore adoption7d0943f

line 28why, one step back
28 const {file, args, options: normalizedOptions} = handleArguments(rawFile, rawArgs, rawOptions);

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

line 29why, one step back
29 const options = handleAsyncOptions(normalizedOptions);

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

lines 30–31why, one step back
30 const {stdioStreamsGroups, stdioState} = handleInputAsync(options, verboseInfo);
31 return {file, args, command, escapedCommand, options, stdioStreamsGroups, stdioState};

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

line 32why, one step back
32};

Split files (#837)ehmickybefore adoption672c2d7

line 33why, one step back
33

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

line 34why, one step back
34// Prevent passing the `timeout` option directly to `child_process.spawn()`

Rename `signal` option to `cancelSignal` (#880)ehmickybefore adoption231beaf

lines 35–41why, one step back
35const handleAsyncOptions = ({timeout, signal, cancelSignal, ...options}) => {
36 if (signal !== undefined) {37 throw new TypeError('The "signal" option has been renamed to "cancelSignal" instead.');38 }3940 return {...options, timeoutDuration: timeout, signal: cancelSignal};41};

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

line 42why, one step back
42

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

line 43why, one step back
43const spawnProcessAsync = ({file, args, options, command, escapedCommand, stdioStreamsGroups, stdioState}) => {

Split files (#837)ehmickybefore adoption672c2d7

lines 44–45why, one step back
44 let spawned;
45 try {

Improve spawning errors (#847)ehmickybefore adoption381dee2

line 46why, one step back
46 spawned = spawn(file, args, options);

Split files (#837)ehmickybefore adoption672c2d7

line 47why, one step back
47 } catch (error) {

Improve spawning errors (#847)ehmickybefore adoption381dee2

line 48why, one step back
48 return handleEarlyError({error, command, escapedCommand, stdioStreamsGroups, options});

Split files (#837)ehmickybefore adoption672c2d7

lines 49–54why, one step back
49 }
5051 const controller = new AbortController();52 setMaxListeners(Number.POSITIVE_INFINITY, controller.signal);5354 const originalStreams = [...spawned.stdio];

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

line 55why, one step back
55 pipeOutputAsync(spawned, stdioStreamsGroups, stdioState, controller);

Split files (#837)ehmickybefore adoption672c2d7

lines 56–61why, one step back
56 cleanupOnExit(spawned, options, controller);
5758 spawned.kill = spawnedKill.bind(undefined, {kill: spawned.kill.bind(spawned), spawned, options, controller});59 spawned.all = makeAllStream(spawned, options);6061 const promise = handlePromise({spawned, options, stdioStreamsGroups, originalStreams, command, escapedCommand, controller});

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

line 62why, one step back
62 return {spawned, promise};

Split files (#837)ehmickybefore adoption672c2d7

lines 63–77why, one step back
63};
6465const handlePromise = async ({spawned, options, stdioStreamsGroups, originalStreams, command, escapedCommand, controller}) => {66 const context = {timedOut: false};6768 const [69 errorInfo,70 [exitCode, signal],71 stdioResults,72 allResult,73 ] = await getSpawnedResult({spawned, options, context, stdioStreamsGroups, originalStreams, controller});74 controller.abort();7576 const stdio = stdioResults.map(stdioResult => handleOutput(options, stdioResult));77 const all = handleOutput(options, allResult);

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

lines 78–79why, one step back
78 const result = getAsyncResult({errorInfo, exitCode, signal, stdio, all, context, options, command, escapedCommand});
79 return handleResult(result, options);

Split files (#837)ehmickybefore adoption672c2d7

line 80why, one step back
80};

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

lines 81–95why, one step back
81
82const getAsyncResult = ({errorInfo, exitCode, signal, stdio, all, context, options, command, escapedCommand}) => 'error' in errorInfo83 ? makeError({84 error: errorInfo.error,85 command,86 escapedCommand,87 timedOut: context.timedOut,88 isCanceled: options.signal?.aborted === true,89 exitCode,90 signal,91 stdio,92 all,93 options,94 })95 : makeSuccessResult({command, escapedCommand, stdio, all, options});