Skip to content
Myr

Refactor duplicate code between `execa()` and `execaSync()` (#648)

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

authored by ehmicky · committed by GitHub · parent 5b6d859 · Browse files · 3 files+43 added−48 removed
This commit

State

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

Diff stats

3 files+43 added−48 removed

  • other:+43 added−48 removed

3 files+43 added−48 removed

modified:lib/command.js+2 added-2 removed
modified:lib/error.js+1 added-1 removed
34 unmodified lines
35 command,36 escapedCommand,37 timedOut,38 isCanceled,39 parsed: {options: {timeout, cwd = process.cwd()}},39 options: {timeout, cwd = process.cwd()},40}) => {41 // `signal` and `exitCode` emitted on `spawned.on('exit')` event can be `null`.42 // We normalize them to `undefined`43 exitCode = exitCode === null ? undefined : exitCode;
43 unmodified lines
modified:index.js+40 added-45 removed
30 unmodified lines
31};3233const normalizeFileUrl = file => file instanceof URL ? fileURLToPath(file) : file;3435const getFilePath = file => {36 const fileString = normalizeFileUrl(file);35const getFilePath = rawFile => {36 const fileString = normalizeFileUrl(rawFile);3738 if (typeof fileString !== 'string') {39 throw new TypeError('First argument must be a string or a file URL.');40 }4142 return fileString;43};4445const handleArguments = (file, args, options = {}) => {46 const parsed = crossSpawn._parse(file, args, options);47 file = parsed.command;48 args = parsed.args;49 options = parsed.options;45const handleArguments = (rawFile, rawArgs, rawOptions = {}) => {46 const filePath = getFilePath(rawFile);47 const command = joinCommand(filePath, rawArgs);48 const escapedCommand = getEscapedCommand(filePath, rawArgs);4950 const {command: file, args, options: initialOptions} = crossSpawn._parse(filePath, rawArgs, rawOptions);5151 options = {52 const options = {53 maxBuffer: DEFAULT_MAX_BUFFER,54 buffer: true,55 stripFinalNewline: true,56 extendEnv: true,57 preferLocal: false,57 localDir: options.cwd || process.cwd(),58 localDir: initialOptions.cwd || process.cwd(),59 execPath: process.execPath,60 encoding: 'utf8',61 reject: true,62 cleanup: true,63 all: false,64 windowsHide: true,65 verbose: verboseDefault,65 ...options,66 shell: normalizeFileUrl(options.shell),66 ...initialOptions,67 shell: normalizeFileUrl(initialOptions.shell),68 };6970 options.env = getEnv(options);71
1 unmodified line
73 // #11674 args.unshift('/q');75 }7677 logCommand(escapedCommand, options);7876 return {file, args, options};79 return {file, args, command, escapedCommand, options};80};8182const handleOutputSync = (options, value, error) => {83 if (Buffer.isBuffer(value)) {
15 unmodified lines
99100 return value;101};102100export function execa(file, args, options) {101 const filePath = getFilePath(file);102 const parsed = handleArguments(filePath, args, options);103 const stdioStreams = handleInputAsync(parsed.options);104 validateTimeout(parsed.options);103export function execa(rawFile, rawArgs, rawOptions) {104 const {file, args, command, escapedCommand, options} = handleArguments(rawFile, rawArgs, rawOptions);105 validateTimeout(options);106106 const command = joinCommand(filePath, args);107 const escapedCommand = getEscapedCommand(filePath, args);108 logCommand(escapedCommand, parsed.options);107 const stdioStreams = handleInputAsync(options);108109 let spawned;110 try {112 spawned = childProcess.spawn(parsed.file, parsed.args, parsed.options);111 spawned = childProcess.spawn(file, args, options);112 } catch (error) {113 // Ensure the returned error is always both a promise and a child process114 const dummySpawned = new childProcess.ChildProcess();115 const errorPromise = Promise.reject(makeError({
2 unmodified lines
118 stderr: '',119 all: '',120 command,121 escapedCommand,123 parsed,122 options,123 timedOut: false,124 isCanceled: false,125 }));126 mergePromise(dummySpawned, errorPromise);127 return dummySpawned;128 }129130 const spawnedPromise = getSpawnedPromise(spawned);132 const timedPromise = setupTimeout(spawned, parsed.options, spawnedPromise);133 const processDone = setExitHandler(spawned, parsed.options, timedPromise);131 const timedPromise = setupTimeout(spawned, options, spawnedPromise);132 const processDone = setExitHandler(spawned, options, timedPromise);133134 const context = {isCanceled: false};135136 spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned));137 spawned.cancel = spawnedCancel.bind(null, spawned, context);138139 const handlePromise = async () => {141 const [{error, exitCode, signal, timedOut}, stdoutResult, stderrResult, allResult] = await getSpawnedResult(spawned, parsed.options, stdioStreams, processDone);142 const stdout = handleOutput(parsed.options, stdoutResult);143 const stderr = handleOutput(parsed.options, stderrResult);144 const all = handleOutput(parsed.options, allResult);140 const [{error, exitCode, signal, timedOut}, stdoutResult, stderrResult, allResult] = await getSpawnedResult(spawned, options, stdioStreams, processDone);141 const stdout = handleOutput(options, stdoutResult);142 const stderr = handleOutput(options, stderrResult);143 const all = handleOutput(options, allResult);144145 if (error || exitCode !== 0 || signal !== null) {146 const returnedError = makeError({147 error,
3 unmodified lines
151 stderr,152 all,153 command,154 escapedCommand,156 parsed,155 options,156 timedOut,158 isCanceled: context.isCanceled || (parsed.options.signal ? parsed.options.signal.aborted : false),157 isCanceled: context.isCanceled || (options.signal ? options.signal.aborted : false),158 });159161 if (!parsed.options.reject) {160 if (!options.reject) {161 return returnedError;162 }163164 throw returnedError;
16 unmodified lines
181 const handlePromiseOnce = onetime(handlePromise);182183 pipeOutputAsync(spawned, stdioStreams);184186 spawned.all = makeAllStream(spawned, parsed.options);185 spawned.all = makeAllStream(spawned, options);186187 addPipeMethods(spawned);188 mergePromise(spawned, handlePromiseOnce);189 return spawned;190}191193export function execaSync(file, args, options) {194 const filePath = getFilePath(file);195 const parsed = handleArguments(filePath, args, options);196 const stdioStreams = handleInputSync(parsed.options);192export function execaSync(rawFile, rawArgs, rawOptions) {193 const {file, args, command, escapedCommand, options} = handleArguments(rawFile, rawArgs, rawOptions);194198 const command = joinCommand(filePath, args);199 const escapedCommand = getEscapedCommand(filePath, args);200 logCommand(escapedCommand, parsed.options);195 const stdioStreams = handleInputSync(options);196197 let result;198 try {204 result = childProcess.spawnSync(parsed.file, parsed.args, parsed.options);199 result = childProcess.spawnSync(file, args, options);200 } catch (error) {201 throw makeError({202 error,203 stdout: '',204 stderr: '',205 all: '',206 command,207 escapedCommand,213 parsed,208 options,209 timedOut: false,210 isCanceled: false,211 });212 }213214 pipeOutputSync(stdioStreams, result);220 const stdout = handleOutputSync(parsed.options, result.stdout, result.error);221 const stderr = handleOutputSync(parsed.options, result.stderr, result.error);215 const stdout = handleOutputSync(options, result.stdout, result.error);216 const stderr = handleOutputSync(options, result.stderr, result.error);217218 if (result.error || result.status !== 0 || result.signal !== null) {219 const error = makeError({220 stdout,
2 unmodified lines
223 signal: result.signal,224 exitCode: result.status,225 command,226 escapedCommand,232 parsed,227 options,228 timedOut: result.error && result.error.code === 'ETIMEDOUT',229 isCanceled: false,230 });231237 if (!parsed.options.reject) {232 if (!options.reject) {233 return error;234 }235236 throw error;
92 unmodified lines