Skip to content
Myr

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

2 files+54 added−58 removed

modified:lib/error.js+41 added-4 removed
modified:index.js+13 added-54 removed
4 unmodified lines
5import process from 'node:process';6import crossSpawn from 'cross-spawn';7import stripFinalNewline from 'strip-final-newline';8import {npmRunPathEnv} from 'npm-run-path';9import {makeError} from './lib/error.js';9import {makeError, makeEarlyError, makeSuccessResult} from './lib/error.js';10import {handleNodeOption} from './lib/node.js';11import {handleInputAsync, pipeOutputAsync, cleanupStdioStreams} from './lib/stdio/async.js';12import {handleInputSync, pipeOutputSync} from './lib/stdio/sync.js';13import {spawnedKill, validateTimeout, normalizeForceKillAfterDelay, cleanupOnExit, isFailedExit} from './lib/kill.js';
140 unmodified lines
154 } 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 lines
194 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 });208209 if (!options.reject) {210 return returnedError;
1 unmodified line
212213 throw returnedError;214 }215224 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};218219export function execaSync(rawFile, rawArgs, rawOptions) {220 const {file, args, command, escapedCommand, options} = handleSyncArguments(rawFile, rawArgs, rawOptions);
2 unmodified lines
223 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});228229 if (!options.reject) {230 return errorInstance;231 }
7 unmodified lines
239 const stdio = output.map(stdioOutput => handleOutput(options, stdioOutput));240241 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 });253254 if (!options.reject) {255 return error;
1 unmodified line
257258 throw error;259 }260290 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}263264const normalizeScriptStdin = ({input, inputFile, stdio}) => input === undefined && inputFile === undefined && stdio === undefined265 ? {stdin: 'inherit'}
53 unmodified lines