specific.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import {debuglog} from 'node:util';
  2. import isPlainObject from 'is-plain-obj';
  3. import {STANDARD_STREAMS_ALIASES} from '../utils/standard-stream.js';
  4. // Some options can have different values for `stdout`/`stderr`/`fd3`.
  5. // This normalizes those to array of values.
  6. // For example, `{verbose: {stdout: 'none', stderr: 'full'}}` becomes `{verbose: ['none', 'none', 'full']}`
  7. export const normalizeFdSpecificOptions = options => {
  8. const optionsCopy = {...options};
  9. for (const optionName of FD_SPECIFIC_OPTIONS) {
  10. optionsCopy[optionName] = normalizeFdSpecificOption(options, optionName);
  11. }
  12. return optionsCopy;
  13. };
  14. export const normalizeFdSpecificOption = (options, optionName) => {
  15. const optionBaseArray = Array.from({length: getStdioLength(options) + 1});
  16. const optionArray = normalizeFdSpecificValue(options[optionName], optionBaseArray, optionName);
  17. return addDefaultValue(optionArray, optionName);
  18. };
  19. const getStdioLength = ({stdio}) => Array.isArray(stdio)
  20. ? Math.max(stdio.length, STANDARD_STREAMS_ALIASES.length)
  21. : STANDARD_STREAMS_ALIASES.length;
  22. const normalizeFdSpecificValue = (optionValue, optionArray, optionName) => isPlainObject(optionValue)
  23. ? normalizeOptionObject(optionValue, optionArray, optionName)
  24. : optionArray.fill(optionValue);
  25. const normalizeOptionObject = (optionValue, optionArray, optionName) => {
  26. for (const fdName of Object.keys(optionValue).sort(compareFdName)) {
  27. for (const fdNumber of parseFdName(fdName, optionName, optionArray)) {
  28. optionArray[fdNumber] = optionValue[fdName];
  29. }
  30. }
  31. return optionArray;
  32. };
  33. // Ensure priority order when setting both `stdout`/`stderr`, `fd1`/`fd2`, and `all`
  34. const compareFdName = (fdNameA, fdNameB) => getFdNameOrder(fdNameA) < getFdNameOrder(fdNameB) ? 1 : -1;
  35. const getFdNameOrder = fdName => {
  36. if (fdName === 'stdout' || fdName === 'stderr') {
  37. return 0;
  38. }
  39. return fdName === 'all' ? 2 : 1;
  40. };
  41. const parseFdName = (fdName, optionName, optionArray) => {
  42. if (fdName === 'ipc') {
  43. return [optionArray.length - 1];
  44. }
  45. const fdNumber = parseFd(fdName);
  46. if (fdNumber === undefined || fdNumber === 0) {
  47. throw new TypeError(`"${optionName}.${fdName}" is invalid.
  48. It must be "${optionName}.stdout", "${optionName}.stderr", "${optionName}.all", "${optionName}.ipc", or "${optionName}.fd3", "${optionName}.fd4" (and so on).`);
  49. }
  50. if (fdNumber >= optionArray.length) {
  51. throw new TypeError(`"${optionName}.${fdName}" is invalid: that file descriptor does not exist.
  52. Please set the "stdio" option to ensure that file descriptor exists.`);
  53. }
  54. return fdNumber === 'all' ? [1, 2] : [fdNumber];
  55. };
  56. // Use the same syntax for fd-specific options and the `from`/`to` options
  57. export const parseFd = fdName => {
  58. if (fdName === 'all') {
  59. return fdName;
  60. }
  61. if (STANDARD_STREAMS_ALIASES.includes(fdName)) {
  62. return STANDARD_STREAMS_ALIASES.indexOf(fdName);
  63. }
  64. const regexpResult = FD_REGEXP.exec(fdName);
  65. if (regexpResult !== null) {
  66. return Number(regexpResult[1]);
  67. }
  68. };
  69. const FD_REGEXP = /^fd(\d+)$/;
  70. const addDefaultValue = (optionArray, optionName) => optionArray.map(optionValue => optionValue === undefined
  71. ? DEFAULT_OPTIONS[optionName]
  72. : optionValue);
  73. // Default value for the `verbose` option
  74. const verboseDefault = debuglog('execa').enabled ? 'full' : 'none';
  75. const DEFAULT_OPTIONS = {
  76. lines: false,
  77. buffer: true,
  78. maxBuffer: 1000 * 1000 * 100,
  79. verbose: verboseDefault,
  80. stripFinalNewline: true,
  81. };
  82. // List of options which can have different values for `stdout`/`stderr`
  83. export const FD_SPECIFIC_OPTIONS = ['lines', 'buffer', 'maxBuffer', 'verbose', 'stripFinalNewline'];
  84. // Retrieve fd-specific option
  85. export const getFdSpecificValue = (optionArray, fdNumber) => fdNumber === 'ipc'
  86. ? optionArray.at(-1)
  87. : optionArray[fdNumber];