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 fileTake 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 back1import {handleStreamsVerbose} from '../verbose/output.js';Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 2–3why, one step back2import {getStdioItemType, isRegularUrl, isUnknownStdioString} from './type.js';3import {getStreamDirection} from './direction.js';Split files (#837)ehmickybefore adoption672c2d7
line 4why, one step back4import {normalizeStdio} from './option.js';Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
line 5why, one step back5import {handleNativeStream} from './native.js';Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
line 6why, one step back6import {handleInputOptions} from './input.js';Strip newlines in transforms (#919)ehmickybefore adoption26e9150
line 7why, one step back7import {handleStreamsLines} from './lines.js';Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
line 8why, one step back8import {handleStreamsEncoding} from './encoding-final.js';Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc
line 9why, one step back9import {normalizeGenerators, getObjectMode} from './generator.js';Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 10why, one step back10import {forwardStdio, willPipeFileDescriptor} from './forward.js';Improve documentation of `stdin`, `stdout`, `stderr` and `stdio` (#626)ehmickybefore adoptionc405105
lines 11–12why, one step back1112// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async/sync modePrint output with `verbose` option (#884)ehmickybefore adoption6fda30f
lines 13–14why, one step back13export const handleInput = (addProperties, options, verboseInfo, isSync) => {14 const stdioState = {};Improve documentation of `stdin`, `stdout`, `stderr` and `stdio` (#626)ehmickybefore adoptionc405105
line 15why, one step back15 const stdio = normalizeStdio(options);Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 16–19why, one step back16 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 back20};21Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 22–24why, one step back22// 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 back25const getFileDescriptor = ({stdioOption, fdNumber, addProperties, options, isSync, stdioState, verboseInfo}) => {Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc
line 26why, one step back26 const outputLines = [];Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 27why, one step back27 const optionName = getOptionName(fdNumber);Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 28–29why, one step back28 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 back30 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 back32};Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
line 33why, one step back33Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 34why, one step back34const getOptionName = fdNumber => KNOWN_OPTION_NAMES[fdNumber] ?? `stdio[${fdNumber}]`;Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb
line 35why, one step back35const KNOWN_OPTION_NAMES = ['stdin', 'stdout', 'stderr'];Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
line 36why, one step back36Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 37–48why, one step back37const 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 back49};50Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 51–55why, one step back51const 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 back56Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 57–62why, one step back57const 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 back63 throw new TypeError(`The \`${optionName}\` option must not be an empty array.`);64 }65Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb
lines 66–67why, one step back66 if (!isStdioArray) {67 return;Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 68–69why, one step back68 }69Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 70why, one step back70 for (const {value, optionName} of stdioItems) {Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de
lines 71–72why, one step back71 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 back73 }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 back79const INVALID_STDIO_ARRAY_OPTIONS = new Set(['ignore', 'ipc']);Improve documentation of `stdin`, `stdout`, `stderr` and `stdio` (#626)ehmickybefore adoptionc405105
line 80why, one step back80Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 81–83why, one step back81const 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 back84 }Improve documentation of `stdin`, `stdout`, `stderr` and `stdio` (#626)ehmickybefore adoptionc405105
lines 85–93why, one step back85};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 back94 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 back95 }96};Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
line 97why, one step back97Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc
lines 98–99why, one step back98const 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 back100 const normalizedStdioItems = normalizeGenerators(allStdioItems, direction, options);101 return normalizedStdioItems.map(stdioItem => addStreamProperties(stdioItem, addProperties, direction));102};103Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc
line 104why, one step back104const addInternalStdioItems = ({stdioItems, fdNumber, optionName, options, isSync, direction, stdioState, verboseInfo, outputLines}) => {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 105–108why, one step back105 if (!willPipeFileDescriptor(stdioItems)) {106 return stdioItems;107 }108Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc
lines 109–110why, one step back109 const objectMode = getObjectMode(stdioItems, direction, options);110 return [Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 111why, one step back111 ...stdioItems,Fix using both `encoding: 'utf16le'` and `lines`/`verbose` options (#931)ehmickybefore adoptiona480e1d
line 112why, one step back112 ...handleStreamsEncoding({options, isSync, direction, optionName, objectMode}),Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 113why, one step back113 ...handleStreamsVerbose({stdioItems, options, isSync, stdioState, verboseInfo, fdNumber, optionName}),Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc
line 114why, one step back114 ...handleStreamsLines({options, isSync, direction, optionName, objectMode, outputLines}),Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 115–117why, one step back115 ];116};117Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
lines 118–120why, one step back118// 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 back121const addStreamProperties = (stdioItem, addProperties, direction) => ({122 ...stdioItem,123 ...addProperties[direction][stdioItem.type](stdioItem),124});