Skip to content
Myr

handle.js

This file

Path

lib/stdio/handle.js

132 lines · 5.9 KiB · JavaScript

Last changed by

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

History of this file

Take it

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

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

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

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

line 2why, one step back
2import {getStdioItemType, isRegularUrl, isUnknownStdioString, FILE_TYPES} from './type.js';

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

Split files (#837)ehmickybefore adoption672c2d7

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

Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4

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';

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

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

Refactor tests (#934)ehmickybefore adoption52c4bde

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4

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

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

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

Add support for `buffer: false` to `execaSync()` (#953)ehmickybefore adoptiond0052ac

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

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

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

lines 26–28why, one step back
26 const {stdioItems: initialStdioItems, isStdioArray} = initializeStdioItems({stdioOption, fdNumber, options, optionName});
27 const direction = getStreamDirection(initialStdioItems, fdNumber, optionName);28 const stdioItems = initialStdioItems.map(stdioItem => handleNativeStream({stdioItem, isStdioArray, fdNumber, direction, isSync}));

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 29–30why, one step back
29 const objectMode = getObjectMode(stdioItems, optionName, direction, options);
30 validateFileObjectMode(stdioItems, objectMode);

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

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

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

line 33why, one step back
33};

Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4

line 34why, one step back
34

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

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

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

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

Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4

line 37why, one step back
37

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

line 38why, one step back
38const initializeStdioItems = ({stdioOption, fdNumber, options, optionName}) => {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

line 49why, one step back
49 return {stdioItems, isStdioArray};

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

line 57why, one step back
57

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 58why, one step back
58const filterDuplicates = stdioItems => stdioItems.filter((stdioItemOne, indexOne) =>

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 59–62why, one step back
59 stdioItems.every((stdioItemTwo, indexTwo) => stdioItemOne.value !== stdioItemTwo.value
60 || indexOne >= indexTwo61 || stdioItemOne.type === 'generator'62 || stdioItemOne.type === 'asyncGenerator'));

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 63–65why, one step back
63
64const validateStdioArray = (stdioItems, isStdioArray, optionName) => {65 if (stdioItems.length === 0) {

Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4

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

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

lines 69–70why, one step back
69 if (!isStdioArray) {
70 return;

Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4

lines 71–72why, one step back
71 }
72

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

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

Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4

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

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

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

Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4

line 83why, one step back
83

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 84–86why, one step back
84const validateStreams = stdioItems => {
85 for (const stdioItem of stdioItems) {86 validateFileStdio(stdioItem);

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

line 87why, one step back
87 }

Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4

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

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

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

Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4

lines 98–100why, one step back
98 }
99};100

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 101–102why, one step back
101const normalizeStdioItems = ({stdioItems, fdNumber, optionName, addProperties, options, direction, stdioState, verboseInfo, objectMode}) => {
102 const allStdioItems = addInternalStdioItems({stdioItems, fdNumber, optionName, options, direction, stdioState, verboseInfo, objectMode});

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 103why, one step back
103 const normalizedStdioItems = normalizeTransforms(allStdioItems, optionName, direction, options);

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 104–106why, one step back
104 return normalizedStdioItems.map(stdioItem => addStreamProperties(stdioItem, addProperties, direction));
105};106

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 107why, one step back
107const addInternalStdioItems = ({stdioItems, fdNumber, optionName, options, direction, stdioState, verboseInfo, objectMode}) => willPipeFileDescriptor(stdioItems)

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

line 108why, one step back
108 ? [

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

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

Add support for `verbose` option to `execaSync()` (#950)ehmickybefore adoptioned052d4

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

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 112–113why, one step back
112 ]
113 : stdioItems;

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 114why, one step back
114

Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 122–127why, one step back
122
123const validateFileObjectMode = (stdioItems, objectMode) => {124 if (!objectMode) {125 return;126 }127

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

line 128why, one step back
128 const fileStdioItem = stdioItems.find(({type}) => FILE_TYPES.has(type));

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 129–132why, one step back
129 if (fileStdioItem !== undefined) {
130 throw new TypeError(`The \`${fileStdioItem.optionName}\` option cannot use both files and transforms in objectMode.`);131 }132};