main-sync.js
This file
Path
lib/methods/main-sync.js
167 lines · 4.6 KiB · JavaScript
Last changed by
Add `ipcInput` option (#1068)ehmickybefore adoption46cae30
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–2why, one step back1import {spawnSync} from 'node:child_process';2import {handleCommand} from '../arguments/command.js';Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
line 3why, one step back3import {normalizeOptions} from '../arguments/options.js';Rename files (#977)ehmickybefore adoption4b2e316
lines 4–14why, one step back4import {makeError, makeEarlyError, makeSuccessResult} from '../return/result.js';5import {handleResult} from '../return/reject.js';6import {handleStdioSync} from '../stdio/handle-sync.js';7import {stripNewline} from '../io/strip-newline.js';8import {addInputOptionsSync} from '../io/input-sync.js';9import {transformOutputSync} from '../io/output-sync.js';10import {getMaxBufferSync} from '../io/max-buffer.js';11import {logEarlyResult} from '../verbose/complete.js';12import {getAllSync} from '../resolve/all-sync.js';13import {getExitResultSync} from '../resolve/exit-sync.js';14Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 15why, one step back15// Main shared logic for all sync methods: `execaSync()`, `$.sync()`Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 16–27why, one step back16export const execaCoreSync = (rawFile, rawArguments, rawOptions) => {17 const {file, commandArguments, command, escapedCommand, startTime, verboseInfo, options, fileDescriptors} = handleSyncArguments(rawFile, rawArguments, rawOptions);18 const result = spawnSubprocessSync({19 file,20 commandArguments,21 options,22 command,23 escapedCommand,24 verboseInfo,25 fileDescriptors,26 startTime,27 });Rename files (#977)ehmickybefore adoption4b2e316
lines 28–30why, one step back28 return handleResult(result, verboseInfo, options);29};30Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 31why, one step back31// Compute arguments to pass to `child_process.spawnSync()`Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 32–33why, one step back32const handleSyncArguments = (rawFile, rawArguments, rawOptions) => {33 const {command, escapedCommand, startTime, verboseInfo} = handleCommand(rawFile, rawArguments, rawOptions);Rename files (#977)ehmickybefore adoption4b2e316
lines 34–36why, one step back3435 try {36 const syncOptions = normalizeSyncOptions(rawOptions);Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
line 37why, one step back37 const {file, commandArguments, options} = normalizeOptions(rawFile, rawArguments, syncOptions);Rename files (#977)ehmickybefore adoption4b2e316
lines 38–39why, one step back38 validateSyncOptions(options);39 const fileDescriptors = handleStdioSync(options, verboseInfo);Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 40–49why, one step back40 return {41 file,42 commandArguments,43 command,44 escapedCommand,45 startTime,46 verboseInfo,47 options,48 fileDescriptors,49 };Rename files (#977)ehmickybefore adoption4b2e316
lines 50–55why, one step back50 } catch (error) {51 logEarlyResult(error, startTime, verboseInfo);52 throw error;53 }54};55Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 56why, one step back56// Options normalization logic specific to sync methodsRename files (#977)ehmickybefore adoption4b2e316
lines 57–58why, one step back57const normalizeSyncOptions = options => options.node && !options.ipc ? {...options, ipc: false} : options;58Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 59why, one step back59// Options validation logic specific to sync methodsAdd `ipcInput` option (#1068)ehmickybefore adoption46cae30
lines 60–64why, one step back60const validateSyncOptions = ({ipc, ipcInput, detached, cancelSignal}) => {61 if (ipcInput) {62 throwInvalidSyncOption('ipcInput');63 }64Rename files (#977)ehmickybefore adoption4b2e316
lines 65–81why, one step back65 if (ipc) {66 throwInvalidSyncOption('ipc: true');67 }6869 if (detached) {70 throwInvalidSyncOption('detached: true');71 }7273 if (cancelSignal) {74 throwInvalidSyncOption('cancelSignal');75 }76};7778const throwInvalidSyncOption = value => {79 throw new TypeError(`The "${value}" option cannot be used with synchronous methods.`);80};81Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 82–91why, one step back82const spawnSubprocessSync = ({file, commandArguments, options, command, escapedCommand, verboseInfo, fileDescriptors, startTime}) => {83 const syncResult = runSubprocessSync({84 file,85 commandArguments,86 options,87 command,88 escapedCommand,89 fileDescriptors,90 startTime,91 });Rename files (#977)ehmickybefore adoption4b2e316
lines 92–96why, one step back92 if (syncResult.failed) {93 return syncResult;94 }9596 const {resultError, exitCode, signal, timedOut, isMaxBuffer} = getExitResultSync(syncResult, options);Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 97–103why, one step back97 const {output, error = resultError} = transformOutputSync({98 fileDescriptors,99 syncResult,100 options,101 isMaxBuffer,102 verboseInfo,103 });Rename files (#977)ehmickybefore adoption4b2e316
lines 104–105why, one step back104 const stdio = output.map((stdioOutput, fdNumber) => stripNewline(stdioOutput, options, fdNumber));105 const all = stripNewline(getAllSync(output, options), options, 'all');Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 106–118why, one step back106 return getSyncResult({107 error,108 exitCode,109 signal,110 timedOut,111 isMaxBuffer,112 stdio,113 all,114 options,115 command,116 escapedCommand,117 startTime,118 });Rename files (#977)ehmickybefore adoption4b2e316
lines 119–120why, one step back119};120Upgrade xo (#980)ehmickybefore adoptionccfa412
line 121why, one step back121const runSubprocessSync = ({file, commandArguments, options, command, escapedCommand, fileDescriptors, startTime}) => {Rename files (#977)ehmickybefore adoption4b2e316
lines 122–124why, one step back122 try {123 addInputOptionsSync(fileDescriptors, options);124 const normalizedOptions = normalizeSpawnSyncOptions(options);Upgrade xo (#980)ehmickybefore adoptionccfa412
line 125why, one step back125 return spawnSync(file, commandArguments, normalizedOptions);Rename files (#977)ehmickybefore adoption4b2e316
line 126why, one step back126 } catch (error) {Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 127–135why, one step back127 return makeEarlyError({128 error,129 command,130 escapedCommand,131 fileDescriptors,132 options,133 startTime,134 isSync: true,135 });Rename files (#977)ehmickybefore adoption4b2e316
lines 136–138why, one step back136 }137};138Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 139why, one step back139// The `encoding` option is handled by Execa, not by `child_process.spawnSync()`Rename files (#977)ehmickybefore adoption4b2e316
lines 140–142why, one step back140const normalizeSpawnSyncOptions = ({encoding, maxBuffer, ...options}) => ({...options, encoding: 'buffer', maxBuffer: getMaxBufferSync(maxBuffer)});141142const getSyncResult = ({error, exitCode, signal, timedOut, isMaxBuffer, stdio, all, options, command, escapedCommand, startTime}) => error === undefinedUpgrade xo (#980)ehmickybefore adoptionccfa412
lines 143–147why, one step back143 ? makeSuccessResult({144 command,145 escapedCommand,146 stdio,147 all,Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 148why, one step back148 ipcMessages: [],Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 149–151why, one step back149 options,150 startTime,151 })Rename files (#977)ehmickybefore adoption4b2e316
lines 152–162why, one step back152 : makeError({153 error,154 command,155 escapedCommand,156 timedOut,157 isCanceled: false,158 isMaxBuffer,159 exitCode,160 signal,161 stdio,162 all,Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 163why, one step back163 ipcMessages: [],Rename files (#977)ehmickybefore adoption4b2e316
lines 164–167why, one step back164 options,165 startTime,166 isSync: true,167 });