validateCLIOptions.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = validateCLIOptions;
  6. exports.DOCUMENTATION_NOTE = void 0;
  7. function _camelcase() {
  8. const data = _interopRequireDefault(require('camelcase'));
  9. _camelcase = function () {
  10. return data;
  11. };
  12. return data;
  13. }
  14. function _chalk() {
  15. const data = _interopRequireDefault(require('chalk'));
  16. _chalk = function () {
  17. return data;
  18. };
  19. return data;
  20. }
  21. var _defaultConfig = _interopRequireDefault(require('./defaultConfig'));
  22. var _deprecated = require('./deprecated');
  23. var _utils = require('./utils');
  24. function _interopRequireDefault(obj) {
  25. return obj && obj.__esModule ? obj : {default: obj};
  26. }
  27. /**
  28. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  29. *
  30. * This source code is licensed under the MIT license found in the
  31. * LICENSE file in the root directory of this source tree.
  32. */
  33. const BULLET = _chalk().default.bold('\u25cf');
  34. const DOCUMENTATION_NOTE = ` ${_chalk().default.bold(
  35. 'CLI Options Documentation:'
  36. )}
  37. https://jestjs.io/docs/en/cli.html
  38. `;
  39. exports.DOCUMENTATION_NOTE = DOCUMENTATION_NOTE;
  40. const createCLIValidationError = (unrecognizedOptions, allowedOptions) => {
  41. let title = `${BULLET} Unrecognized CLI Parameter`;
  42. let message;
  43. const comment =
  44. ` ${_chalk().default.bold('CLI Options Documentation')}:\n` +
  45. ` https://jestjs.io/docs/en/cli.html\n`;
  46. if (unrecognizedOptions.length === 1) {
  47. const unrecognized = unrecognizedOptions[0];
  48. const didYouMeanMessage =
  49. unrecognized.length > 1
  50. ? (0, _utils.createDidYouMeanMessage)(
  51. unrecognized,
  52. Array.from(allowedOptions)
  53. )
  54. : '';
  55. message =
  56. ` Unrecognized option ${_chalk().default.bold(
  57. (0, _utils.format)(unrecognized)
  58. )}.` + (didYouMeanMessage ? ` ${didYouMeanMessage}` : '');
  59. } else {
  60. title += 's';
  61. message =
  62. ` Following options were not recognized:\n` +
  63. ` ${_chalk().default.bold((0, _utils.format)(unrecognizedOptions))}`;
  64. }
  65. return new _utils.ValidationError(title, message, comment);
  66. };
  67. const logDeprecatedOptions = (deprecatedOptions, deprecationEntries, argv) => {
  68. deprecatedOptions.forEach(opt => {
  69. (0, _deprecated.deprecationWarning)(argv, opt, deprecationEntries, {
  70. ..._defaultConfig.default,
  71. comment: DOCUMENTATION_NOTE
  72. });
  73. });
  74. };
  75. function validateCLIOptions(argv, options, rawArgv = []) {
  76. const yargsSpecialOptions = ['$0', '_', 'help', 'h'];
  77. const deprecationEntries = options.deprecationEntries || {};
  78. const allowedOptions = Object.keys(options).reduce(
  79. (acc, option) => acc.add(option).add(options[option].alias || option),
  80. new Set(yargsSpecialOptions)
  81. );
  82. const unrecognizedOptions = Object.keys(argv).filter(
  83. arg =>
  84. !allowedOptions.has((0, _camelcase().default)(arg)) &&
  85. !allowedOptions.has(arg) &&
  86. (!rawArgv.length || rawArgv.includes(arg)),
  87. []
  88. );
  89. if (unrecognizedOptions.length) {
  90. throw createCLIValidationError(unrecognizedOptions, allowedOptions);
  91. }
  92. const CLIDeprecations = Object.keys(deprecationEntries).reduce(
  93. (acc, entry) => {
  94. if (options[entry]) {
  95. acc[entry] = deprecationEntries[entry];
  96. const alias = options[entry].alias;
  97. if (alias) {
  98. acc[alias] = deprecationEntries[entry];
  99. }
  100. }
  101. return acc;
  102. },
  103. {}
  104. );
  105. const deprecations = new Set(Object.keys(CLIDeprecations));
  106. const deprecatedOptions = Object.keys(argv).filter(
  107. arg => deprecations.has(arg) && argv[arg] != null
  108. );
  109. if (deprecatedOptions.length) {
  110. logDeprecatedOptions(deprecatedOptions, CLIDeprecations, argv);
  111. }
  112. return true;
  113. }