Skip to content
Myr

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 file

Take it

Rename files (#977)ehmickybefore adoption4b2e316

lines 1–2why, one step back
1import {spawnSync} from 'node:child_process';
2import {handleCommand} from '../arguments/command.js';

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

line 3why, one step back
3import {normalizeOptions} from '../arguments/options.js';

Fix Node 24-specific deprecation warning (#1199)ehmickybefore adoption1ac5b91

line 4why, one step back
4import {concatenateShell} from '../arguments/shell.js';

Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff

lines 5–10why, one step back
5import {
6 makeError,7 makeEarlyError,8 makeSuccessResult,9 getEarlyErrorOutput,10} from '../return/result.js';

Rename files (#977)ehmickybefore adoption4b2e316

lines 11–16why, one step back
11import {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 back
17import {getAllSync, getAllSyncFds} from '../resolve/all-sync.js';

Rename files (#977)ehmickybefore adoption4b2e316

lines 18–19why, one step back
18import {getExitResultSync} from '../resolve/exit-sync.js';
19

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 20why, one step back
20// Main shared logic for all sync methods: `execaSync()`, `$.sync()`

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 21–32why, one step back
21export 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 });

Rename files (#977)ehmickybefore adoption4b2e316

lines 33–35why, one step back
33 return handleResult(result, verboseInfo, options);
34};35

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 36why, one step back
36// Compute arguments to pass to `child_process.spawnSync()`

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 37–38why, one step back
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 back
39 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 };

Rename files (#977)ehmickybefore adoption4b2e316

lines 53–54why, one step back
53};
54

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 55why, one step back
55// Options normalization logic specific to sync methods

Rename files (#977)ehmickybefore adoption4b2e316

lines 56–57why, one step back
56const normalizeSyncOptions = options => options.node && !options.ipc ? {...options, ipc: false} : options;
57

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 58why, one step back
58// Options validation logic specific to sync methods

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

line 59why, one step back
59const validateSyncOptions = ({ipc, ipcInput, detached, cancelSignal, killDescendants}) => {

Fix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a

lines 60–61why, one step back
60 // Any value can be sent over IPC, including falsy ones, so only `undefined` means the option is not set
61 if (ipcInput !== undefined) {

Add `ipcInput` option (#1068)ehmickybefore adoption46cae30

lines 62–64why, one step back
62 throwInvalidSyncOption('ipcInput');
63 }64

Rename files (#977)ehmickybefore adoption4b2e316

lines 65–70why, one step back
65 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 back
71 }
7273 if (killDescendants) {74 throwInvalidSyncOption('killDescendants: true');

Rename files (#977)ehmickybefore adoption4b2e316

lines 75–85why, one step back
75 }
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};85

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 86–95why, one step back
86const 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 });

Rename files (#977)ehmickybefore adoption4b2e316

lines 96–100why, one step back
96 if (syncResult.failed) {
97 return syncResult;98 }99100 const {resultError, exitCode, signal, timedOut, isMaxBuffer} = getExitResultSync(syncResult, options);

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 101–106why, one step back
101 const {output, error = resultError} = transformOutputSync({
102 fileDescriptors,103 syncResult,104 options,105 verboseInfo,106 });

Rename files (#977)ehmickybefore adoption4b2e316

line 107why, one step back
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 back
108 const allFds = getAllSyncFds(output);
109 const all = stripNewline(getAllSync(output, allFds, options), options, 'all', allFds);

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 110–122why, one step back
110 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 });

Rename files (#977)ehmickybefore adoption4b2e316

lines 123–124why, one step back
123};
124

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 125why, one step back
125const runSubprocessSync = ({file, commandArguments, options, command, escapedCommand, fileDescriptors, startTime}) => {

Rename files (#977)ehmickybefore adoption4b2e316

lines 126–128why, one step back
126 try {
127 addInputOptionsSync(fileDescriptors, options);128 const normalizedOptions = normalizeSpawnSyncOptions(options);

TweaksSindre Sorhusbefore adoption698a1b8

lines 129–130why, one step back
129 // Keep the null prototype, otherwise a polluted `Object.prototype.failed` would make the result look like an early error
130 return {__proto__: null, ...spawnSync(...concatenateShell(file, commandArguments, normalizedOptions))};

Rename files (#977)ehmickybefore adoption4b2e316

line 131why, one step back
131 } catch (error) {

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 132–139why, one step back
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 back
140 ...getEarlyErrorOutput(fileDescriptors, options),

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 141why, one step back
141 });

Rename files (#977)ehmickybefore adoption4b2e316

lines 142–144why, one step back
142 }
143};144

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 145why, one step back
145// The `encoding` option is handled by Execa, not by `child_process.spawnSync()`

Rename files (#977)ehmickybefore adoption4b2e316

lines 146–148why, one step back
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 === undefined

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 149–153why, one step back
149 ? makeSuccessResult({
150 command,151 escapedCommand,152 stdio,153 all,

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 154why, one step back
154 ipcOutput: [],

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 155–157why, one step back
155 options,
156 startTime,157 })

Rename files (#977)ehmickybefore adoption4b2e316

lines 158–163why, one step back
158 : makeError({
159 error,160 command,161 escapedCommand,162 timedOut,163 isCanceled: false,

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 164why, one step back
164 isGracefullyCanceled: false,

Rename files (#977)ehmickybefore adoption4b2e316

line 165why, one step back
165 isMaxBuffer,

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 166why, one step back
166 isForcefullyTerminated: false,

Rename files (#977)ehmickybefore adoption4b2e316

lines 167–170why, one step back
167 exitCode,
168 signal,169 stdio,170 all,

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 171why, one step back
171 ipcOutput: [],

Rename files (#977)ehmickybefore adoption4b2e316

lines 172–175why, one step back
172 options,
173 startTime,174 isSync: true,175 });