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 fileTake it
The walk stopped after 200 commits: lines older than fdd8a9f point at it.
Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
line 1why, one step back1import {debuglog} from 'node:util';Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 2–8why, one step back2import 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 back9 // Keep the null prototype, otherwise a polluted `Object.prototype` would inject option values10 const optionsCopy = {__proto__: null, ...options};Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 11–19why, one step back1112 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 back20 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 back23 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;29Fix fd-specific options to account for `ipc` (#1241)Sindre Sorhusbefore adoptionb886883
lines 30–31why, one step back30const 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 back32 : optionArray.fill(optionValue);33Fix fd-specific options to account for `ipc` (#1241)Sindre Sorhusbefore adoptionb886883
line 34why, one step back34const normalizeOptionObject = (optionValue, optionArray, optionName, stdioLength) => {Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 35why, one step back35 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 back36 for (const fdNumber of parseFdName(fdName, optionName, stdioLength)) {Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 37–54why, one step back37 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};54Fix fd-specific options to account for `ipc` (#1241)Sindre Sorhusbefore adoptionb886883
line 55why, one step back55const parseFdName = (fdName, optionName, stdioLength) => {Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe
line 56why, one step back56 if (fdName === 'ipc') {Fix fd-specific options to account for `ipc` (#1241)Sindre Sorhusbefore adoptionb886883
line 57why, one step back57 return [stdioLength];Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe
lines 58–59why, one step back58 }59Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 60–62why, one step back60 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 back63It 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 back64 }65Fix fd-specific options to account for `ipc` (#1241)Sindre Sorhusbefore adoptionb886883
line 66why, one step back66 if (fdNumber !== 'all' && fdNumber >= stdioLength) {Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 67–85why, one step back67 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 back86 return Number(regexpResult.groups.fdNumber);Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 87–89why, one step back87 }88};89Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
line 90why, one step back90const FD_REGEXP = /^fd(?<fdNumber>\d+)$/;Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 91–95why, one step back9192const addDefaultValue = (optionArray, optionName) => optionArray.map(optionValue => optionValue === undefined93 ? DEFAULT_OPTIONS[optionName]94 : optionValue);95Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
lines 96–98why, one step back96// Default value for the `verbose` option97const verboseDefault = debuglog('execa').enabled ? 'full' : 'none';98Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 99–108why, one step back99const 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 back109110// Retrieve fd-specific option111export const getFdSpecificValue = (optionArray, fdNumber) => fdNumber === 'ipc'112 ? optionArray.at(-1)113 : optionArray[fdNumber];