input.js
This file
Path
lib/stdio/input.js
36 lines · 1.2 KiB · JavaScript
Last changed by
Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637import {isStream as isNodeStream} from 'is-stream';
// Override the `stdin` option with the `input` optionexport const handleInputOption = (stdioStream, input) => { if (input === undefined || stdioStream.index !== 0) { return stdioStream; }
const optionName = 'input'; validateInputOption(stdioStream.value, optionName); const type = isNodeStream(input) ? 'nodeStream' : 'stringOrBuffer'; return {...stdioStream, value: input, type, optionName};};
// Override the `stdin` option with the `inputFile` optionexport const handleInputFileOption = (stdioStream, inputFile, input) => { if (inputFile === undefined || stdioStream.index !== 0) { return stdioStream; }
if (input !== undefined) { throw new TypeError('The `input` and `inputFile` options cannot be both set.'); }
const optionName = 'inputFile'; validateInputOption(stdioStream.value, optionName); return {...stdioStream, value: inputFile, type: 'filePath', optionName};};
const validateInputOption = (value, optionName) => { if (!CAN_USE_INPUT.has(value)) { throw new TypeError(`The \`${optionName}\` and \`stdin\` options cannot be both set.`); }};
const CAN_USE_INPUT = new Set([undefined, null, 'overlapped', 'pipe']);