Skip to content
Myr

main-sync.js

This file

Path

lib/methods/main-sync.js

169 lines · 5.0 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

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';

Rename files (#977)ehmickybefore adoption4b2e316

lines 5–11why, one step back
5import {makeError, makeEarlyError, makeSuccessResult} from '../return/result.js';
6import {handleResult} from '../return/reject.js';7import {handleStdioSync} from '../stdio/handle-sync.js';8import {stripNewline} from '../io/strip-newline.js';9import {addInputOptionsSync} from '../io/input-sync.js';10import {transformOutputSync} from '../io/output-sync.js';11import {getMaxBufferSync} from '../io/max-buffer.js';

Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f

line 12why, one step back
12import {getAllSync, getAllSyncFds} from '../resolve/all-sync.js';

Rename files (#977)ehmickybefore adoption4b2e316

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

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

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 16–27why, one step back
16export 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 back
28 return handleResult(result, verboseInfo, options);
29};30

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 32–33why, one step back
32const handleSyncArguments = (rawFile, rawArguments, rawOptions) => {
33 const {command, escapedCommand, startTime, verboseInfo} = handleCommand(rawFile, rawArguments, rawOptions);

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 34–47why, one step back
34 const syncOptions = normalizeSyncOptions(rawOptions);
35 const {file, commandArguments, options} = normalizeOptions(rawFile, rawArguments, syncOptions);36 validateSyncOptions(options);37 const fileDescriptors = handleStdioSync(options, verboseInfo);38 return {39 file,40 commandArguments,41 command,42 escapedCommand,43 startTime,44 verboseInfo,45 options,46 fileDescriptors,47 };

Rename files (#977)ehmickybefore adoption4b2e316

lines 48–49why, one step back
48};
49

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

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

Rename files (#977)ehmickybefore adoption4b2e316

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

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

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

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

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

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

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

Add `ipcInput` option (#1068)ehmickybefore adoption46cae30

lines 57–59why, one step back
57 throwInvalidSyncOption('ipcInput');
58 }59

Rename files (#977)ehmickybefore adoption4b2e316

lines 60–65why, one step back
60 if (ipc) {
61 throwInvalidSyncOption('ipc: true');62 }6364 if (detached) {65 throwInvalidSyncOption('detached: true');

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

lines 66–69why, one step back
66 }
6768 if (killDescendants) {69 throwInvalidSyncOption('killDescendants: true');

Rename files (#977)ehmickybefore adoption4b2e316

lines 70–80why, one step back
70 }
7172 if (cancelSignal) {73 throwInvalidSyncOption('cancelSignal');74 }75};7677const throwInvalidSyncOption = value => {78 throw new TypeError(`The "${value}" option cannot be used with synchronous methods.`);79};80

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 81–90why, one step back
81const spawnSubprocessSync = ({file, commandArguments, options, command, escapedCommand, verboseInfo, fileDescriptors, startTime}) => {
82 const syncResult = runSubprocessSync({83 file,84 commandArguments,85 options,86 command,87 escapedCommand,88 fileDescriptors,89 startTime,90 });

Rename files (#977)ehmickybefore adoption4b2e316

lines 91–95why, one step back
91 if (syncResult.failed) {
92 return syncResult;93 }9495 const {resultError, exitCode, signal, timedOut, isMaxBuffer} = getExitResultSync(syncResult, options);

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 96–101why, one step back
96 const {output, error = resultError} = transformOutputSync({
97 fileDescriptors,98 syncResult,99 options,100 verboseInfo,101 });

Rename files (#977)ehmickybefore adoption4b2e316

line 102why, one step back
102 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 103–104why, one step back
103 const allFds = getAllSyncFds(output);
104 const all = stripNewline(getAllSync(output, allFds, options), options, 'all', allFds);

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 105–117why, one step back
105 return getSyncResult({
106 error,107 exitCode,108 signal,109 timedOut,110 isMaxBuffer,111 stdio,112 all,113 options,114 command,115 escapedCommand,116 startTime,117 });

Rename files (#977)ehmickybefore adoption4b2e316

lines 118–119why, one step back
118};
119

Upgrade xo (#980)ehmickybefore adoptionccfa412

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

Rename files (#977)ehmickybefore adoption4b2e316

lines 121–123why, one step back
121 try {
122 addInputOptionsSync(fileDescriptors, options);123 const normalizedOptions = normalizeSpawnSyncOptions(options);

TweaksSindre Sorhusbefore adoption698a1b8

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

Rename files (#977)ehmickybefore adoption4b2e316

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 127–135why, one step back
127 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 back
136 }
137};138

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

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

Rename files (#977)ehmickybefore adoption4b2e316

lines 140–142why, one step back
140const normalizeSpawnSyncOptions = ({encoding, maxBuffer, ...options}) => ({...options, encoding: 'buffer', maxBuffer: getMaxBufferSync(maxBuffer)});
141142const getSyncResult = ({error, exitCode, signal, timedOut, isMaxBuffer, stdio, all, options, command, escapedCommand, startTime}) => error === undefined

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 143–147why, one step back
143 ? makeSuccessResult({
144 command,145 escapedCommand,146 stdio,147 all,

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

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 149–151why, one step back
149 options,
150 startTime,151 })

Rename files (#977)ehmickybefore adoption4b2e316

lines 152–157why, one step back
152 : makeError({
153 error,154 command,155 escapedCommand,156 timedOut,157 isCanceled: false,

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

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

Rename files (#977)ehmickybefore adoption4b2e316

line 159why, one step back
159 isMaxBuffer,

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

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

Rename files (#977)ehmickybefore adoption4b2e316

lines 161–164why, one step back
161 exitCode,
162 signal,163 stdio,164 all,

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

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

Rename files (#977)ehmickybefore adoption4b2e316

lines 166–169why, one step back
166 options,
167 startTime,168 isSync: true,169 });