Skip to content
Myr

handle.js

This file

Path

lib/stdio/handle.js

124 lines · 5.6 KiB · JavaScript

Last changed by

Fix using both `encoding: 'utf16le'` and `lines`/`verbose` options (#931)ehmickybefore adoptiona480e1d

History of this file

Take it

The walk stopped after 200 commits: lines older than c405105 point at it.

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

line 1why, one step back
1import {handleStreamsVerbose} from '../verbose/output.js';

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 2–3why, one step back
2import {getStdioItemType, isRegularUrl, isUnknownStdioString} from './type.js';
3import {getStreamDirection} from './direction.js';

Split files (#837)ehmickybefore adoption672c2d7

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

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

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

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

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

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

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

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

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

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

Improve documentation of `stdin`, `stdout`, `stderr` and `stdio` (#626)ehmickybefore adoptionc405105

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

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

lines 13–14why, one step back
13export const handleInput = (addProperties, options, verboseInfo, isSync) => {
14 const stdioState = {};

Improve documentation of `stdin`, `stdout`, `stderr` and `stdio` (#626)ehmickybefore adoptionc405105

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 16–19why, one step back
16 const fileDescriptors = stdio.map((stdioOption, fdNumber) =>
17 getFileDescriptor({stdioOption, fdNumber, addProperties, options, isSync, stdioState, verboseInfo}));18 options.stdio = fileDescriptors.map(({stdioItems}) => forwardStdio(stdioItems));19 return {fileDescriptors, stdioState};

Improve documentation of `stdin`, `stdout`, `stderr` and `stdio` (#626)ehmickybefore adoptionc405105

lines 20–21why, one step back
20};
21

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

lines 22–24why, one step back
22// We make sure passing an array with a single item behaves the same as passing that item without an array.
23// This is what users would expect.24// For example, `stdout: ['ignore']` behaves the same as `stdout: 'ignore'`.

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 25why, one step back
25const getFileDescriptor = ({stdioOption, fdNumber, addProperties, options, isSync, stdioState, verboseInfo}) => {

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 26why, one step back
26 const outputLines = [];

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

line 27why, one step back
27 const optionName = getOptionName(fdNumber);

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 28–29why, one step back
28 const stdioItems = initializeStdioItems(stdioOption, fdNumber, options, optionName);
29 const direction = getStreamDirection(stdioItems, fdNumber, optionName);

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 30–31why, one step back
30 const normalizedStdioItems = normalizeStdioItems({stdioItems, fdNumber, optionName, addProperties, options, isSync, direction, stdioState, verboseInfo, outputLines});
31 return {fdNumber, direction, outputLines, stdioItems: normalizedStdioItems};

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

line 32why, one step back
32};

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

line 33why, one step back
33

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

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

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

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

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

line 36why, one step back
36

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 37–48why, one step back
37const initializeStdioItems = (stdioOption, fdNumber, options, optionName) => {
38 const values = Array.isArray(stdioOption) ? stdioOption : [stdioOption];39 const initialStdioItems = [40 ...values.map(value => initializeStdioItem(value, optionName)),41 ...handleInputOptions(options, fdNumber),42 ];4344 const stdioItems = filterDuplicates(initialStdioItems);45 const isStdioArray = stdioItems.length > 1;46 validateStdioArray(stdioItems, isStdioArray, optionName);47 validateStreams(stdioItems);48 return stdioItems.map(stdioItem => handleNativeStream(stdioItem, isStdioArray, fdNumber));

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

lines 49–50why, one step back
49};
50

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 51–55why, one step back
51const initializeStdioItem = (value, optionName) => ({
52 type: getStdioItemType(value, optionName),53 value,54 optionName,55});

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

line 56why, one step back
56

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 57–62why, one step back
57const filterDuplicates = stdioItems => stdioItems.filter((stdioItemOne, indexOne) =>
58 stdioItems.every((stdioItemTwo, indexTwo) =>59 stdioItemOne.value !== stdioItemTwo.value || indexOne >= indexTwo || stdioItemOne.type === 'generator'));6061const validateStdioArray = (stdioItems, isStdioArray, optionName) => {62 if (stdioItems.length === 0) {

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

lines 63–65why, one step back
63 throw new TypeError(`The \`${optionName}\` option must not be an empty array.`);
64 }65

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

lines 66–67why, one step back
66 if (!isStdioArray) {
67 return;

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

lines 68–69why, one step back
68 }
69

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 70why, one step back
70 for (const {value, optionName} of stdioItems) {

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

lines 71–72why, one step back
71 if (INVALID_STDIO_ARRAY_OPTIONS.has(value)) {
72 throw new Error(`The \`${optionName}\` option must not include \`${value}\`.`);

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

lines 73–78why, one step back
73 }
74 }75};7677// Using those `stdio` values together with others for the same stream does not make sense, so we make it fail.78// However, we do allow it if the array has a single item.

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

line 79why, one step back
79const INVALID_STDIO_ARRAY_OPTIONS = new Set(['ignore', 'ipc']);

Improve documentation of `stdin`, `stdout`, `stderr` and `stdio` (#626)ehmickybefore adoptionc405105

line 80why, one step back
80

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 81–83why, one step back
81const validateStreams = stdioItems => {
82 for (const stdioItem of stdioItems) {83 validateFileStdio(stdioItem);

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

line 84why, one step back
84 }

Improve documentation of `stdin`, `stdout`, `stderr` and `stdio` (#626)ehmickybefore adoptionc405105

lines 85–93why, one step back
85};
8687const validateFileStdio = ({type, value, optionName}) => {88 if (isRegularUrl(value)) {89 throw new TypeError(`The \`${optionName}: URL\` option must use the \`file:\` scheme.90For example, you can use the \`pathToFileURL()\` method of the \`url\` core module.`);91 }9293 if (isUnknownStdioString(type, value)) {

Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13

line 94why, one step back
94 throw new TypeError(`The \`${optionName}: { file: '...' }\` option must be used instead of \`${optionName}: '...'\`.`);

Improve documentation of `stdin`, `stdout`, `stderr` and `stdio` (#626)ehmickybefore adoptionc405105

lines 95–96why, one step back
95 }
96};

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

line 97why, one step back
97

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 98–99why, one step back
98const normalizeStdioItems = ({stdioItems, fdNumber, optionName, addProperties, options, isSync, direction, stdioState, verboseInfo, outputLines}) => {
99 const allStdioItems = addInternalStdioItems({stdioItems, fdNumber, optionName, options, isSync, direction, stdioState, verboseInfo, outputLines});

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 100–103why, one step back
100 const normalizedStdioItems = normalizeGenerators(allStdioItems, direction, options);
101 return normalizedStdioItems.map(stdioItem => addStreamProperties(stdioItem, addProperties, direction));102};103

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 104why, one step back
104const addInternalStdioItems = ({stdioItems, fdNumber, optionName, options, isSync, direction, stdioState, verboseInfo, outputLines}) => {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 105–108why, one step back
105 if (!willPipeFileDescriptor(stdioItems)) {
106 return stdioItems;107 }108

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 109–110why, one step back
109 const objectMode = getObjectMode(stdioItems, direction, options);
110 return [

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 111why, one step back
111 ...stdioItems,

Fix using both `encoding: 'utf16le'` and `lines`/`verbose` options (#931)ehmickybefore adoptiona480e1d

line 112why, one step back
112 ...handleStreamsEncoding({options, isSync, direction, optionName, objectMode}),

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 113why, one step back
113 ...handleStreamsVerbose({stdioItems, options, isSync, stdioState, verboseInfo, fdNumber, optionName}),

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 114why, one step back
114 ...handleStreamsLines({options, isSync, direction, optionName, objectMode, outputLines}),

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 115–117why, one step back
115 ];
116};117

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

lines 118–120why, one step back
118// Some `stdio` values require Execa to create streams.
119// For example, file paths create file read/write streams.120// Those transformations are specified in `addProperties`, which is both direction-specific and type-specific.

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 121–124why, one step back
121const addStreamProperties = (stdioItem, addProperties, direction) => ({
122 ...stdioItem,123 ...addProperties[direction][stdioItem.type](stdioItem),124});