stdio-option.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import {STANDARD_STREAMS_ALIASES} from '../utils/standard-stream.js';
  2. import {normalizeIpcStdioArray} from '../ipc/array.js';
  3. import {isFullVerbose} from '../verbose/values.js';
  4. // Add support for `stdin`/`stdout`/`stderr` as an alias for `stdio`.
  5. // Also normalize the `stdio` option.
  6. export const normalizeStdioOption = ({stdio, ipc, buffer, ...options}, verboseInfo, isSync) => {
  7. const stdioArray = getStdioArray(stdio, options).map((stdioOption, fdNumber) => addDefaultValue(stdioOption, fdNumber));
  8. return isSync
  9. ? normalizeStdioSync(stdioArray, buffer, verboseInfo)
  10. : normalizeIpcStdioArray(stdioArray, ipc);
  11. };
  12. const getStdioArray = (stdio, options) => {
  13. if (stdio === undefined) {
  14. return STANDARD_STREAMS_ALIASES.map(alias => options[alias]);
  15. }
  16. if (hasAlias(options)) {
  17. throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${STANDARD_STREAMS_ALIASES.map(alias => `\`${alias}\``).join(', ')}`);
  18. }
  19. if (typeof stdio === 'string') {
  20. return [stdio, stdio, stdio];
  21. }
  22. if (!Array.isArray(stdio)) {
  23. throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${typeof stdio}\``);
  24. }
  25. const length = Math.max(stdio.length, STANDARD_STREAMS_ALIASES.length);
  26. return Array.from({length}, (_, fdNumber) => stdio[fdNumber]);
  27. };
  28. const hasAlias = options => STANDARD_STREAMS_ALIASES.some(alias => options[alias] !== undefined);
  29. const addDefaultValue = (stdioOption, fdNumber) => {
  30. if (Array.isArray(stdioOption)) {
  31. return stdioOption.map(item => addDefaultValue(item, fdNumber));
  32. }
  33. if (stdioOption === null || stdioOption === undefined) {
  34. return fdNumber >= STANDARD_STREAMS_ALIASES.length ? 'ignore' : 'pipe';
  35. }
  36. return stdioOption;
  37. };
  38. // Using `buffer: false` with synchronous methods implies `stdout`/`stderr`: `ignore`.
  39. // Unless the output is needed, e.g. due to `verbose: 'full'` or to redirecting to a file.
  40. const normalizeStdioSync = (stdioArray, buffer, verboseInfo) => stdioArray.map((stdioOption, fdNumber) =>
  41. !buffer[fdNumber]
  42. && fdNumber !== 0
  43. && !isFullVerbose(verboseInfo, fdNumber)
  44. && isOutputPipeOnly(stdioOption)
  45. ? 'ignore'
  46. : stdioOption);
  47. const isOutputPipeOnly = stdioOption => stdioOption === 'pipe'
  48. || (Array.isArray(stdioOption) && stdioOption.every(item => item === 'pipe'));