Avoid code duplication with return value's logic (#829)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 9435a34 · Browse files · 2 files·+54 added−58 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+54 added−58 removed
- other:+54 added−58 removed
Provenance
- Commit
100f04f- Tree
98326ec- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
9435a34
2 files changed+54 added−58 removed
modified:lib/error.js+41 added-4 removed
90 unmodified lines9192939495969598999697989910010110210310410510610740 unmodified lines14814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718890 unmodified lines return {exitCode, signal, signalDescription};};
export const makeError = ({ stdio, all, error: rawError, signal: rawSignal, exitCode: rawExitCode, command, escapedCommand, timedOut, isCanceled, exitCode: rawExitCode, signal: rawSignal, stdio, all, options: {timeoutDuration, timeout = timeoutDuration, cwd},}) => { const initialError = rawError instanceof DiscardedError ? undefined : rawError; const {exitCode, signal, signalDescription} = normalizeExitPayload(rawExitCode, rawSignal);40 unmodified lines }
return error;};
export const makeEarlyError = ({ error, command, escapedCommand, stdioStreamsGroups, options,}) => makeError({ error, command, escapedCommand, timedOut: false, isCanceled: false, stdio: Array.from({length: stdioStreamsGroups.length}), options,});
export const makeSuccessResult = ({ command, escapedCommand, stdio, all, options: {cwd},}) => ({ command, escapedCommand, cwd, failed: false, timedOut: false, isCanceled: false, isTerminated: false, exitCode: 0, stdout: stdio[1], stderr: stdio[2], all, stdio,});modified:index.js+13 added-54 removed
4 unmodified lines5 import process from 'node:process';6 import crossSpawn from 'cross-spawn';7 import stripFinalNewline from 'strip-final-newline';8 import {npmRunPathEnv} from 'npm-run-path';9-import {makeError} from './lib/error.js';9+import {makeError, makeEarlyError, makeSuccessResult} from './lib/error.js';10 import {handleNodeOption} from './lib/node.js';11 import {handleInputAsync, pipeOutputAsync, cleanupStdioStreams} from './lib/stdio/async.js';12 import {handleInputSync, pipeOutputSync} from './lib/stdio/sync.js';13 import {spawnedKill, validateTimeout, normalizeForceKillAfterDelay, cleanupOnExit, isFailedExit} from './lib/kill.js';140 unmodified lines154 } catch (error) {155 cleanupStdioStreams(stdioStreamsGroups);156 // Ensure the returned error is always both a promise and a child process157 const dummySpawned = new childProcess.ChildProcess();158- const errorInstance = makeError({159- error,160- stdio: Array.from({length: stdioStreamsGroups.length}),161- command,162- escapedCommand,163- options,164- timedOut: false,165- isCanceled: false,166- });158+ const errorInstance = makeEarlyError({error, command, escapedCommand, stdioStreamsGroups, options});159 const errorPromise = options.reject ? Promise.reject(errorInstance) : Promise.resolve(errorInstance);160 mergePromise(dummySpawned, errorPromise);161 return dummySpawned;162 }31 unmodified lines194 if ('error' in errorInfo) {195 const isCanceled = options.signal?.aborted === true;196 const returnedError = makeError({197 error: errorInfo.error,198+ command,199+ escapedCommand,200+ timedOut: context.timedOut,201+ isCanceled,202 exitCode,203 signal,204 stdio,205 all,210- command,211- escapedCommand,206 options,213- timedOut: context.timedOut,214- isCanceled,207 });208 209 if (!options.reject) {210 return returnedError;1 unmodified line212 213 throw returnedError;214 }215 224- return {225- command,226- escapedCommand,227- cwd: options.cwd,228- failed: false,229- timedOut: false,230- isCanceled: false,231- isTerminated: false,232- exitCode: 0,233- stdout: stdio[1],234- stderr: stdio[2],235- all,236- stdio,237- };216+ return makeSuccessResult({command, escapedCommand, stdio, all, options});217 };218 219 export function execaSync(rawFile, rawArgs, rawOptions) {220 const {file, args, command, escapedCommand, options} = handleSyncArguments(rawFile, rawArgs, rawOptions);2 unmodified lines223 let result;224 try {225 result = childProcess.spawnSync(file, args, options);226 } catch (error) {248- const errorInstance = makeError({249- error,250- stdio: Array.from({length: stdioStreamsGroups.stdioLength}),251- command,252- escapedCommand,253- options,254- timedOut: false,255- isCanceled: false,256- });227+ const errorInstance = makeEarlyError({error, command, escapedCommand, stdioStreamsGroups, options});228 229 if (!options.reject) {230 return errorInstance;231 }7 unmodified lines239 const stdio = output.map(stdioOutput => handleOutput(options, stdioOutput));240 241 if (result.error !== undefined || isFailedExit(result.status, result.signal)) {242 const error = makeError({272- stdio,243 error: result.error,274- signal: result.signal,275- exitCode: result.status,244 command,245 escapedCommand,278- options,246 timedOut: result.error && result.error.code === 'ETIMEDOUT',247 isCanceled: false,248+ exitCode: result.status,249+ signal: result.signal,250+ stdio,251+ options,252 });253 254 if (!options.reject) {255 return error;1 unmodified line257 258 throw error;259 }260 290- return {291- command,292- escapedCommand,293- cwd: options.cwd,294- failed: false,295- timedOut: false,296- isCanceled: false,297- isTerminated: false,298- exitCode: 0,299- stdout: stdio[1],300- stderr: stdio[2],301- stdio,302- };261+ return makeSuccessResult({command, escapedCommand, stdio, options});262 }263 264 const normalizeScriptStdin = ({input, inputFile, stdio}) => input === undefined && inputFile === undefined && stdio === undefined265 ? {stdin: 'inherit'}53 unmodified lines