sync.js
This file
Path
lib/sync.js
90 lines · 3.5 KiB · JavaScript
Last changed by
Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091import {spawnSync} from 'node:child_process';import {handleCommand, handleOptions} from './arguments/options.js';import {makeError, makeEarlyError, makeSuccessResult} from './return/error.js';import {handleOutput, handleResult} from './return/output.js';import {handleInputSync} from './stdio/sync.js';import {addInputOptionsSync} from './stdio/input-sync.js';import {transformOutputSync} from './stdio/output-sync.js';import {logEarlyResult} from './verbose/complete.js';import {getSyncExitResult} from './exit/code.js';
export const execaCoreSync = (rawFile, rawArgs, rawOptions) => { const {file, args, command, escapedCommand, startTime, verboseInfo, options, fileDescriptors} = handleSyncArguments(rawFile, rawArgs, rawOptions); const result = spawnSubprocessSync({file, args, options, command, escapedCommand, fileDescriptors, startTime}); return handleResult(result, verboseInfo, options);};
const handleSyncArguments = (rawFile, rawArgs, rawOptions) => { const {command, escapedCommand, startTime, verboseInfo} = handleCommand(rawFile, rawArgs, rawOptions);
try { const syncOptions = normalizeSyncOptions(rawOptions); const {file, args, options} = handleOptions(rawFile, rawArgs, syncOptions); validateSyncOptions(options); const fileDescriptors = handleInputSync(options, verboseInfo); return {file, args, command, escapedCommand, startTime, verboseInfo, options, fileDescriptors}; } catch (error) { logEarlyResult(error, startTime, verboseInfo); throw error; }};
const normalizeSyncOptions = options => options.node && !options.ipc ? {...options, ipc: false} : options;
const validateSyncOptions = ({ipc, detached, cancelSignal}) => { if (ipc) { throwInvalidSyncOption('ipc: true'); }
if (detached) { throwInvalidSyncOption('detached: true'); }
if (cancelSignal) { throwInvalidSyncOption('cancelSignal'); }};
const throwInvalidSyncOption = value => { throw new TypeError(`The "${value}" option cannot be used with synchronous methods.`);};
const spawnSubprocessSync = ({file, args, options, command, escapedCommand, fileDescriptors, startTime}) => { const syncResult = runSubprocessSync({file, args, options, command, escapedCommand, fileDescriptors, startTime}); if (syncResult.failed) { return syncResult; }
const {resultError, exitCode, signal} = getSyncExitResult(syncResult); const {output, error = resultError} = transformOutputSync(fileDescriptors, syncResult, options); const stdio = output.map(stdioOutput => handleOutput(options, stdioOutput)); return getSyncResult({error, exitCode, signal, stdio, options, command, escapedCommand, startTime});};
const runSubprocessSync = ({file, args, options, command, escapedCommand, fileDescriptors, startTime}) => { try { addInputOptionsSync(fileDescriptors, options); const normalizedOptions = normalizeSpawnSyncOptions(options); return spawnSync(file, args, normalizedOptions); } catch (error) { return makeEarlyError({error, command, escapedCommand, fileDescriptors, options, startTime, isSync: true}); }};
const normalizeSpawnSyncOptions = ({encoding, ...options}) => ({...options, encoding: 'buffer'});
const getSyncResult = ({error, exitCode, signal, stdio, options, command, escapedCommand, startTime}) => error === undefined ? makeSuccessResult({command, escapedCommand, stdio, options, startTime}) : makeError({ error, command, escapedCommand, timedOut: error.code === 'ETIMEDOUT', isCanceled: false, exitCode, signal, stdio, options, startTime, isSync: true, });