validateCLIOptions.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = validateCLIOptions;
  6. exports.DOCUMENTATION_NOTE = void 0;
  7. function _chalk() {
  8. const data = _interopRequireDefault(require('chalk'));
  9. _chalk = function _chalk() {
  10. return data;
  11. };
  12. return data;
  13. }
  14. function _camelcase() {
  15. const data = _interopRequireDefault(require('camelcase'));
  16. _camelcase = function _camelcase() {
  17. return data;
  18. };
  19. return data;
  20. }
  21. var _utils = require('./utils');
  22. var _deprecated = require('./deprecated');
  23. var _defaultConfig = _interopRequireDefault(require('./defaultConfig'));
  24. function _interopRequireDefault(obj) {
  25. return obj && obj.__esModule ? obj : {default: obj};
  26. }
  27. function _objectSpread(target) {
  28. for (var i = 1; i < arguments.length; i++) {
  29. var source = arguments[i] != null ? arguments[i] : {};
  30. var ownKeys = Object.keys(source);
  31. if (typeof Object.getOwnPropertySymbols === 'function') {
  32. ownKeys = ownKeys.concat(
  33. Object.getOwnPropertySymbols(source).filter(function(sym) {
  34. return Object.getOwnPropertyDescriptor(source, sym).enumerable;
  35. })
  36. );
  37. }
  38. ownKeys.forEach(function(key) {
  39. _defineProperty(target, key, source[key]);
  40. });
  41. }
  42. return target;
  43. }
  44. function _defineProperty(obj, key, value) {
  45. if (key in obj) {
  46. Object.defineProperty(obj, key, {
  47. value: value,
  48. enumerable: true,
  49. configurable: true,
  50. writable: true
  51. });
  52. } else {
  53. obj[key] = value;
  54. }
  55. return obj;
  56. }
  57. const BULLET = _chalk().default.bold('\u25cf');
  58. const DOCUMENTATION_NOTE = ` ${_chalk().default.bold(
  59. 'CLI Options Documentation:'
  60. )}
  61. https://jestjs.io/docs/en/cli.html
  62. `;
  63. exports.DOCUMENTATION_NOTE = DOCUMENTATION_NOTE;
  64. const createCLIValidationError = (unrecognizedOptions, allowedOptions) => {
  65. let title = `${BULLET} Unrecognized CLI Parameter`;
  66. let message;
  67. const comment =
  68. ` ${_chalk().default.bold('CLI Options Documentation')}:\n` +
  69. ` https://jestjs.io/docs/en/cli.html\n`;
  70. if (unrecognizedOptions.length === 1) {
  71. const unrecognized = unrecognizedOptions[0];
  72. const didYouMeanMessage = (0, _utils.createDidYouMeanMessage)(
  73. unrecognized,
  74. Array.from(allowedOptions)
  75. );
  76. message =
  77. ` Unrecognized option ${_chalk().default.bold(
  78. (0, _utils.format)(unrecognized)
  79. )}.` + (didYouMeanMessage ? ` ${didYouMeanMessage}` : '');
  80. } else {
  81. title += 's';
  82. message =
  83. ` Following options were not recognized:\n` +
  84. ` ${_chalk().default.bold((0, _utils.format)(unrecognizedOptions))}`;
  85. }
  86. return new _utils.ValidationError(title, message, comment);
  87. };
  88. const logDeprecatedOptions = (deprecatedOptions, deprecationEntries, argv) => {
  89. deprecatedOptions.forEach(opt => {
  90. (0, _deprecated.deprecationWarning)(
  91. argv,
  92. opt,
  93. deprecationEntries,
  94. _objectSpread({}, _defaultConfig.default, {
  95. comment: DOCUMENTATION_NOTE
  96. })
  97. );
  98. });
  99. };
  100. function validateCLIOptions(argv, options, rawArgv = []) {
  101. const yargsSpecialOptions = ['$0', '_', 'help', 'h'];
  102. const deprecationEntries = options.deprecationEntries || {};
  103. const allowedOptions = Object.keys(options).reduce(
  104. (acc, option) => acc.add(option).add(options[option].alias || option),
  105. new Set(yargsSpecialOptions)
  106. );
  107. const unrecognizedOptions = Object.keys(argv).filter(
  108. arg =>
  109. !allowedOptions.has((0, _camelcase().default)(arg)) &&
  110. (!rawArgv.length || rawArgv.includes(arg)),
  111. []
  112. );
  113. if (unrecognizedOptions.length) {
  114. throw createCLIValidationError(unrecognizedOptions, allowedOptions);
  115. }
  116. const CLIDeprecations = Object.keys(deprecationEntries).reduce(
  117. (acc, entry) => {
  118. if (options[entry]) {
  119. acc[entry] = deprecationEntries[entry];
  120. const alias = options[entry].alias;
  121. if (alias) {
  122. acc[alias] = deprecationEntries[entry];
  123. }
  124. }
  125. return acc;
  126. },
  127. {}
  128. );
  129. const deprecations = new Set(Object.keys(CLIDeprecations));
  130. const deprecatedOptions = Object.keys(argv).filter(
  131. arg => deprecations.has(arg) && argv[arg] != null
  132. );
  133. if (deprecatedOptions.length) {
  134. logDeprecatedOptions(deprecatedOptions, CLIDeprecations, argv);
  135. }
  136. return true;
  137. }