async.js
This file
Path
lib/async.js
105 lines · 4.6 KiB · JavaScript
Last changed by
Add support for `all` option to `execaSync()` (#956)ehmickybefore adoption63b5ac4
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106import {setMaxListeners} from 'node:events';import {spawn} from 'node:child_process';import {handleCommand, handleOptions} from './arguments/options.js';import {makeError, makeSuccessResult} from './return/error.js';import {stripNewline, handleResult} from './return/output.js';import {handleEarlyError} from './return/early-error.js';import {handleInputAsync, pipeOutputAsync} from './stdio/async.js';import {subprocessKill} from './exit/kill.js';import {cleanupOnExit} from './exit/cleanup.js';import {pipeToSubprocess} from './pipe/setup.js';import {SUBPROCESS_OPTIONS} from './pipe/validate.js';import {logEarlyResult} from './verbose/complete.js';import {makeAllStream} from './stream/all.js';import {addConvertedStreams} from './convert/add.js';import {getSubprocessResult} from './stream/resolve.js';import {mergePromise} from './promise.js';
export const execaCoreAsync = (rawFile, rawArgs, rawOptions, createNested) => { const {file, args, command, escapedCommand, startTime, verboseInfo, options, fileDescriptors, stdioState} = handleAsyncArguments(rawFile, rawArgs, rawOptions); const {subprocess, promise} = spawnSubprocessAsync({file, args, options, startTime, verboseInfo, command, escapedCommand, fileDescriptors, stdioState}); subprocess.pipe = pipeToSubprocess.bind(undefined, {source: subprocess, sourcePromise: promise, boundOptions: {}, createNested}); mergePromise(subprocess, promise); SUBPROCESS_OPTIONS.set(subprocess, {options, fileDescriptors}); return subprocess;};
const handleAsyncArguments = (rawFile, rawArgs, rawOptions) => { const {command, escapedCommand, startTime, verboseInfo} = handleCommand(rawFile, rawArgs, rawOptions);
try { const {file, args, options: normalizedOptions} = handleOptions(rawFile, rawArgs, rawOptions); const options = handleAsyncOptions(normalizedOptions); const {fileDescriptors, stdioState} = handleInputAsync(options, verboseInfo); return {file, args, command, escapedCommand, startTime, verboseInfo, options, fileDescriptors, stdioState}; } catch (error) { logEarlyResult(error, startTime, verboseInfo); throw error; }};
// Prevent passing the `timeout` option directly to `child_process.spawn()`const handleAsyncOptions = ({timeout, signal, cancelSignal, ...options}) => { if (signal !== undefined) { throw new TypeError('The "signal" option has been renamed to "cancelSignal" instead.'); }
return {...options, timeoutDuration: timeout, signal: cancelSignal};};
const spawnSubprocessAsync = ({file, args, options, startTime, verboseInfo, command, escapedCommand, fileDescriptors, stdioState}) => { let subprocess; try { subprocess = spawn(file, args, options); } catch (error) { return handleEarlyError({error, command, escapedCommand, fileDescriptors, options, startTime, verboseInfo}); }
const controller = new AbortController(); setMaxListeners(Number.POSITIVE_INFINITY, controller.signal);
const originalStreams = [...subprocess.stdio]; pipeOutputAsync(subprocess, fileDescriptors, stdioState, controller); cleanupOnExit(subprocess, options, controller);
subprocess.kill = subprocessKill.bind(undefined, {kill: subprocess.kill.bind(subprocess), subprocess, options, controller}); subprocess.all = makeAllStream(subprocess, options); addConvertedStreams(subprocess, options);
const promise = handlePromise({subprocess, options, startTime, verboseInfo, fileDescriptors, originalStreams, command, escapedCommand, controller}); return {subprocess, promise};};
const handlePromise = async ({subprocess, options, startTime, verboseInfo, fileDescriptors, originalStreams, command, escapedCommand, controller}) => { const context = {timedOut: false};
const [ errorInfo, [exitCode, signal], stdioResults, allResult, ] = await getSubprocessResult({subprocess, options, context, fileDescriptors, originalStreams, controller}); controller.abort();
const stdio = stdioResults.map(stdioResult => stripNewline(stdioResult, options)); const all = stripNewline(allResult, options); const result = getAsyncResult({errorInfo, exitCode, signal, stdio, all, context, options, command, escapedCommand, startTime}); return handleResult(result, verboseInfo, options);};
const getAsyncResult = ({errorInfo, exitCode, signal, stdio, all, context, options, command, escapedCommand, startTime}) => 'error' in errorInfo ? makeError({ error: errorInfo.error, command, escapedCommand, timedOut: context.timedOut, isCanceled: options.signal?.aborted === true, exitCode, signal, stdio, all, options, startTime, isSync: false, }) : makeSuccessResult({command, escapedCommand, stdio, all, options, startTime});