Skip to content
Myr

specific.js

This file

Path

lib/arguments/specific.js

113 lines · 3.9 KiB · JavaScript

Last changed by

Harden some thingsSindre Sorhusbefore adoption4a0a121

History of this file

Take it

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

Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199

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

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 2–8why, one step back
2import isPlainObject from 'is-plain-obj';
3import {STANDARD_STREAMS_ALIASES} from '../utils/standard-stream.js';45// Some options can have different values for `stdout`/`stderr`/`fd3`.6// This normalizes those to array of values.7// For example, `{verbose: {stdout: 'none', stderr: 'full'}}` becomes `{verbose: ['none', 'none', 'full']}`8export const normalizeFdSpecificOptions = options => {

Harden some thingsSindre Sorhusbefore adoption4a0a121

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

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 11–19why, one step back
11
12 for (const optionName of FD_SPECIFIC_OPTIONS) {13 optionsCopy[optionName] = normalizeFdSpecificOption(options, optionName);14 }1516 return optionsCopy;17};1819export const normalizeFdSpecificOption = (options, optionName) => {

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

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

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

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

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

lines 30–31why, one step back
30const normalizeFdSpecificValue = (optionValue, optionArray, optionName, stdioLength) => isPlainObject(optionValue)
31 ? normalizeOptionObject(optionValue, optionArray, optionName, stdioLength)

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 32–33why, one step back
32 : optionArray.fill(optionValue);
33

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

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

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

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

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

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

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 37–54why, one step back
37 optionArray[fdNumber] = optionValue[fdName];
38 }39 }4041 return optionArray;42};4344// Ensure priority order when setting both `stdout`/`stderr`, `fd1`/`fd2`, and `all`45const compareFdName = (fdNameA, fdNameB) => getFdNameOrder(fdNameA) < getFdNameOrder(fdNameB) ? 1 : -1;4647const getFdNameOrder = fdName => {48 if (fdName === 'stdout' || fdName === 'stderr') {49 return 0;50 }5152 return fdName === 'all' ? 2 : 1;53};54

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

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

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

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

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

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

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

lines 58–59why, one step back
58 }
59

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

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

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

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

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

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

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

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

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 67–85why, one step back
67 throw new TypeError(`"${optionName}.${fdName}" is invalid: that file descriptor does not exist.
68Please set the "stdio" option to ensure that file descriptor exists.`);69 }7071 return fdNumber === 'all' ? [1, 2] : [fdNumber];72};7374// Use the same syntax for fd-specific options and the `from`/`to` options75export const parseFd = fdName => {76 if (fdName === 'all') {77 return fdName;78 }7980 if (STANDARD_STREAMS_ALIASES.includes(fdName)) {81 return STANDARD_STREAMS_ALIASES.indexOf(fdName);82 }8384 const regexpResult = FD_REGEXP.exec(fdName);85 if (regexpResult !== null) {

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

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

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 87–89why, one step back
87 }
88};89

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

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

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 91–95why, one step back
91
92const addDefaultValue = (optionArray, optionName) => optionArray.map(optionValue => optionValue === undefined93 ? DEFAULT_OPTIONS[optionName]94 : optionValue);95

Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199

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

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 99–108why, one step back
99const DEFAULT_OPTIONS = {
100 lines: false,101 buffer: true,102 maxBuffer: 1000 * 1000 * 100,103 verbose: verboseDefault,104 stripFinalNewline: true,105};106107// List of options which can have different values for `stdout`/`stderr`108export const FD_SPECIFIC_OPTIONS = ['lines', 'buffer', 'maxBuffer', 'verbose', 'stripFinalNewline'];

Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199

lines 109–113why, one step back
109
110// Retrieve fd-specific option111export const getFdSpecificValue = (optionArray, fdNumber) => fdNumber === 'ipc'112 ? optionArray.at(-1)113 : optionArray[fdNumber];