input-option.js
This file
Path
lib/stdio/input-option.js
50 lines · 1.3 KiB · JavaScript
Last changed by
Rename files (#977)ehmickybefore adoption4b2e316
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–50why, one step back1import {isReadableStream} from 'is-stream';2import {isUint8Array} from '../utils/uint-array.js';3import {isUrl, isFilePathString} from './type.js';45// Append the `stdin` option with the `input` and `inputFile` options6export const handleInputOptions = ({input, inputFile}, fdNumber) => fdNumber === 07 ? [8 ...handleInputOption(input),9 ...handleInputFileOption(inputFile),10 ]11 : [];1213const handleInputOption = input => input === undefined ? [] : [{14 type: getInputType(input),15 value: input,16 optionName: 'input',17}];1819const getInputType = input => {20 if (isReadableStream(input, {checkOpen: false})) {21 return 'nodeStream';22 }2324 if (typeof input === 'string') {25 return 'string';26 }2728 if (isUint8Array(input)) {29 return 'uint8Array';30 }3132 throw new Error('The `input` option must be a string, a Uint8Array or a Node.js Readable stream.');33};3435const handleInputFileOption = inputFile => inputFile === undefined ? [] : [{36 ...getInputFileType(inputFile),37 optionName: 'inputFile',38}];3940const getInputFileType = inputFile => {41 if (isUrl(inputFile)) {42 return {type: 'fileUrl', value: inputFile};43 }4445 if (isFilePathString(inputFile)) {46 return {type: 'filePath', value: {file: inputFile}};47 }4849 throw new Error('The `inputFile` option must be a file path string or a file URL.');50};