main-sync.js
This file
Path
lib/methods/main-sync.js
175 lines · 5.1 KiB · JavaScript
Last changed by
Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 1–3why, one step back1import {spawnSync} from 'node:child_process';2import {handleCommand} from '../arguments/command.js';3import {normalizeOptions} from '../arguments/options.js';Fix Node 24-specific deprecation warning (#1199)ehmickybefore adoption1ac5b91
line 4why, one step back4import {concatenateShell} from '../arguments/shell.js';Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
lines 5–10why, one step back5import {6 makeError,7 makeEarlyError,8 makeSuccessResult,9 getEarlyErrorOutput,10} from '../return/result.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 11–16why, one step back11import {handleResult} from '../return/reject.js';12import {handleStdioSync} from '../stdio/handle-sync.js';13import {stripNewline} from '../io/strip-newline.js';14import {addInputOptionsSync} from '../io/input-sync.js';15import {transformOutputSync} from '../io/output-sync.js';16import {getMaxBufferSync} from '../io/max-buffer.js';Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
line 17why, one step back17import {getAllSync, getAllSyncFds} from '../resolve/all-sync.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 18–19why, one step back18import {getExitResultSync} from '../resolve/exit-sync.js';19Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 20why, one step back20// Main shared logic for all sync methods: `execaSync()`, `$.sync()`100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 21–38why, one step back21export const execaCoreSync = (rawFile, rawArguments, rawOptions) => {22 const {file, commandArguments, command, escapedCommand, startTime, verboseInfo, options, fileDescriptors} = handleSyncArguments(rawFile, rawArguments, rawOptions);23 const result = spawnSubprocessSync({24 file,25 commandArguments,26 options,27 command,28 escapedCommand,29 verboseInfo,30 fileDescriptors,31 startTime,32 });33 return handleResult(result, verboseInfo, options);34};3536// Compute arguments to pass to `child_process.spawnSync()`37const handleSyncArguments = (rawFile, rawArguments, rawOptions) => {38 const {command, escapedCommand, startTime, verboseInfo} = handleCommand(rawFile, rawArguments, rawOptions);Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
lines 39–52why, one step back39 const syncOptions = normalizeSyncOptions(rawOptions);40 const {file, commandArguments, options} = normalizeOptions(rawFile, rawArguments, syncOptions);41 validateSyncOptions(options);42 const fileDescriptors = handleStdioSync(options, verboseInfo);43 return {44 file,45 commandArguments,46 command,47 escapedCommand,48 startTime,49 verboseInfo,50 options,51 fileDescriptors,52 };100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 53–58why, one step back53};5455// Options normalization logic specific to sync methods56const normalizeSyncOptions = options => options.node && !options.ipc ? {...options, ipc: false} : options;5758// Options validation logic specific to sync methodsAdd `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec
line 59why, one step back59const validateSyncOptions = ({ipc, ipcInput, detached, cancelSignal, killDescendants}) => {Fix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a
lines 60–61why, one step back60 // Any value can be sent over IPC, including falsy ones, so only `undefined` means the option is not set61 if (ipcInput !== undefined) {Add `ipcInput` option (#1068)ehmickybefore adoption46cae30
lines 62–64why, one step back62 throwInvalidSyncOption('ipcInput');63 }64100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 65–70why, one step back65 if (ipc) {66 throwInvalidSyncOption('ipc: true');67 }6869 if (detached) {70 throwInvalidSyncOption('detached: true');Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec
lines 71–74why, one step back71 }7273 if (killDescendants) {74 throwInvalidSyncOption('killDescendants: true');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 75–107why, one step back75 }7677 if (cancelSignal) {78 throwInvalidSyncOption('cancelSignal');79 }80};8182const throwInvalidSyncOption = value => {83 throw new TypeError(`The "${value}" option cannot be used with synchronous methods.`);84};8586const spawnSubprocessSync = ({file, commandArguments, options, command, escapedCommand, verboseInfo, fileDescriptors, startTime}) => {87 const syncResult = runSubprocessSync({88 file,89 commandArguments,90 options,91 command,92 escapedCommand,93 fileDescriptors,94 startTime,95 });96 if (syncResult.failed) {97 return syncResult;98 }99100 const {resultError, exitCode, signal, timedOut, isMaxBuffer} = getExitResultSync(syncResult, options);101 const {output, error = resultError} = transformOutputSync({102 fileDescriptors,103 syncResult,104 options,105 verboseInfo,106 });107 const stdio = output.map((stdioOutput, fdNumber) => stripNewline(stdioOutput, options, fdNumber));Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
lines 108–109why, one step back108 const allFds = getAllSyncFds(output);109 const all = stripNewline(getAllSync(output, allFds, options), options, 'all', allFds);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 110–128why, one step back110 return getSyncResult({111 error,112 exitCode,113 signal,114 timedOut,115 isMaxBuffer,116 stdio,117 all,118 options,119 command,120 escapedCommand,121 startTime,122 });123};124125const runSubprocessSync = ({file, commandArguments, options, command, escapedCommand, fileDescriptors, startTime}) => {126 try {127 addInputOptionsSync(fileDescriptors, options);128 const normalizedOptions = normalizeSpawnSyncOptions(options);TweaksSindre Sorhusbefore adoption698a1b8
lines 129–130why, one step back129 // Keep the null prototype, otherwise a polluted `Object.prototype.failed` would make the result look like an early error130 return {__proto__: null, ...spawnSync(...concatenateShell(file, commandArguments, normalizedOptions))};100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 131–139why, one step back131 } catch (error) {132 return makeEarlyError({133 error,134 command,135 escapedCommand,136 fileDescriptors,137 options,138 startTime,139 isSync: true,Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
line 140why, one step back140 ...getEarlyErrorOutput(fileDescriptors, options),100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 141–153why, one step back141 });142 }143};144145// The `encoding` option is handled by Execa, not by `child_process.spawnSync()`146const normalizeSpawnSyncOptions = ({encoding, maxBuffer, ...options}) => ({...options, encoding: 'buffer', maxBuffer: getMaxBufferSync(maxBuffer)});147148const getSyncResult = ({error, exitCode, signal, timedOut, isMaxBuffer, stdio, all, options, command, escapedCommand, startTime}) => error === undefined149 ? makeSuccessResult({150 command,151 escapedCommand,152 stdio,153 all,Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 154why, one step back154 ipcOutput: [],100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 155–163why, one step back155 options,156 startTime,157 })158 : makeError({159 error,160 command,161 escapedCommand,162 timedOut,163 isCanceled: false,Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 164why, one step back164 isGracefullyCanceled: false,100% test coverage (#1018)ehmickybefore adoption9f65b86
line 165why, one step back165 isMaxBuffer,Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 166why, one step back166 isForcefullyTerminated: false,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 167–170why, one step back167 exitCode,168 signal,169 stdio,170 all,Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 171why, one step back171 ipcOutput: [],100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 172–175why, one step back172 options,173 startTime,174 isSync: true,175 });