run-option-metadata.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. 'use strict';
  2. /**
  3. * Metadata about various options of the `run` command
  4. * @see module:lib/cli/run
  5. * @module
  6. * @private
  7. */
  8. /**
  9. * Dictionary of yargs option types to list of options having said type
  10. * @type {Record<string, string[]>}
  11. * @private
  12. */
  13. const TYPES = (exports.types = {
  14. array: [
  15. 'extension',
  16. 'file',
  17. 'global',
  18. 'ignore',
  19. 'node-option',
  20. 'reporter-option',
  21. 'require',
  22. 'spec',
  23. 'watch-files',
  24. 'watch-ignore'
  25. ],
  26. boolean: [
  27. 'allow-uncaught',
  28. 'async-only',
  29. 'bail',
  30. 'check-leaks',
  31. 'color',
  32. 'delay',
  33. 'diff',
  34. 'dry-run',
  35. 'exit',
  36. 'pass-on-failing-test-suite',
  37. 'fail-zero',
  38. 'forbid-only',
  39. 'forbid-pending',
  40. 'full-trace',
  41. 'inline-diffs',
  42. 'invert',
  43. 'list-interfaces',
  44. 'list-reporters',
  45. 'no-colors',
  46. 'parallel',
  47. 'posix-exit-codes',
  48. 'recursive',
  49. 'sort',
  50. 'watch'
  51. ],
  52. number: ['retries', 'jobs'],
  53. string: [
  54. 'config',
  55. 'fgrep',
  56. 'grep',
  57. 'package',
  58. 'reporter',
  59. 'ui',
  60. 'slow',
  61. 'timeout'
  62. ]
  63. });
  64. /**
  65. * Option aliases keyed by canonical option name.
  66. * Arrays used to reduce
  67. * @type {Record<string, string[]>}
  68. * @private
  69. */
  70. exports.aliases = {
  71. 'async-only': ['A'],
  72. bail: ['b'],
  73. color: ['c', 'colors'],
  74. fgrep: ['f'],
  75. global: ['globals'],
  76. grep: ['g'],
  77. ignore: ['exclude'],
  78. invert: ['i'],
  79. jobs: ['j'],
  80. 'no-colors': ['C'],
  81. 'node-option': ['n'],
  82. parallel: ['p'],
  83. reporter: ['R'],
  84. 'reporter-option': ['reporter-options', 'O'],
  85. require: ['r'],
  86. slow: ['s'],
  87. sort: ['S'],
  88. timeout: ['t', 'timeouts'],
  89. ui: ['u'],
  90. watch: ['w']
  91. };
  92. const ALL_MOCHA_FLAGS = Object.keys(TYPES).reduce((acc, key) => {
  93. // gets all flags from each of the fields in `types`, adds those,
  94. // then adds aliases of each flag (if any)
  95. TYPES[key].forEach(flag => {
  96. acc.add(flag);
  97. const aliases = exports.aliases[flag] || [];
  98. aliases.forEach(alias => {
  99. acc.add(alias);
  100. });
  101. });
  102. return acc;
  103. }, new Set());
  104. /**
  105. * Returns `true` if the provided `flag` is known to Mocha.
  106. * @param {string} flag - Flag to check
  107. * @returns {boolean} If `true`, this is a Mocha flag
  108. * @private
  109. */
  110. exports.isMochaFlag = flag => {
  111. return ALL_MOCHA_FLAGS.has(flag.replace(/^--?/, ''));
  112. };
  113. /**
  114. * Returns expected yarg option type for a given mocha flag.
  115. * @param {string} flag - Flag to check (can be with or without leading dashes "--"")
  116. * @returns {string | undefined} - If flag is a valid mocha flag, the expected type of argument for this flag is returned, otherwise undefined is returned.
  117. * @private
  118. */
  119. exports.expectedTypeForFlag = flag => {
  120. const normalizedName = flag.replace(/^--?/, '');
  121. // If flag is an alias, get it's full name.
  122. const aliases = exports.aliases;
  123. const fullFlagName =
  124. Object.keys(aliases).find(flagName =>
  125. aliases[flagName].includes(normalizedName)
  126. ) || normalizedName;
  127. return Object.keys(TYPES).find(flagType =>
  128. TYPES[flagType].includes(fullFlagName)
  129. );
  130. };