handle.js
This file
Path
lib/stdio/handle.js
96 lines · 4.3 KiB · JavaScript
Last changed by
Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
History of this fileTake it
Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
lines 1–2why, one step back1import {getStdioOptionType, isRegularUrl, isUnknownStdioString} from './type.js';2import {addStreamDirection} from './direction.js';Split files (#837)ehmickybefore adoption672c2d7
line 3why, one step back3import {normalizeStdio} from './option.js';Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
line 4why, one step back4import {handleNativeStream} from './native.js';Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
line 5why, one step back5import {handleInputOptions} from './input.js';Line-wise standard streams (#741)ehmickybefore adoption479d12e
line 6why, one step back6import {handleStreamsLines} from './lines.js';Split files (#837)ehmickybefore adoption672c2d7
line 7why, one step back7import {handleStreamsEncoding} from './encoding-start.js';Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 8why, one step back8import {normalizeGenerators} from './generator.js';Split files (#837)ehmickybefore adoption672c2d7
line 9why, one step back9import {forwardStdio} from './forward.js';Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
lines 10–11why, one step back1011// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async/sync modeFix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
line 12why, one step back12export const handleInput = (addProperties, options, isSync) => {Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
line 13why, one step back13 const stdio = normalizeStdio(options);Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 14why, one step back14 const [stdinStreams, ...otherStreamsGroups] = stdio.map((stdioOption, fdNumber) => getStdioStreams(stdioOption, fdNumber));Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
lines 15–16why, one step back15 const stdioStreamsGroups = [[...stdinStreams, ...handleInputOptions(options)], ...otherStreamsGroups]16 .map(stdioStreams => validateStreams(stdioStreams))Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb
line 17why, one step back17 .map(stdioStreams => addStreamDirection(stdioStreams))Line-wise standard streams (#741)ehmickybefore adoption479d12e
line 18why, one step back18 .map(stdioStreams => handleStreamsLines(stdioStreams, options, isSync))Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
line 19why, one step back19 .map(stdioStreams => handleStreamsEncoding(stdioStreams, options, isSync))Refactor `encoding` option (#735)ehmickybefore adoption1907e7c
line 20why, one step back20 .map(stdioStreams => normalizeGenerators(stdioStreams))Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
line 21why, one step back21 .map(stdioStreams => addStreamsProperties(stdioStreams, addProperties));Split files (#837)ehmickybefore adoption672c2d7
line 22why, one step back22 options.stdio = forwardStdio(stdioStreamsGroups);Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
line 23why, one step back23 return stdioStreamsGroups;Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
lines 24–25why, one step back24};25Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 26–28why, one step back26// We make sure passing an array with a single item behaves the same as passing that item without an array.27// This is what users would expect.28// For example, `stdout: ['ignore']` behaves the same as `stdout: 'ignore'`.Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
lines 29–30why, one step back29const getStdioStreams = (stdioOption, fdNumber) => {30 const optionName = getOptionName(fdNumber);Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de
line 31why, one step back31 const stdioOptions = Array.isArray(stdioOption) ? stdioOption : [stdioOption];Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 32why, one step back32 const rawStdioStreams = stdioOptions.map(stdioOption => getStdioStream(stdioOption, optionName, fdNumber));Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de
lines 33–36why, one step back33 const stdioStreams = filterDuplicates(rawStdioStreams);34 const isStdioArray = stdioStreams.length > 1;35 validateStdioArray(stdioStreams, isStdioArray, optionName);36 return stdioStreams.map(stdioStream => handleNativeStream(stdioStream, isStdioArray));Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb
line 37why, one step back37};Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
line 38why, one step back38Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 39why, one step back39const getOptionName = fdNumber => KNOWN_OPTION_NAMES[fdNumber] ?? `stdio[${fdNumber}]`;Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb
line 40why, one step back40const KNOWN_OPTION_NAMES = ['stdin', 'stdout', 'stderr'];Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
line 41why, one step back41Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 42why, one step back42const getStdioStream = (stdioOption, optionName, fdNumber) => {Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de
line 43why, one step back43 const type = getStdioOptionType(stdioOption, optionName);Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 44why, one step back44 return {type, value: stdioOption, optionName, fdNumber};Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de
lines 45–52why, one step back45};4647const filterDuplicates = stdioStreams => stdioStreams.filter((stdioStreamOne, indexOne) =>48 stdioStreams.every((stdioStreamTwo, indexTwo) =>49 stdioStreamOne.value !== stdioStreamTwo.value || indexOne >= indexTwo || stdioStreamOne.type === 'generator'));5051const validateStdioArray = (stdioStreams, isStdioArray, optionName) => {52 if (stdioStreams.length === 0) {Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 53–55why, one step back53 throw new TypeError(`The \`${optionName}\` option must not be an empty array.`);54 }55Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb
lines 56–57why, one step back56 if (!isStdioArray) {57 return;Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 58–59why, one step back58 }59Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de
lines 60–62why, one step back60 for (const {value, optionName} of stdioStreams) {61 if (INVALID_STDIO_ARRAY_OPTIONS.has(value)) {62 throw new Error(`The \`${optionName}\` option must not include \`${value}\`.`);Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 63–68why, one step back63 }64 }65};6667// Using those `stdio` values together with others for the same stream does not make sense, so we make it fail.68// However, we do allow it if the array has a single item.Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de
line 69why, one step back69const INVALID_STDIO_ARRAY_OPTIONS = new Set(['ignore', 'ipc']);Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
line 70why, one step back70Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
lines 71–74why, one step back71const validateStreams = stdioStreams => {72 for (const stdioStream of stdioStreams) {73 validateFileStdio(stdioStream);74 }Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
line 75why, one step back75Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
line 76why, one step back76 return stdioStreams;Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
lines 77–85why, one step back77};7879const validateFileStdio = ({type, value, optionName}) => {80 if (isRegularUrl(value)) {81 throw new TypeError(`The \`${optionName}: URL\` option must use the \`file:\` scheme.82For example, you can use the \`pathToFileURL()\` method of the \`url\` core module.`);83 }8485 if (isUnknownStdioString(type, value)) {Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13
line 86why, one step back86 throw new TypeError(`The \`${optionName}: { file: '...' }\` option must be used instead of \`${optionName}: '...'\`.`);Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
lines 87–88why, one step back87 }88};Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
lines 89–92why, one step back8990// Some `stdio` values require Execa to create streams.91// For example, file paths create file read/write streams.92// Those transformations are specified in `addProperties`, which is both direction-specific and type-specific.Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
line 93why, one step back93const addStreamsProperties = (stdioStreams, addProperties) => stdioStreams.map(stdioStream => ({Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
line 94why, one step back94 ...stdioStream,Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
line 95why, one step back95 ...addProperties[stdioStream.direction][stdioStream.type]?.(stdioStream),Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb
line 96why, one step back96}));