Skip to content
Myr

async.js

This file

Path

lib/async.js

105 lines · 4.6 KiB · JavaScript

Last changed by

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

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

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

line 3why, one step back
3import {handleCommand, handleOptions} 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–17why, one step back
16import {mergePromise} from './promise.js';
17

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

line 18why, one step back
18export const execaCoreAsync = (rawFile, rawArgs, rawOptions, createNested) => {

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 template strings with all methods (#933)ehmickybefore adoption8074d85

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

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

line 27why, one step back
27const handleAsyncArguments = (rawFile, rawArgs, rawOptions) => {

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

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

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

lines 29–30why, one step back
29
30 try {

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

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

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

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

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

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

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

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

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

line 39why, one step back
39};

Split files (#837)ehmickybefore adoption672c2d7

line 40why, one step back
40

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

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

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

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

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

line 49why, one step back
49

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

line 51why, one step back
51 let subprocess;

Split files (#837)ehmickybefore adoption672c2d7

line 52why, one step back
52 try {

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

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

Split files (#837)ehmickybefore adoption672c2d7

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

Split files (#837)ehmickybefore adoption672c2d7

lines 56–60why, one step back
56 }
5758 const controller = new AbortController();59 setMaxListeners(Number.POSITIVE_INFINITY, controller.signal);60

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

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

Split files (#837)ehmickybefore adoption672c2d7

line 64why, one step back
64

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

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

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

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

Split files (#837)ehmickybefore adoption672c2d7

line 68why, one step back
68

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

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

Split files (#837)ehmickybefore adoption672c2d7

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

Split files (#837)ehmickybefore adoption672c2d7

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

Split files (#837)ehmickybefore adoption672c2d7

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

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

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

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

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

Split files (#837)ehmickybefore adoption672c2d7

line 88why, one step back
88};

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

line 89why, one step back
89

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

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

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

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

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

line 102why, one step back
102 startTime,

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

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

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

line 104why, one step back
104 })

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

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