Skip to content
Myr

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

Rename files (#977)ehmickybefore adoption4b2e316

lines 4–14why, one step back
4import {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';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);

Rename files (#977)ehmickybefore adoption4b2e316

lines 34–36why, one step back
34
35 try {36 const syncOptions = normalizeSyncOptions(rawOptions);

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

line 37why, one step back
37 const {file, commandArguments, options} = normalizeOptions(rawFile, rawArguments, syncOptions);

Rename files (#977)ehmickybefore adoption4b2e316

lines 38–39why, one step back
38 validateSyncOptions(options);
39 const fileDescriptors = handleStdioSync(options, verboseInfo);

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 40–49why, one step back
40 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 back
50 } catch (error) {
51 logEarlyResult(error, startTime, verboseInfo);52 throw error;53 }54};55

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

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

Rename files (#977)ehmickybefore adoption4b2e316

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

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

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

Add `ipcInput` option (#1068)ehmickybefore adoption46cae30

lines 60–64why, one step back
60const validateSyncOptions = ({ipc, ipcInput, detached, cancelSignal}) => {
61 if (ipcInput) {62 throwInvalidSyncOption('ipcInput');63 }64

Rename files (#977)ehmickybefore adoption4b2e316

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 82–91why, one step back
82const 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 back
92 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 back
97 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 back
104 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 back
106 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 back
119};
120

Upgrade xo (#980)ehmickybefore adoptionccfa412

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

Rename files (#977)ehmickybefore adoption4b2e316

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 125why, one step back
125 return spawnSync(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,

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

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

Rename files (#977)ehmickybefore adoption4b2e316

lines 152–162why, one step back
152 : 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 back
163 ipcMessages: [],

Rename files (#977)ehmickybefore adoption4b2e316

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