Skip to content
Myr

Refactor `reject` option (#868)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 8f51c6b · Browse files · 5 files+80 added−94 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+80 added−94 removed

  • other:+80 added−94 removed

5 files+80 added−94 removed

modified:lib/return/early-error.js+5 added-10 removed
modified:lib/return/output.js+8 added
modified:lib/stdio/sync.js+3 added-3 removed
modified:lib/async.js+32 added-40 removed
1import {setMaxListeners} from 'node:events';2import {spawn} from 'node:child_process';3import {normalizeArguments, handleArguments} from './arguments/options.js';4import {makeError, makeSuccessResult} from './return/error.js';5import {handleOutput} from './return/output.js';5import {handleOutput, handleResult} from './return/output.js';6import {handleEarlyError} from './return/early-error.js';7import {handleInputAsync, pipeOutputAsync} from './stdio/async.js';8import {spawnedKill} from './exit/kill.js';9import {cleanupOnExit} from './exit/cleanup.js';
3 unmodified lines
13import {getSpawnedResult} from './stream/resolve.js';14import {mergePromise} from './promise.js';1516export const execa = (rawFile, rawArgs, rawOptions) => {17 const {spawned, promise, options, stdioStreamsGroups} = runExeca(rawFile, rawArgs, rawOptions);17 const {file, args, command, escapedCommand, options} = handleAsyncArguments(rawFile, rawArgs, rawOptions);18 const stdioStreamsGroups = handleInputAsync(options);19 const {spawned, promise} = runExeca({file, args, options, command, escapedCommand, stdioStreamsGroups});20 spawned.pipe = pipeToProcess.bind(undefined, {source: spawned, sourcePromise: promise, stdioStreamsGroups, destinationOptions: {}});21 mergePromise(spawned, promise);22 PROCESS_OPTIONS.set(spawned, options);23 return spawned;24};2524const runExeca = (rawFile, rawArgs, rawOptions) => {25 const {file, args, command, escapedCommand, options} = handleAsyncArguments(rawFile, rawArgs, rawOptions);26 const stdioStreamsGroups = handleInputAsync(options);26const handleAsyncArguments = (rawFile, rawArgs, rawOptions) => {27 [rawFile, rawArgs, rawOptions] = normalizeArguments(rawFile, rawArgs, rawOptions);28 const {file, args, command, escapedCommand, options: normalizedOptions} = handleArguments(rawFile, rawArgs, rawOptions);29 const options = handleAsyncOptions(normalizedOptions);30 return {file, args, command, escapedCommand, options};31};3233// Prevent passing the `timeout` option directly to `child_process.spawn()`34const handleAsyncOptions = ({timeout, ...options}) => ({...options, timeoutDuration: timeout});3536const runExeca = ({file, args, options, command, escapedCommand, stdioStreamsGroups}) => {37 let spawned;38 try {39 spawned = spawn(file, args, options);40 } catch (error) {
10 unmodified lines
51 spawned.kill = spawnedKill.bind(undefined, {kill: spawned.kill.bind(spawned), spawned, options, controller});52 spawned.all = makeAllStream(spawned, options);5354 const promise = handlePromise({spawned, options, stdioStreamsGroups, originalStreams, command, escapedCommand, controller});46 return {spawned, promise, options, stdioStreamsGroups};55 return {spawned, promise};56};5749const handleAsyncArguments = (rawFile, rawArgs, rawOptions) => {50 [rawFile, rawArgs, rawOptions] = normalizeArguments(rawFile, rawArgs, rawOptions);51 const {file, args, command, escapedCommand, options: normalizedOptions} = handleArguments(rawFile, rawArgs, rawOptions);52 const options = handleAsyncOptions(normalizedOptions);53 return {file, args, command, escapedCommand, options};54};5556// Prevent passing the `timeout` option directly to `child_process.spawn()`57const handleAsyncOptions = ({timeout, ...options}) => ({...options, timeoutDuration: timeout});5858const handlePromise = async ({spawned, options, stdioStreamsGroups, originalStreams, command, escapedCommand, controller}) => {59 const context = {timedOut: false};6061 const [
5 unmodified lines
67 controller.abort();6869 const stdio = stdioResults.map(stdioResult => handleOutput(options, stdioResult));70 const all = handleOutput(options, allResult);7273 if ('error' in errorInfo) {74 const isCanceled = options.signal?.aborted === true;75 const returnedError = makeError({76 error: errorInfo.error,77 command,78 escapedCommand,79 timedOut: context.timedOut,80 isCanceled,81 exitCode,82 signal,83 stdio,84 all,85 options,86 });8788 if (!options.reject) {89 return returnedError;90 }9192 throw returnedError;93 }9495 return makeSuccessResult({command, escapedCommand, stdio, all, options});71 const result = getAsyncResult({errorInfo, exitCode, signal, stdio, all, context, options, command, escapedCommand});72 return handleResult(result, options);73};7475const getAsyncResult = ({errorInfo, exitCode, signal, stdio, all, context, options, command, escapedCommand}) => 'error' in errorInfo76 ? makeError({77 error: errorInfo.error,78 command,79 escapedCommand,80 timedOut: context.timedOut,81 isCanceled: options.signal?.aborted === true,82 exitCode,83 signal,84 stdio,85 all,86 options,87 })88 : makeSuccessResult({command, escapedCommand, stdio, all, options});
modified:lib/sync.js+32 added-41 removed