validate_cli_options.js 3.7 KB

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