Skip to content
Myr

specific.js

This file

Path

lib/arguments/specific.js

132 lines · 4.7 KiB · JavaScript

Last changed by

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199

line 1why, one step back
1import {debuglog} from 'node:util';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 2–3why, one step back
2import isPlainObject from 'is-plain-obj';
3import {STANDARD_STREAMS_ALIASES} from '../utils/standard-stream.js';

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

line 4why, one step back
4import {checkBooleanOption} from '../utils/options.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 5–9why, one step back
5
6// Some options can have different values for `stdout`/`stderr`/`fd3`.7// This normalizes those to array of values.8// For example, `{verbose: {stdout: 'none', stderr: 'full'}}` becomes `{verbose: ['none', 'none', 'full']}`9export const normalizeFdSpecificOptions = options => {

Harden some thingsSindre Sorhusbefore adoption4a0a121

lines 10–11why, one step back
10 // Keep the null prototype, otherwise a polluted `Object.prototype` would inject option values
11 const optionsCopy = {__proto__: null, ...options};

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 12–20why, one step back
12
13 for (const optionName of FD_SPECIFIC_OPTIONS) {14 optionsCopy[optionName] = normalizeFdSpecificOption(options, optionName);15 }1617 return optionsCopy;18};1920export const normalizeFdSpecificOption = (options, optionName) => {

Fix fd-specific options to account for `ipc` (#1241)Sindre Sorhusbefore adoptionb886883

lines 21–23why, one step back
21 const stdioLength = getStdioLength(options);
22 const optionBaseArray = Array.from({length: stdioLength + 1});23 const optionArray = normalizeFdSpecificValue(options[optionName], optionBaseArray, optionName, stdioLength);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 24–30why, one step back
24 return addDefaultValue(optionArray, optionName);
25};2627const getStdioLength = ({stdio}) => Array.isArray(stdio)28 ? Math.max(stdio.length, STANDARD_STREAMS_ALIASES.length)29 : STANDARD_STREAMS_ALIASES.length;30

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

lines 31–34why, one step back
31const normalizeFdSpecificValue = (optionValue, optionArray, optionName, stdioLength) => {
32 if (isPlainObject(optionValue)) {33 return normalizeOptionObject(optionValue, optionArray, optionName, stdioLength);34 }

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 35why, one step back
35

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

lines 36–39why, one step back
36 validateFdSpecificValue(optionValue, optionName);
37 return optionArray.fill(optionValue);38};39

Fix fd-specific options to account for `ipc` (#1241)Sindre Sorhusbefore adoptionb886883

line 40why, one step back
40const normalizeOptionObject = (optionValue, optionArray, optionName, stdioLength) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 41why, one step back
41 for (const fdName of Object.keys(optionValue).sort(compareFdName)) {

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

line 42why, one step back
42 validateFdSpecificValue(optionValue[fdName], optionName, `${optionName}.${fdName}`);

Fix fd-specific options to account for `ipc` (#1241)Sindre Sorhusbefore adoptionb886883

line 43why, one step back
43 for (const fdNumber of parseFdName(fdName, optionName, stdioLength)) {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 44–61why, one step back
44 optionArray[fdNumber] = optionValue[fdName];
45 }46 }4748 return optionArray;49};5051// Ensure priority order when setting both `stdout`/`stderr`, `fd1`/`fd2`, and `all`52const compareFdName = (fdNameA, fdNameB) => getFdNameOrder(fdNameA) < getFdNameOrder(fdNameB) ? 1 : -1;5354const getFdNameOrder = fdName => {55 if (fdName === 'stdout' || fdName === 'stderr') {56 return 0;57 }5859 return fdName === 'all' ? 2 : 1;60};61

Fix fd-specific options to account for `ipc` (#1241)Sindre Sorhusbefore adoptionb886883

line 62why, one step back
62const parseFdName = (fdName, optionName, stdioLength) => {

Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe

line 63why, one step back
63 if (fdName === 'ipc') {

Fix fd-specific options to account for `ipc` (#1241)Sindre Sorhusbefore adoptionb886883

line 64why, one step back
64 return [stdioLength];

Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe

lines 65–66why, one step back
65 }
66

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 67–69why, one step back
67 const fdNumber = parseFd(fdName);
68 if (fdNumber === undefined || fdNumber === 0) {69 throw new TypeError(`"${optionName}.${fdName}" is invalid.

Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe

line 70why, one step back
70It must be "${optionName}.stdout", "${optionName}.stderr", "${optionName}.all", "${optionName}.ipc", or "${optionName}.fd3", "${optionName}.fd4" (and so on).`);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 71–72why, one step back
71 }
72

Fix fd-specific options to account for `ipc` (#1241)Sindre Sorhusbefore adoptionb886883

line 73why, one step back
73 if (fdNumber !== 'all' && fdNumber >= stdioLength) {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 74–92why, one step back
74 throw new TypeError(`"${optionName}.${fdName}" is invalid: that file descriptor does not exist.
75Please set the "stdio" option to ensure that file descriptor exists.`);76 }7778 return fdNumber === 'all' ? [1, 2] : [fdNumber];79};8081// Use the same syntax for fd-specific options and the `from`/`to` options82export const parseFd = fdName => {83 if (fdName === 'all') {84 return fdName;85 }8687 if (STANDARD_STREAMS_ALIASES.includes(fdName)) {88 return STANDARD_STREAMS_ALIASES.indexOf(fdName);89 }9091 const regexpResult = FD_REGEXP.exec(fdName);92 if (regexpResult !== null) {

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

line 93why, one step back
93 return Number(regexpResult.groups.fdNumber);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 94–96why, one step back
94 }
95};96

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

line 97why, one step back
97const FD_REGEXP = /^fd(?<fdNumber>\d+)$/;

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 98–101why, one step back
98
99const addDefaultValue = (optionArray, optionName) => optionArray.map(optionValue => optionValue === undefined100 ? DEFAULT_OPTIONS[optionName]101 : optionValue);

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

lines 102–113why, one step back
102
103const BOOLEAN_FD_SPECIFIC_OPTIONS = new Set(['lines', 'buffer', 'stripFinalNewline']);104105// Wrong types must be reported instead of being silently coerced, e.g. a truthy string enabling the `buffer` option.106// `undefined` uses the default value.107const validateFdSpecificValue = (optionValue, optionName, fullOptionName = optionName) => {108 if (BOOLEAN_FD_SPECIFIC_OPTIONS.has(optionName)) {109 checkBooleanOption(optionValue, fullOptionName);110 } else if (optionName === 'maxBuffer' && optionValue !== undefined && (typeof optionValue !== 'number' || Number.isNaN(optionValue))) {111 throw new TypeError(`The \`${fullOptionName}\` option must use a number.`);112 }113};

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 114why, one step back
114

Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199

lines 115–117why, one step back
115// Default value for the `verbose` option
116const verboseDefault = debuglog('execa').enabled ? 'full' : 'none';117

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 118–127why, one step back
118const DEFAULT_OPTIONS = {
119 lines: false,120 buffer: true,121 maxBuffer: 1000 * 1000 * 100,122 verbose: verboseDefault,123 stripFinalNewline: true,124};125126// List of options which can have different values for `stdout`/`stderr`127export const FD_SPECIFIC_OPTIONS = ['lines', 'buffer', 'maxBuffer', 'verbose', 'stripFinalNewline'];

Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199

lines 128–132why, one step back
128
129// Retrieve fd-specific option130export const getFdSpecificValue = (optionArray, fdNumber) => fdNumber === 'ipc'131 ? optionArray.at(-1)132 : optionArray[fdNumber];