Skip to content
Myr

sync.js

This file

Path

lib/sync.js

67 lines · 2.1 KiB · JavaScript

Last changed by

Improve spawning errors (#847)ehmickybefore adoption381dee2

History of this file

Take it

Improve spawning errors (#847)ehmickybefore adoption381dee2

line 1why, one step back
1import {spawnSync} from 'node:child_process';

Improve input validation (#838)ehmickybefore adoption8ee137c

line 2why, one step back
2import {normalizeArguments, handleArguments} from './arguments/options.js';

Split files (#837)ehmickybefore adoption672c2d7

lines 3–13why, one step back
3import {makeError, makeEarlyError, makeSuccessResult} from './return/error.js';
4import {handleOutput} from './return/output.js';5import {handleInputSync, pipeOutputSync} from './stdio/sync.js';6import {isFailedExit} from './exit/code.js';78export const execaSync = (rawFile, rawArgs, rawOptions) => {9 const {file, args, command, escapedCommand, options} = handleSyncArguments(rawFile, rawArgs, rawOptions);10 const stdioStreamsGroups = handleInputSync(options);1112 let result;13 try {

Improve spawning errors (#847)ehmickybefore adoption381dee2

line 14why, one step back
14 result = spawnSync(file, args, options);

Split files (#837)ehmickybefore adoption672c2d7

lines 15–53why, one step back
15 } catch (error) {
16 const errorInstance = makeEarlyError({error, command, escapedCommand, stdioStreamsGroups, options});1718 if (!options.reject) {19 return errorInstance;20 }2122 throw errorInstance;23 }2425 pipeOutputSync(stdioStreamsGroups, result);2627 const output = result.output || Array.from({length: 3});28 const stdio = output.map(stdioOutput => handleOutput(options, stdioOutput));2930 if (result.error !== undefined || isFailedExit(result.status, result.signal)) {31 const error = makeError({32 error: result.error,33 command,34 escapedCommand,35 timedOut: result.error && result.error.code === 'ETIMEDOUT',36 isCanceled: false,37 exitCode: result.status,38 signal: result.signal,39 stdio,40 options,41 });4243 if (!options.reject) {44 return error;45 }4647 throw error;48 }4950 return makeSuccessResult({command, escapedCommand, stdio, options});51};5253const handleSyncArguments = (rawFile, rawArgs, rawOptions) => {

Improve input validation (#838)ehmickybefore adoption8ee137c

line 54why, one step back
54 [rawFile, rawArgs, rawOptions] = normalizeArguments(rawFile, rawArgs, rawOptions);

Split files (#837)ehmickybefore adoption672c2d7

lines 55–67why, one step back
55 const syncOptions = normalizeSyncOptions(rawOptions);
56 const {file, args, command, escapedCommand, options} = handleArguments(rawFile, rawArgs, syncOptions);57 validateSyncOptions(options);58 return {file, args, command, escapedCommand, options};59};6061const normalizeSyncOptions = options => options.node && !options.ipc ? {...options, ipc: false} : options;6263const validateSyncOptions = ({ipc}) => {64 if (ipc) {65 throw new TypeError('The "ipc: true" option cannot be used with synchronous methods.');66 }67};