input-sync.js
This file
Path
lib/stdio/input-sync.js
45 lines · 1.9 KiB · JavaScript
Last changed by
Simplify implementation (#955)ehmickybefore adoption8ad9ee7
History of this fileTake it
Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 1–11why, one step back1import {joinToUint8Array, isUint8Array} from './uint-array.js';2import {TYPE_TO_MESSAGE} from './type.js';3import {getGenerators, runGeneratorsSync} from './generator.js';45// Apply `stdin`/`input`/`inputFile` options, before spawning, in sync mode, by converting it to the `input` option6export const addInputOptionsSync = (fileDescriptors, options) => {7 for (const fdNumber of getInputFdNumbers(fileDescriptors)) {8 addInputOptionSync(fileDescriptors, fdNumber, options);9 }10};11Simplify implementation (#955)ehmickybefore adoption8ad9ee7
lines 12–14why, one step back12const getInputFdNumbers = fileDescriptors => new Set(Object.entries(fileDescriptors)13 .filter(([, {direction}]) => direction === 'input')14 .map(([fdNumber]) => Number(fdNumber)));Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 15–16why, one step back1516const addInputOptionSync = (fileDescriptors, fdNumber, options) => {Simplify implementation (#955)ehmickybefore adoption8ad9ee7
lines 17–18why, one step back17 const {stdioItems} = fileDescriptors[fdNumber];18 const allStdioItems = stdioItems.filter(({contents}) => contents !== undefined);Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 19–28why, one step back19 if (allStdioItems.length === 0) {20 return;21 }2223 if (fdNumber !== 0) {24 const [{type, optionName}] = allStdioItems;25 throw new TypeError(`Only the \`stdin\` option, not \`${optionName}\`, can be ${TYPE_TO_MESSAGE[type]} with synchronous methods.`);26 }2728 const allContents = allStdioItems.map(({contents}) => contents);Simplify implementation (#955)ehmickybefore adoption8ad9ee7
line 29why, one step back29 const transformedContents = allContents.map(contents => applySingleInputGeneratorsSync(contents, stdioItems));Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 30–32why, one step back30 options.input = joinToUint8Array(transformedContents);31};32Simplify implementation (#955)ehmickybefore adoption8ad9ee7
lines 33–34why, one step back33const applySingleInputGeneratorsSync = (contents, stdioItems) => {34 const generators = getGenerators(stdioItems).reverse();Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 35–45why, one step back35 const newContents = runGeneratorsSync(contents, generators);36 validateSerializable(newContents);37 return joinToUint8Array(newContents);38};3940const validateSerializable = newContents => {41 const invalidItem = newContents.find(item => typeof item !== 'string' && !isUint8Array(item));42 if (invalidItem !== undefined) {43 throw new TypeError(`The \`stdin\` option is invalid: when passing objects as input, a transform must be used to serialize them to strings or Uint8Arrays: ${invalidItem}.`);44 }45};