main-sync.js
This file
Path
lib/methods/main-sync.js
162 lines · 4.5 KiB · JavaScript
Last changed by
Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
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–13why, 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 {getAllSync} from '../resolve/all-sync.js';12import {getExitResultSync} from '../resolve/exit-sync.js';13Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 14why, one step back14// Main shared logic for all sync methods: `execaSync()`, `$.sync()`Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 15–26why, one step back15export const execaCoreSync = (rawFile, rawArguments, rawOptions) => {16 const {file, commandArguments, command, escapedCommand, startTime, verboseInfo, options, fileDescriptors} = handleSyncArguments(rawFile, rawArguments, rawOptions);17 const result = spawnSubprocessSync({18 file,19 commandArguments,20 options,21 command,22 escapedCommand,23 verboseInfo,24 fileDescriptors,25 startTime,26 });Rename files (#977)ehmickybefore adoption4b2e316
lines 27–29why, one step back27 return handleResult(result, verboseInfo, options);28};29Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 30why, one step back30// Compute arguments to pass to `child_process.spawnSync()`Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 31–32why, one step back31const handleSyncArguments = (rawFile, rawArguments, rawOptions) => {32 const {command, escapedCommand, startTime, verboseInfo} = handleCommand(rawFile, rawArguments, rawOptions);Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
lines 33–46why, one step back33 const syncOptions = normalizeSyncOptions(rawOptions);34 const {file, commandArguments, options} = normalizeOptions(rawFile, rawArguments, syncOptions);35 validateSyncOptions(options);36 const fileDescriptors = handleStdioSync(options, verboseInfo);37 return {38 file,39 commandArguments,40 command,41 escapedCommand,42 startTime,43 verboseInfo,44 options,45 fileDescriptors,46 };Rename files (#977)ehmickybefore adoption4b2e316
lines 47–48why, one step back47};48Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 49why, one step back49// Options normalization logic specific to sync methodsRename files (#977)ehmickybefore adoption4b2e316
lines 50–51why, one step back50const normalizeSyncOptions = options => options.node && !options.ipc ? {...options, ipc: false} : options;51Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 52why, one step back52// Options validation logic specific to sync methodsAdd `ipcInput` option (#1068)ehmickybefore adoption46cae30
lines 53–57why, one step back53const validateSyncOptions = ({ipc, ipcInput, detached, cancelSignal}) => {54 if (ipcInput) {55 throwInvalidSyncOption('ipcInput');56 }57Rename files (#977)ehmickybefore adoption4b2e316
lines 58–74why, one step back58 if (ipc) {59 throwInvalidSyncOption('ipc: true');60 }6162 if (detached) {63 throwInvalidSyncOption('detached: true');64 }6566 if (cancelSignal) {67 throwInvalidSyncOption('cancelSignal');68 }69};7071const throwInvalidSyncOption = value => {72 throw new TypeError(`The "${value}" option cannot be used with synchronous methods.`);73};74Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 75–84why, one step back75const spawnSubprocessSync = ({file, commandArguments, options, command, escapedCommand, verboseInfo, fileDescriptors, startTime}) => {76 const syncResult = runSubprocessSync({77 file,78 commandArguments,79 options,80 command,81 escapedCommand,82 fileDescriptors,83 startTime,84 });Rename files (#977)ehmickybefore adoption4b2e316
lines 85–89why, one step back85 if (syncResult.failed) {86 return syncResult;87 }8889 const {resultError, exitCode, signal, timedOut, isMaxBuffer} = getExitResultSync(syncResult, options);Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 90–96why, one step back90 const {output, error = resultError} = transformOutputSync({91 fileDescriptors,92 syncResult,93 options,94 isMaxBuffer,95 verboseInfo,96 });Rename files (#977)ehmickybefore adoption4b2e316
lines 97–98why, one step back97 const stdio = output.map((stdioOutput, fdNumber) => stripNewline(stdioOutput, options, fdNumber));98 const all = stripNewline(getAllSync(output, options), options, 'all');Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 99–111why, one step back99 return getSyncResult({100 error,101 exitCode,102 signal,103 timedOut,104 isMaxBuffer,105 stdio,106 all,107 options,108 command,109 escapedCommand,110 startTime,111 });Rename files (#977)ehmickybefore adoption4b2e316
lines 112–113why, one step back112};113Upgrade xo (#980)ehmickybefore adoptionccfa412
line 114why, one step back114const runSubprocessSync = ({file, commandArguments, options, command, escapedCommand, fileDescriptors, startTime}) => {Rename files (#977)ehmickybefore adoption4b2e316
lines 115–117why, one step back115 try {116 addInputOptionsSync(fileDescriptors, options);117 const normalizedOptions = normalizeSpawnSyncOptions(options);Upgrade xo (#980)ehmickybefore adoptionccfa412
line 118why, one step back118 return spawnSync(file, commandArguments, normalizedOptions);Rename files (#977)ehmickybefore adoption4b2e316
line 119why, one step back119 } catch (error) {Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 120–128why, one step back120 return makeEarlyError({121 error,122 command,123 escapedCommand,124 fileDescriptors,125 options,126 startTime,127 isSync: true,128 });Rename files (#977)ehmickybefore adoption4b2e316
lines 129–131why, one step back129 }130};131Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 132why, one step back132// The `encoding` option is handled by Execa, not by `child_process.spawnSync()`Rename files (#977)ehmickybefore adoption4b2e316
lines 133–135why, one step back133const normalizeSpawnSyncOptions = ({encoding, maxBuffer, ...options}) => ({...options, encoding: 'buffer', maxBuffer: getMaxBufferSync(maxBuffer)});134135const getSyncResult = ({error, exitCode, signal, timedOut, isMaxBuffer, stdio, all, options, command, escapedCommand, startTime}) => error === undefinedUpgrade xo (#980)ehmickybefore adoptionccfa412
lines 136–140why, one step back136 ? makeSuccessResult({137 command,138 escapedCommand,139 stdio,140 all,Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 141why, one step back141 ipcOutput: [],Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 142–144why, one step back142 options,143 startTime,144 })Rename files (#977)ehmickybefore adoption4b2e316
lines 145–150why, one step back145 : makeError({146 error,147 command,148 escapedCommand,149 timedOut,150 isCanceled: false,Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 151why, one step back151 isGracefullyCanceled: false,Rename files (#977)ehmickybefore adoption4b2e316
line 152why, one step back152 isMaxBuffer,Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 153why, one step back153 isForcefullyTerminated: false,Rename files (#977)ehmickybefore adoption4b2e316
lines 154–157why, one step back154 exitCode,155 signal,156 stdio,157 all,Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 158why, one step back158 ipcOutput: [],Rename files (#977)ehmickybefore adoption4b2e316
lines 159–162why, one step back159 options,160 startTime,161 isSync: true,162 });