Skip to content
Myr

main-sync.js

This file

Path

lib/methods/main-sync.js

167 lines · 4.7 KiB · JavaScript

Last changed by

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

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–14why, 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';12import {getAllSync} from '../resolve/all-sync.js';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}) => {

Add `ipcInput` option (#1068)ehmickybefore adoption46cae30

lines 55–58why, one step back
55 if (ipcInput) {
56 throwInvalidSyncOption('ipcInput');57 }58

Rename files (#977)ehmickybefore adoption4b2e316

lines 59–64why, one step back
59 if (ipc) {
60 throwInvalidSyncOption('ipc: true');61 }6263 if (detached) {64 throwInvalidSyncOption('detached: true');

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

lines 65–68why, one step back
65 }
6667 if (killDescendants) {68 throwInvalidSyncOption('killDescendants: true');

Rename files (#977)ehmickybefore adoption4b2e316

lines 69–79why, one step back
69 }
7071 if (cancelSignal) {72 throwInvalidSyncOption('cancelSignal');73 }74};7576const throwInvalidSyncOption = value => {77 throw new TypeError(`The "${value}" option cannot be used with synchronous methods.`);78};79

Upgrade xo (#980)ehmickybefore adoptionccfa412

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

Rename files (#977)ehmickybefore adoption4b2e316

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

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

Rename files (#977)ehmickybefore adoption4b2e316

lines 102–103why, one step back
102 const stdio = output.map((stdioOutput, fdNumber) => stripNewline(stdioOutput, options, fdNumber));
103 const all = stripNewline(getAllSync(output, options), options, 'all');

Upgrade xo (#980)ehmickybefore adoptionccfa412

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

Rename files (#977)ehmickybefore adoption4b2e316

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

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

Rename files (#977)ehmickybefore adoption4b2e316

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

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

line 123why, one step back
123 return spawnSync(...concatenateShell(file, commandArguments, normalizedOptions));

Rename files (#977)ehmickybefore adoption4b2e316

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 125–133why, one step back
125 return makeEarlyError({
126 error,127 command,128 escapedCommand,129 fileDescriptors,130 options,131 startTime,132 isSync: true,133 });

Rename files (#977)ehmickybefore adoption4b2e316

lines 134–136why, one step back
134 }
135};136

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

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

Rename files (#977)ehmickybefore adoption4b2e316

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

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

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

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 147–149why, one step back
147 options,
148 startTime,149 })

Rename files (#977)ehmickybefore adoption4b2e316

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

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

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

Rename files (#977)ehmickybefore adoption4b2e316

line 157why, one step back
157 isMaxBuffer,

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

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

Rename files (#977)ehmickybefore adoption4b2e316

lines 159–162why, one step back
159 exitCode,
160 signal,161 stdio,162 all,

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

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

Rename files (#977)ehmickybefore adoption4b2e316

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