Skip to content
Myr

main-sync.js

This file

Path

lib/methods/main-sync.js

161 lines · 4.5 KiB · JavaScript

Last changed by

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

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 more code comments (#984)ehmickybefore adoptionfdd8a9f

line 15why, one step back
15// Main shared logic for all sync methods: `execaSync()`, `execaCommandSync()`, `$.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

Rename files (#977)ehmickybefore adoption4b2e316

lines 60–77why, one step back
60const validateSyncOptions = ({ipc, detached, cancelSignal}) => {
61 if (ipc) {62 throwInvalidSyncOption('ipc: true');63 }6465 if (detached) {66 throwInvalidSyncOption('detached: true');67 }6869 if (cancelSignal) {70 throwInvalidSyncOption('cancelSignal');71 }72};7374const throwInvalidSyncOption = value => {75 throw new TypeError(`The "${value}" option cannot be used with synchronous methods.`);76};77

Upgrade xo (#980)ehmickybefore adoptionccfa412

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

Rename files (#977)ehmickybefore adoption4b2e316

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

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

Rename files (#977)ehmickybefore adoption4b2e316

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

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

Rename files (#977)ehmickybefore adoption4b2e316

lines 115–116why, one step back
115};
116

Upgrade xo (#980)ehmickybefore adoptionccfa412

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

Rename files (#977)ehmickybefore adoption4b2e316

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 121why, one step back
121 return spawnSync(file, commandArguments, normalizedOptions);

Rename files (#977)ehmickybefore adoption4b2e316

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

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

Rename files (#977)ehmickybefore adoption4b2e316

lines 132–134why, one step back
132 }
133};134

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

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

Rename files (#977)ehmickybefore adoption4b2e316

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

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 139–146why, one step back
139 ? makeSuccessResult({
140 command,141 escapedCommand,142 stdio,143 all,144 options,145 startTime,146 })

Rename files (#977)ehmickybefore adoption4b2e316

lines 147–161why, one step back
147 : makeError({
148 error,149 command,150 escapedCommand,151 timedOut,152 isCanceled: false,153 isMaxBuffer,154 exitCode,155 signal,156 stdio,157 all,158 options,159 startTime,160 isSync: true,161 });