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
Provenance
- Commit
4fb84f0- Tree
a517939- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
5b6d859
3 files changed+43 added−48 removed
modified:lib/command.js+2 added-2 removed
16 unmodified lines1718192021212223232425262791 unmodified lines16 unmodified lines
return `"${arg.replaceAll('"', '\\"')}"`;};
export const joinCommand = (file, args) => normalizeArgs(file, args).join(' ');export const joinCommand = (file, rawArgs) => normalizeArgs(file, rawArgs).join(' ');
export const getEscapedCommand = (file, args) => normalizeArgs(file, args).map(arg => escapeArg(arg)).join(' ');export const getEscapedCommand = (file, rawArgs) => normalizeArgs(file, rawArgs).map(arg => escapeArg(arg)).join(' ');
const SPACES_REGEXP = / +/g;
// Handle `execaCommand()`91 unmodified linesmodified:lib/error.js+1 added-1 removed
34 unmodified lines35 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 linesmodified:index.js+40 added-45 removed
30 unmodified lines31 };32 33 const normalizeFileUrl = file => file instanceof URL ? fileURLToPath(file) : file;34 35-const getFilePath = file => {36- const fileString = normalizeFileUrl(file);35+const getFilePath = rawFile => {36+ const fileString = normalizeFileUrl(rawFile);37 38 if (typeof fileString !== 'string') {39 throw new TypeError('First argument must be a string or a file URL.');40 }41 42 return fileString;43 };44 45-const handleArguments = (file, args, options = {}) => {46- const parsed = crossSpawn._parse(file, args, options);47- file = parsed.command;48- args = parsed.args;49- options = parsed.options;45+const handleArguments = (rawFile, rawArgs, rawOptions = {}) => {46+ const filePath = getFilePath(rawFile);47+ const command = joinCommand(filePath, rawArgs);48+ const escapedCommand = getEscapedCommand(filePath, rawArgs);49 50+ const {command: file, args, options: initialOptions} = crossSpawn._parse(filePath, rawArgs, rawOptions);51+51- 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 };69 70 options.env = getEnv(options);71 1 unmodified line73 // #11674 args.unshift('/q');75 }76 77+ logCommand(escapedCommand, options);78+76- return {file, args, options};79+ return {file, args, command, escapedCommand, options};80 };81 82 const handleOutputSync = (options, value, error) => {83 if (Buffer.isBuffer(value)) {15 unmodified lines99 100 return value;101 };102 100-export 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);103+export function execa(rawFile, rawArgs, rawOptions) {104+ const {file, args, command, escapedCommand, options} = handleArguments(rawFile, rawArgs, rawOptions);105+ validateTimeout(options);106 106- const command = joinCommand(filePath, args);107- const escapedCommand = getEscapedCommand(filePath, args);108- logCommand(escapedCommand, parsed.options);107+ const stdioStreams = handleInputAsync(options);108 109 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 lines118 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 }129 130 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);133 134 const context = {isCanceled: false};135 136 spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned));137 spawned.cancel = spawnedCancel.bind(null, spawned, context);138 139 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);144 145 if (error || exitCode !== 0 || signal !== null) {146 const returnedError = makeError({147 error,3 unmodified lines151 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 });159 161- if (!parsed.options.reject) {160+ if (!options.reject) {161 return returnedError;162 }163 164 throw returnedError;16 unmodified lines181 const handlePromiseOnce = onetime(handlePromise);182 183 pipeOutputAsync(spawned, stdioStreams);184 186- spawned.all = makeAllStream(spawned, parsed.options);185+ spawned.all = makeAllStream(spawned, options);186 187 addPipeMethods(spawned);188 mergePromise(spawned, handlePromiseOnce);189 return spawned;190 }191 193-export function execaSync(file, args, options) {194- const filePath = getFilePath(file);195- const parsed = handleArguments(filePath, args, options);196- const stdioStreams = handleInputSync(parsed.options);192+export function execaSync(rawFile, rawArgs, rawOptions) {193+ const {file, args, command, escapedCommand, options} = handleArguments(rawFile, rawArgs, rawOptions);194 198- const command = joinCommand(filePath, args);199- const escapedCommand = getEscapedCommand(filePath, args);200- logCommand(escapedCommand, parsed.options);195+ const stdioStreams = handleInputSync(options);196 197 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 }213 214 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);217 218 if (result.error || result.status !== 0 || result.signal !== null) {219 const error = makeError({220 stdout,2 unmodified lines223 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 });231 237- if (!parsed.options.reject) {232+ if (!options.reject) {233 return error;234 }235 236 throw error;92 unmodified lines