Skip to content
Myr

async.js

This file

Path

lib/async.js

106 lines · 4.6 KiB · JavaScript

Last changed by

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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';

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 8why, one step back
8import {subprocessKill} from './exit/kill.js';

Split files (#837)ehmickybefore adoption672c2d7

line 9why, one step back
9import {cleanupOnExit} from './exit/cleanup.js';

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 10–11why, one step back
10import {pipeToSubprocess} from './pipe/setup.js';
11import {SUBPROCESS_OPTIONS} from './pipe/validate.js';

Print process completion with verbose option (#887)ehmickybefore adoption7264bcf

line 12why, one step back
12import {logEarlyResult} from './verbose/complete.js';

Split files (#837)ehmickybefore adoption672c2d7

line 13why, one step back
13import {makeAllStream} from './stream/all.js';

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

line 14why, one step back
14import {addConvertedStreams} from './convert/add.js';

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 15why, one step back
15import {getSubprocessResult} from './stream/resolve.js';

Split files (#837)ehmickybefore adoption672c2d7

lines 16–18why, one step back
16import {mergePromise} from './promise.js';
1718export const execa = (rawFile, rawArgs, rawOptions) => {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 19–20why, one step back
19 const {file, args, command, escapedCommand, startTime, verboseInfo, options, fileDescriptors, stdioState} = handleAsyncArguments(rawFile, rawArgs, rawOptions);
20 const {subprocess, promise} = spawnSubprocessAsync({file, args, options, startTime, verboseInfo, command, escapedCommand, fileDescriptors, stdioState});

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

line 21why, one step back
21 subprocess.pipe = pipeToSubprocess.bind(undefined, {source: subprocess, sourcePromise: promise, boundOptions: {}});

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 22why, one step back
22 mergePromise(subprocess, promise);

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 23why, one step back
23 SUBPROCESS_OPTIONS.set(subprocess, {options, fileDescriptors});

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 24why, one step back
24 return subprocess;

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

lines 25–26why, one step back
25};
26

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

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

Add `result|error.durationMs` (#896)ehmickybefore adoption7088091

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

Print process completion with verbose option (#887)ehmickybefore adoption7264bcf

lines 30–33why, one step back
30
31 try {32 const {file, args, options: normalizedOptions} = handleArguments(rawFile, rawArgs, rawOptions);33 const options = handleAsyncOptions(normalizedOptions);

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 34–35why, one step back
34 const {fileDescriptors, stdioState} = handleInputAsync(options, verboseInfo);
35 return {file, args, command, escapedCommand, startTime, verboseInfo, options, fileDescriptors, stdioState};

Print process completion with verbose option (#887)ehmickybefore adoption7264bcf

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

Add `result|error.durationMs` (#896)ehmickybefore adoption7088091

line 37why, one step back
37 logEarlyResult(error, startTime, verboseInfo);

Print process completion with verbose option (#887)ehmickybefore adoption7264bcf

lines 38–39why, one step back
38 throw error;
39 }

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

line 40why, one step back
40};

Split files (#837)ehmickybefore adoption672c2d7

line 41why, one step back
41

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

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

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

lines 43–49why, one step back
43const handleAsyncOptions = ({timeout, signal, cancelSignal, ...options}) => {
44 if (signal !== undefined) {45 throw new TypeError('The "signal" option has been renamed to "cancelSignal" instead.');46 }4748 return {...options, timeoutDuration: timeout, signal: cancelSignal};49};

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

line 50why, one step back
50

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 51why, one step back
51const spawnSubprocessAsync = ({file, args, options, startTime, verboseInfo, command, escapedCommand, fileDescriptors, stdioState}) => {

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 52why, one step back
52 let subprocess;

Split files (#837)ehmickybefore adoption672c2d7

line 53why, one step back
53 try {

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 54why, one step back
54 subprocess = spawn(file, args, options);

Split files (#837)ehmickybefore adoption672c2d7

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 56why, one step back
56 return handleEarlyError({error, command, escapedCommand, fileDescriptors, options, startTime, verboseInfo});

Split files (#837)ehmickybefore adoption672c2d7

lines 57–61why, one step back
57 }
5859 const controller = new AbortController();60 setMaxListeners(Number.POSITIVE_INFINITY, controller.signal);61

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 62why, one step back
62 const originalStreams = [...subprocess.stdio];

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 63why, one step back
63 pipeOutputAsync(subprocess, fileDescriptors, stdioState, controller);

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 64why, one step back
64 cleanupOnExit(subprocess, options, controller);

Split files (#837)ehmickybefore adoption672c2d7

line 65why, one step back
65

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 66–67why, one step back
66 subprocess.kill = subprocessKill.bind(undefined, {kill: subprocess.kill.bind(subprocess), subprocess, options, controller});
67 subprocess.all = makeAllStream(subprocess, options);

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 68why, one step back
68 addConvertedStreams(subprocess, options);

Split files (#837)ehmickybefore adoption672c2d7

line 69why, one step back
69

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 70why, one step back
70 const promise = handlePromise({subprocess, options, startTime, verboseInfo, fileDescriptors, originalStreams, command, escapedCommand, controller});

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 71why, one step back
71 return {subprocess, promise};

Split files (#837)ehmickybefore adoption672c2d7

lines 72–73why, one step back
72};
73

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 74why, one step back
74const handlePromise = async ({subprocess, options, startTime, verboseInfo, fileDescriptors, originalStreams, command, escapedCommand, controller}) => {

Split files (#837)ehmickybefore adoption672c2d7

lines 75–81why, one step back
75 const context = {timedOut: false};
7677 const [78 errorInfo,79 [exitCode, signal],80 stdioResults,81 allResult,

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 82why, one step back
82 ] = await getSubprocessResult({subprocess, options, context, fileDescriptors, originalStreams, controller});

Split files (#837)ehmickybefore adoption672c2d7

lines 83–86why, one step back
83 controller.abort();
8485 const stdio = stdioResults.map(stdioResult => handleOutput(options, stdioResult));86 const all = handleOutput(options, allResult);

Add `result|error.durationMs` (#896)ehmickybefore adoption7088091

line 87why, one step back
87 const result = getAsyncResult({errorInfo, exitCode, signal, stdio, all, context, options, command, escapedCommand, startTime});

Print process completion with verbose option (#887)ehmickybefore adoption7264bcf

line 88why, one step back
88 return handleResult(result, verboseInfo, options);

Split files (#837)ehmickybefore adoption672c2d7

line 89why, one step back
89};

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

line 90why, one step back
90

Add `result|error.durationMs` (#896)ehmickybefore adoption7088091

line 91why, one step back
91const getAsyncResult = ({errorInfo, exitCode, signal, stdio, all, context, options, command, escapedCommand, startTime}) => 'error' in errorInfo

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

lines 92–102why, one step back
92 ? makeError({
93 error: errorInfo.error,94 command,95 escapedCommand,96 timedOut: context.timedOut,97 isCanceled: options.signal?.aborted === true,98 exitCode,99 signal,100 stdio,101 all,102 options,

Add `result|error.durationMs` (#896)ehmickybefore adoption7088091

line 103why, one step back
103 startTime,

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

line 104why, one step back
104 isSync: false,

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

line 105why, one step back
105 })

Add `result|error.durationMs` (#896)ehmickybefore adoption7088091

line 106why, one step back
106 : makeSuccessResult({command, escapedCommand, stdio, all, options, startTime});