input-option.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import {isReadableStream} from 'is-stream';
  2. import {isUint8Array} from '../utils/uint-array.js';
  3. import {isUrl, isFilePathString} from './type.js';
  4. // Append the `stdin` option with the `input` and `inputFile` options
  5. export const handleInputOptions = ({input, inputFile}, fdNumber) => fdNumber === 0
  6. ? [
  7. ...handleInputOption(input),
  8. ...handleInputFileOption(inputFile),
  9. ]
  10. : [];
  11. const handleInputOption = input => input === undefined ? [] : [{
  12. type: getInputType(input),
  13. value: input,
  14. optionName: 'input',
  15. }];
  16. const getInputType = input => {
  17. if (isReadableStream(input, {checkOpen: false})) {
  18. return 'nodeStream';
  19. }
  20. if (typeof input === 'string') {
  21. return 'string';
  22. }
  23. if (isUint8Array(input)) {
  24. return 'uint8Array';
  25. }
  26. throw new Error('The `input` option must be a string, a Uint8Array or a Node.js Readable stream.');
  27. };
  28. const handleInputFileOption = inputFile => inputFile === undefined ? [] : [{
  29. ...getInputFileType(inputFile),
  30. optionName: 'inputFile',
  31. }];
  32. const getInputFileType = inputFile => {
  33. if (isUrl(inputFile)) {
  34. return {type: 'fileUrl', value: inputFile};
  35. }
  36. if (isFilePathString(inputFile)) {
  37. return {type: 'filePath', value: {file: inputFile}};
  38. }
  39. throw new Error('The `inputFile` option must be a file path string or a file URL.');
  40. };