options.js 897 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. /**
  3. * Dependencies.
  4. */
  5. const fs = require('fs');
  6. /**
  7. * Export `getOptions`.
  8. */
  9. module.exports = getOptions;
  10. /**
  11. * Get options.
  12. */
  13. function getOptions() {
  14. if (
  15. process.argv.length === 3 &&
  16. (process.argv[2] === '-h' || process.argv[2] === '--help')
  17. ) {
  18. return;
  19. }
  20. const optsPath =
  21. process.argv.indexOf('--opts') === -1
  22. ? 'test/mocha.opts'
  23. : process.argv[process.argv.indexOf('--opts') + 1];
  24. try {
  25. const opts = fs
  26. .readFileSync(optsPath, 'utf8')
  27. .replace(/^#.*$/gm, '')
  28. .replace(/\\\s/g, '%20')
  29. .split(/\s/)
  30. .filter(Boolean)
  31. .map(value => value.replace(/%20/g, ' '));
  32. process.argv = process.argv
  33. .slice(0, 2)
  34. .concat(opts.concat(process.argv.slice(2)));
  35. } catch (ignore) {
  36. // NOTE: should console.error() and throw the error
  37. }
  38. process.env.LOADED_MOCHA_OPTS = true;
  39. }