Skip to content
Myr

options.js

This file

Path

lib/arguments/options.js

123 lines · 3.7 KiB · JavaScript

Last changed by

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

History of this file

Take it

Split files (#837)ehmickybefore adoption672c2d7

lines 1–4why, one step back
1import {basename} from 'node:path';
2import process from 'node:process';3import crossSpawn from 'cross-spawn';4import {npmRunPathEnv} from 'npm-run-path';

Improve input validation (#838)ehmickybefore adoption8ee137c

line 5why, one step back
5import isPlainObject from 'is-plain-obj';

Split files (#837)ehmickybefore adoption672c2d7

lines 6–7why, one step back
6import {normalizeForceKillAfterDelay} from '../exit/kill.js';
7import {validateTimeout} from '../exit/timeout.js';

Improve `verbose` option (#883)ehmickybefore adoption7d0943f

line 8why, one step back
8import {logCommand} from '../verbose/start.js';

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

line 9why, one step back
9import {getVerboseInfo} from '../verbose/info.js';

Add `result|error.durationMs` (#896)ehmickybefore adoption7088091

line 10why, one step back
10import {getStartTime} from '../return/duration.js';

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 11why, one step back
11import {validateEncoding, BINARY_ENCODINGS} from './encoding.js';

Split files (#837)ehmickybefore adoption672c2d7

lines 12–15why, one step back
12import {handleNodeOption} from './node.js';
13import {joinCommand} from './escape.js';14import {normalizeCwd, safeNormalizeFileUrl, normalizeFileUrl} from './cwd.js';15

Improve input validation (#838)ehmickybefore adoption8ee137c

line 16why, one step back
16export const normalizeArguments = (rawFile, rawArgs = [], rawOptions = {}) => {

Split files (#837)ehmickybefore adoption672c2d7

line 17why, one step back
17 const filePath = safeNormalizeFileUrl(rawFile, 'First argument');

Improve input validation (#838)ehmickybefore adoption8ee137c

lines 18–25why, one step back
18 const [args, options] = isPlainObject(rawArgs)
19 ? [[], rawArgs]20 : [rawArgs, rawOptions];2122 if (!Array.isArray(args)) {23 throw new TypeError(`Second argument must be either an array of arguments or an options object: ${args}`);24 }25

Improve process arguments validation (#873)ehmickybefore adoption54b5ea7

lines 26–35why, one step back
26 if (args.some(arg => typeof arg === 'object' && arg !== null)) {
27 throw new TypeError(`Second argument must be an array of strings: ${args}`);28 }2930 const normalizedArgs = args.map(String);31 const nullByteArg = normalizedArgs.find(arg => arg.includes('\0'));32 if (nullByteArg !== undefined) {33 throw new TypeError(`Arguments cannot contain null bytes ("\\0"): ${nullByteArg}`);34 }35

Improve input validation (#838)ehmickybefore adoption8ee137c

lines 36–39why, one step back
36 if (!isPlainObject(options)) {
37 throw new TypeError(`Last argument must be an options object: ${options}`);38 }39

Improve process arguments validation (#873)ehmickybefore adoption54b5ea7

line 40why, one step back
40 return [filePath, normalizedArgs, options];

Improve input validation (#838)ehmickybefore adoption8ee137c

lines 41–42why, one step back
41};
42

Improve `verbose` option (#883)ehmickybefore adoption7d0943f

line 43why, one step back
43export const handleCommand = (filePath, rawArgs, rawOptions) => {

Add `result|error.durationMs` (#896)ehmickybefore adoption7088091

line 44why, one step back
44 const startTime = getStartTime();

Split files (#837)ehmickybefore adoption672c2d7

line 45why, one step back
45 const {command, escapedCommand} = joinCommand(filePath, rawArgs);

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

lines 46–47why, one step back
46 const verboseInfo = getVerboseInfo(rawOptions);
47 logCommand(escapedCommand, verboseInfo, rawOptions);

Add `result|error.durationMs` (#896)ehmickybefore adoption7088091

line 48why, one step back
48 return {command, escapedCommand, startTime, verboseInfo};

Improve `verbose` option (#883)ehmickybefore adoption7d0943f

line 49why, one step back
49};

Split files (#837)ehmickybefore adoption672c2d7

line 50why, one step back
50

Improve `verbose` option (#883)ehmickybefore adoption7d0943f

line 51why, one step back
51export const handleArguments = (filePath, rawArgs, rawOptions) => {

Split files (#837)ehmickybefore adoption672c2d7

lines 52–58why, one step back
52 rawOptions.cwd = normalizeCwd(rawOptions.cwd);
53 const [processedFile, processedArgs, processedOptions] = handleNodeOption(filePath, rawArgs, rawOptions);5455 const {command: file, args, options: initialOptions} = crossSpawn._parse(processedFile, processedArgs, processedOptions);5657 const options = addDefaultOptions(initialOptions);58 validateTimeout(options);

Stricter values for the `encoding` option (#928)ehmickybefore adoption477bbc4

line 59why, one step back
59 validateEncoding(options);

Split files (#837)ehmickybefore adoption672c2d7

lines 60–62why, one step back
60 options.shell = normalizeFileUrl(options.shell);
61 options.env = getEnv(options);62 options.forceKillAfterDelay = normalizeForceKillAfterDelay(options.forceKillAfterDelay);

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 63why, one step back
63 options.lines &&= !BINARY_ENCODINGS.has(options.encoding) && options.buffer;

Split files (#837)ehmickybefore adoption672c2d7

lines 64–69why, one step back
64
65 if (process.platform === 'win32' && basename(file, '.exe') === 'cmd') {66 // #11667 args.unshift('/q');68 }69

Improve `verbose` option (#883)ehmickybefore adoption7d0943f

line 70why, one step back
70 return {file, args, options};

Split files (#837)ehmickybefore adoption672c2d7

lines 71–89why, one step back
71};
7273const addDefaultOptions = ({74 maxBuffer = DEFAULT_MAX_BUFFER,75 buffer = true,76 stripFinalNewline = true,77 extendEnv = true,78 preferLocal = false,79 cwd,80 localDir = cwd,81 encoding = 'utf8',82 reject = true,83 cleanup = true,84 all = false,85 windowsHide = true,86 killSignal = 'SIGTERM',87 forceKillAfterDelay = true,88 lines = false,89 ipc = false,

Change default value of `serialization` option to `advanced` (#905)ehmickybefore adoption4577582

line 90why, one step back
90 serialization = 'advanced',

Split files (#837)ehmickybefore adoption672c2d7

lines 91–109why, one step back
91 ...options
92}) => ({93 ...options,94 maxBuffer,95 buffer,96 stripFinalNewline,97 extendEnv,98 preferLocal,99 cwd,100 localDir,101 encoding,102 reject,103 cleanup,104 all,105 windowsHide,106 killSignal,107 forceKillAfterDelay,108 lines,109 ipc,

Change default value of `serialization` option to `advanced` (#905)ehmickybefore adoption4577582

line 110why, one step back
110 serialization,

Split files (#837)ehmickybefore adoption672c2d7

lines 111–114why, one step back
111});
112113const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;114

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

line 115why, one step back
115const getEnv = ({env: envOption, extendEnv, preferLocal, node, localDir, nodePath}) => {

Split files (#837)ehmickybefore adoption672c2d7

lines 116–117why, one step back
116 const env = extendEnv ? {...process.env, ...envOption} : envOption;
117

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

lines 118–119why, one step back
118 if (preferLocal || node) {
119 return npmRunPathEnv({env, cwd: localDir, execPath: nodePath, preferLocal, addExecPath: node});

Split files (#837)ehmickybefore adoption672c2d7

lines 120–123why, one step back
120 }
121122 return env;123};