Skip to content
Myr

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 file

Take it

Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553

lines 1–2why, one step back
1import {getStdioOptionType, isRegularUrl, isUnknownStdioString} from './type.js';
2import {addStreamDirection} from './direction.js';

Split files (#837)ehmickybefore adoption672c2d7

line 3why, one step back
3import {normalizeStdio} from './option.js';

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

line 4why, one step back
4import {handleNativeStream} from './native.js';

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

line 5why, one step back
5import {handleInputOptions} from './input.js';

Line-wise standard streams (#741)ehmickybefore adoption479d12e

line 6why, one step back
6import {handleStreamsLines} from './lines.js';

Split files (#837)ehmickybefore adoption672c2d7

line 7why, one step back
7import {handleStreamsEncoding} from './encoding-start.js';

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 8why, one step back
8import {normalizeGenerators} from './generator.js';

Split files (#837)ehmickybefore adoption672c2d7

line 9why, one step back
9import {forwardStdio} from './forward.js';

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 10–11why, one step back
10
11// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async/sync mode

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

line 12why, one step back
12export const handleInput = (addProperties, options, isSync) => {

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

line 13why, one step back
13 const stdio = normalizeStdio(options);

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 14why, one step back
14 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 back
15 const stdioStreamsGroups = [[...stdinStreams, ...handleInputOptions(options)], ...otherStreamsGroups]
16 .map(stdioStreams => validateStreams(stdioStreams))

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

line 17why, one step back
17 .map(stdioStreams => addStreamDirection(stdioStreams))

Line-wise standard streams (#741)ehmickybefore adoption479d12e

line 18why, one step back
18 .map(stdioStreams => handleStreamsLines(stdioStreams, options, isSync))

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

line 19why, one step back
19 .map(stdioStreams => handleStreamsEncoding(stdioStreams, options, isSync))

Refactor `encoding` option (#735)ehmickybefore adoption1907e7c

line 20why, one step back
20 .map(stdioStreams => normalizeGenerators(stdioStreams))

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

line 21why, one step back
21 .map(stdioStreams => addStreamsProperties(stdioStreams, addProperties));

Split files (#837)ehmickybefore adoption672c2d7

line 22why, one step back
22 options.stdio = forwardStdio(stdioStreamsGroups);

Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3

line 23why, one step back
23 return stdioStreamsGroups;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 24–25why, one step back
24};
25

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 26–28why, one step back
26// 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 back
29const getStdioStreams = (stdioOption, fdNumber) => {
30 const optionName = getOptionName(fdNumber);

Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de

line 31why, one step back
31 const stdioOptions = Array.isArray(stdioOption) ? stdioOption : [stdioOption];

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 32why, one step back
32 const rawStdioStreams = stdioOptions.map(stdioOption => getStdioStream(stdioOption, optionName, fdNumber));

Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de

lines 33–36why, one step back
33 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 back
37};

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

line 38why, one step back
38

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 39why, one step back
39const getOptionName = fdNumber => KNOWN_OPTION_NAMES[fdNumber] ?? `stdio[${fdNumber}]`;

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

line 40why, one step back
40const KNOWN_OPTION_NAMES = ['stdin', 'stdout', 'stderr'];

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

line 41why, one step back
41

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 42why, one step back
42const getStdioStream = (stdioOption, optionName, fdNumber) => {

Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de

line 43why, one step back
43 const type = getStdioOptionType(stdioOption, optionName);

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 44why, one step back
44 return {type, value: stdioOption, optionName, fdNumber};

Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de

lines 45–52why, one step back
45};
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 back
53 throw new TypeError(`The \`${optionName}\` option must not be an empty array.`);
54 }55

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

lines 56–57why, one step back
56 if (!isStdioArray) {
57 return;

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

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

Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de

lines 60–62why, one step back
60 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 back
63 }
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 back
69const INVALID_STDIO_ARRAY_OPTIONS = new Set(['ignore', 'ipc']);

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

line 70why, one step back
70

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 71–74why, one step back
71const 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 back
75

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

line 76why, one step back
76 return stdioStreams;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 77–85why, one step back
77};
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 back
86 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 back
87 }
88};

Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553

lines 89–92why, one step back
89
90// 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 back
93const addStreamsProperties = (stdioStreams, addProperties) => stdioStreams.map(stdioStream => ({

Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553

line 94why, one step back
94 ...stdioStream,

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

line 95why, one step back
95 ...addProperties[stdioStream.direction][stdioStream.type]?.(stdioStream),

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

line 96why, one step back
96}));