options.js
This file
Path
lib/arguments/options.js
86 lines · 2.6 KiB · JavaScript
Last changed by
Improve validation and typing of signals (#1025)ehmickybefore adoption7213287
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687import {basename} from 'node:path';import process from 'node:process';import crossSpawn from 'cross-spawn';import {npmRunPathEnv} from 'npm-run-path';import {normalizeForceKillAfterDelay} from '../terminate/kill.js';import {normalizeKillSignal} from '../terminate/signal.js';import {validateTimeout} from '../terminate/timeout.js';import {handleNodeOption} from '../methods/node.js';import {validateEncoding, BINARY_ENCODINGS} from './encoding-option.js';import {normalizeCwd} from './cwd.js';import {normalizeFileUrl} from './file-url.js';import {normalizeFdSpecificOptions} from './specific.js';
// Normalize the options object, and sometimes also the file paths and arguments.// Applies default values, validate allowed options, normalize them.export const normalizeOptions = (filePath, rawArguments, rawOptions) => { rawOptions.cwd = normalizeCwd(rawOptions.cwd); const [processedFile, processedArguments, processedOptions] = handleNodeOption(filePath, rawArguments, rawOptions);
const {command: file, args: commandArguments, options: initialOptions} = crossSpawn._parse(processedFile, processedArguments, processedOptions);
const fdOptions = normalizeFdSpecificOptions(initialOptions); const options = addDefaultOptions(fdOptions); validateTimeout(options); validateEncoding(options); options.shell = normalizeFileUrl(options.shell); options.env = getEnv(options); options.killSignal = normalizeKillSignal(options.killSignal); options.forceKillAfterDelay = normalizeForceKillAfterDelay(options.forceKillAfterDelay); options.lines = options.lines.map((lines, fdNumber) => lines && !BINARY_ENCODINGS.has(options.encoding) && options.buffer[fdNumber]);
if (process.platform === 'win32' && basename(file, '.exe') === 'cmd') { // #116 commandArguments.unshift('/q'); }
return {file, commandArguments, options};};
const addDefaultOptions = ({ extendEnv = true, preferLocal = false, cwd, localDir: localDirectory = cwd, encoding = 'utf8', reject = true, cleanup = true, all = false, windowsHide = true, killSignal = 'SIGTERM', forceKillAfterDelay = true, ipc = false, serialization = 'advanced', ...options}) => ({ ...options, extendEnv, preferLocal, cwd, localDirectory, encoding, reject, cleanup, all, windowsHide, killSignal, forceKillAfterDelay, ipc, serialization,});
const getEnv = ({env: envOption, extendEnv, preferLocal, node, localDirectory, nodePath}) => { const env = extendEnv ? {...process.env, ...envOption} : envOption;
if (preferLocal || node) { return npmRunPathEnv({ env, cwd: localDirectory, execPath: nodePath, preferLocal, addExecPath: node, }); }
return env;};