Skip to content
Myr

specific.js

This file

Path

lib/arguments/specific.js

99 lines · 3.3 KiB · JavaScript

Last changed by

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

History of this file

Take it

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

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

Rename files (#977)ehmickybefore adoption4b2e316

line 2why, one step back
2import {STANDARD_STREAMS_ALIASES} from '../utils/standard-stream.js';

Allow setting different `verbose` values for `stdout`/`stderr` (#970)ehmickybefore adoption6f279c0

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

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

line 4why, one step back
4

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 5–7why, one step back
5// 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']}`

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

lines 8–9why, one step back
8export const normalizeFdSpecificOptions = options => {
9 const optionsCopy = {...options};

Allow setting different `verbose` values for `stdout`/`stderr` (#970)ehmickybefore adoption6f279c0

line 10why, one step back
10

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

line 11why, one step back
11 for (const optionName of FD_SPECIFIC_OPTIONS) {

Allow setting different `verbose` values for `stdout`/`stderr` (#970)ehmickybefore adoption6f279c0

line 12why, one step back
12 optionsCopy[optionName] = normalizeFdSpecificOption(options, optionName);

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

lines 13–17why, one step back
13 }
1415 return optionsCopy;16};17

Allow setting different `verbose` values for `stdout`/`stderr` (#970)ehmickybefore adoption6f279c0

lines 18–23why, one step back
18export const normalizeFdSpecificOption = (options, optionName) => {
19 const optionBaseArray = Array.from({length: getStdioLength(options)});20 const optionArray = normalizeFdSpecificValue(options[optionName], optionBaseArray, optionName);21 return addDefaultValue(optionArray, optionName);22};23

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

lines 24–27why, one step back
24const getStdioLength = ({stdio}) => Array.isArray(stdio)
25 ? Math.max(stdio.length, STANDARD_STREAMS_ALIASES.length)26 : STANDARD_STREAMS_ALIASES.length;27

Allow setting different `verbose` values for `stdout`/`stderr` (#970)ehmickybefore adoption6f279c0

line 28why, one step back
28const normalizeFdSpecificValue = (optionValue, optionArray, optionName) => isPlainObject(optionValue)

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

lines 29–32why, one step back
29 ? normalizeOptionObject(optionValue, optionArray, optionName)
30 : optionArray.fill(optionValue);3132const normalizeOptionObject = (optionValue, optionArray, optionName) => {

Fix fd-specific options (#974)ehmickybefore adoption4940511

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

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

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

Fix fd-specific options (#974)ehmickybefore adoption4940511

line 35why, one step back
35 optionArray[fdNumber] = optionValue[fdName];

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

lines 36–39why, one step back
36 }
37 }3839 return optionArray;

Fix fd-specific options (#974)ehmickybefore adoption4940511

lines 40–50why, one step back
40};
4142// Ensure priority order when setting both `stdout`/`stderr`, `fd1`/`fd2`, and `all`43const compareFdName = (fdNameA, fdNameB) => getFdNameOrder(fdNameA) < getFdNameOrder(fdNameB) ? 1 : -1;4445const getFdNameOrder = fdName => {46 if (fdName === 'stdout' || fdName === 'stderr') {47 return 0;48 }4950 return fdName === 'all' ? 2 : 1;

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

lines 51–67why, one step back
51};
5253const parseFdName = (fdName, optionName, optionArray) => {54 const fdNumber = parseFd(fdName);55 if (fdNumber === undefined || fdNumber === 0) {56 throw new TypeError(`"${optionName}.${fdName}" is invalid.57It must be "${optionName}.stdout", "${optionName}.stderr", "${optionName}.all", or "${optionName}.fd3", "${optionName}.fd4" (and so on).`);58 }5960 if (fdNumber >= optionArray.length) {61 throw new TypeError(`"${optionName}.${fdName}" is invalid: that file descriptor does not exist.62Please set the "stdio" option to ensure that file descriptor exists.`);63 }6465 return fdNumber === 'all' ? [1, 2] : [fdNumber];66};67

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 68why, one step back
68// Use the same syntax for fd-specific options and the `from`/`to` options

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

lines 69–90why, one step back
69export const parseFd = fdName => {
70 if (fdName === 'all') {71 return fdName;72 }7374 if (STANDARD_STREAMS_ALIASES.includes(fdName)) {75 return STANDARD_STREAMS_ALIASES.indexOf(fdName);76 }7778 const regexpResult = FD_REGEXP.exec(fdName);79 if (regexpResult !== null) {80 return Number(regexpResult[1]);81 }82};8384const FD_REGEXP = /^fd(\d+)$/;8586const addDefaultValue = (optionArray, optionName) => optionArray.map(optionValue => optionValue === undefined87 ? DEFAULT_OPTIONS[optionName]88 : optionValue);8990const DEFAULT_OPTIONS = {

Allow setting different `lines` values for `stdout`/`stderr` (#972)ehmickybefore adoptionc474b30

line 91why, one step back
91 lines: false,

Allow setting different `buffer` values for `stdout`/`stderr` (#973)ehmickybefore adoptiona25a50d

line 92why, one step back
92 buffer: true,

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

line 93why, one step back
93 maxBuffer: 1000 * 1000 * 100,

Allow setting different `verbose` values for `stdout`/`stderr` (#970)ehmickybefore adoption6f279c0

line 94why, one step back
94 verbose: verboseDefault,

Allow setting different `stripFinalNewline` values for `stdout`/`stderr` (#971)ehmickybefore adoption64d52db

line 95why, one step back
95 stripFinalNewline: true,

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

line 96why, one step back
96};

Allow setting different `verbose` values for `stdout`/`stderr` (#970)ehmickybefore adoption6f279c0

line 97why, one step back
97

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 98why, one step back
98// List of options which can have different values for `stdout`/`stderr`

Allow setting different `buffer` values for `stdout`/`stderr` (#973)ehmickybefore adoptiona25a50d

line 99why, one step back
99export const FD_SPECIFIC_OPTIONS = ['lines', 'buffer', 'maxBuffer', 'verbose', 'stripFinalNewline'];