info.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import {isVerbose, VERBOSE_VALUES, isVerboseFunction} from './values.js';
  2. // Information computed before spawning, used by the `verbose` option
  3. export const getVerboseInfo = (verbose, escapedCommand, rawOptions) => {
  4. validateVerbose(verbose);
  5. const commandId = getCommandId(verbose);
  6. return {
  7. verbose,
  8. escapedCommand,
  9. commandId,
  10. rawOptions,
  11. };
  12. };
  13. const getCommandId = verbose => isVerbose({verbose}) ? COMMAND_ID++ : undefined;
  14. // Prepending the `pid` is useful when multiple commands print their output at the same time.
  15. // However, we cannot use the real PID since this is not available with `child_process.spawnSync()`.
  16. // Also, we cannot use the real PID if we want to print it before `child_process.spawn()` is run.
  17. // As a pro, it is shorter than a normal PID and never re-uses the same id.
  18. // As a con, it cannot be used to send signals.
  19. let COMMAND_ID = 0n;
  20. const validateVerbose = verbose => {
  21. for (const fdVerbose of verbose) {
  22. if (fdVerbose === false) {
  23. throw new TypeError('The "verbose: false" option was renamed to "verbose: \'none\'".');
  24. }
  25. if (fdVerbose === true) {
  26. throw new TypeError('The "verbose: true" option was renamed to "verbose: \'short\'".');
  27. }
  28. if (!VERBOSE_VALUES.includes(fdVerbose) && !isVerboseFunction(fdVerbose)) {
  29. const allowedValues = VERBOSE_VALUES.map(allowedValue => `'${allowedValue}'`).join(', ');
  30. throw new TypeError(`The "verbose" option must not be ${fdVerbose}. Allowed values are: ${allowedValues} or a function.`);
  31. }
  32. }
  33. };