Skip to content
Myr

stdio-option.js

This file

Path

lib/stdio/stdio-option.js

61 lines · 2.3 KiB · JavaScript

Last changed by

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

History of this file

Take it

Rename files (#977)ehmickybefore adoption4b2e316

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

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 3–4why, one step back
3// Add support for `stdin`/`stdout`/`stderr` as an alias for `stdio`.
4// Also normalize the `stdio` option.

Rename files (#977)ehmickybefore adoption4b2e316

lines 5–44why, one step back
5export const normalizeStdioOption = ({stdio, ipc, buffer, verbose, ...options}, isSync) => {
6 const stdioArray = getStdioArray(stdio, options).map((stdioOption, fdNumber) => addDefaultValue(stdioOption, fdNumber));7 return isSync ? normalizeStdioSync(stdioArray, buffer, verbose) : normalizeStdioAsync(stdioArray, ipc);8};910const getStdioArray = (stdio, options) => {11 if (stdio === undefined) {12 return STANDARD_STREAMS_ALIASES.map(alias => options[alias]);13 }1415 if (hasAlias(options)) {16 throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${STANDARD_STREAMS_ALIASES.map(alias => `\`${alias}\``).join(', ')}`);17 }1819 if (typeof stdio === 'string') {20 return [stdio, stdio, stdio];21 }2223 if (!Array.isArray(stdio)) {24 throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${typeof stdio}\``);25 }2627 const length = Math.max(stdio.length, STANDARD_STREAMS_ALIASES.length);28 return Array.from({length}, (_, fdNumber) => stdio[fdNumber]);29};3031const hasAlias = options => STANDARD_STREAMS_ALIASES.some(alias => options[alias] !== undefined);3233const addDefaultValue = (stdioOption, fdNumber) => {34 if (Array.isArray(stdioOption)) {35 return stdioOption.map(item => addDefaultValue(item, fdNumber));36 }3738 if (stdioOption === null || stdioOption === undefined) {39 return fdNumber >= STANDARD_STREAMS_ALIASES.length ? 'ignore' : 'pipe';40 }4142 return stdioOption;43};44

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 45–46why, one step back
45// Using `buffer: false` with synchronous methods implies `stdout`/`stderr`: `ignore`.
46// Unless the output is needed, e.g. due to `verbose: 'full'` or to redirecting to a file.

Rename files (#977)ehmickybefore adoption4b2e316

lines 47–57why, one step back
47const normalizeStdioSync = (stdioArray, buffer, verbose) => stdioArray.map((stdioOption, fdNumber) =>
48 !buffer[fdNumber]49 && fdNumber !== 050 && verbose[fdNumber] !== 'full'51 && isOutputPipeOnly(stdioOption)52 ? 'ignore'53 : stdioOption);5455const isOutputPipeOnly = stdioOption => stdioOption === 'pipe'56 || (Array.isArray(stdioOption) && stdioOption.every(item => item === 'pipe'));57

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 58why, one step back
58// The `ipc` option adds an `ipc` item to the `stdio` option

Rename files (#977)ehmickybefore adoption4b2e316

lines 59–61why, one step back
59const normalizeStdioAsync = (stdioArray, ipc) => ipc && !stdioArray.includes('ipc')
60 ? [...stdioArray, 'ipc']61 : stdioArray;