validate_cli_options.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 _jestConfig;
  12. function _load_jestConfig() {
  13. return _jestConfig = require('jest-config');
  14. }
  15. var _utils;
  16. function _load_utils() {
  17. return _utils = require('./utils');
  18. }
  19. var _deprecated;
  20. function _load_deprecated() {
  21. return _deprecated = require('./deprecated');
  22. }
  23. var _default_config;
  24. function _load_default_config() {
  25. return _default_config = _interopRequireDefault(require('./default_config'));
  26. }
  27. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  28. /**
  29. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  30. *
  31. * This source code is licensed under the MIT license found in the
  32. * LICENSE file in the root directory of this source tree.
  33. *
  34. *
  35. */
  36. const BULLET = (_chalk || _load_chalk()).default.bold('\u25cf');
  37. const DOCUMENTATION_NOTE = exports.DOCUMENTATION_NOTE = ` ${(_chalk || _load_chalk()).default.bold('CLI Options Documentation:')}
  38. https://facebook.github.io/jest/docs/en/cli.html
  39. `;
  40. const createCLIValidationError = (unrecognizedOptions, allowedOptions) => {
  41. let title = `${BULLET} Unrecognized CLI Parameter`;
  42. let message;
  43. const comment = ` ${(_chalk || _load_chalk()).default.bold('CLI Options Documentation')}:\n` + ` https://facebook.github.io/jest/docs/en/cli.html\n`;
  44. if (unrecognizedOptions.length === 1) {
  45. const unrecognized = unrecognizedOptions[0];
  46. const didYouMeanMessage = (0, (_utils || _load_utils()).createDidYouMeanMessage)(unrecognized, Array.from(allowedOptions));
  47. message = ` Unrecognized option ${(_chalk || _load_chalk()).default.bold((0, (_utils || _load_utils()).format)(unrecognized))}.` + (didYouMeanMessage ? ` ${didYouMeanMessage}` : '');
  48. } else {
  49. title += 's';
  50. message = ` Following options were not recognized:\n` + ` ${(_chalk || _load_chalk()).default.bold((0, (_utils || _load_utils()).format)(unrecognizedOptions))}`;
  51. }
  52. return new (_utils || _load_utils()).ValidationError(title, message, comment);
  53. };
  54. const logDeprecatedOptions = (deprecatedOptions, deprecationEntries, argv) => {
  55. deprecatedOptions.forEach(opt => {
  56. (0, (_deprecated || _load_deprecated()).deprecationWarning)(argv, opt, deprecationEntries, Object.assign({}, (_default_config || _load_default_config()).default, {
  57. comment: DOCUMENTATION_NOTE
  58. }));
  59. });
  60. };
  61. function validateCLIOptions(argv, options) {
  62. const yargsSpecialOptions = ['$0', '_', 'help', 'h'];
  63. const allowedOptions = Object.keys(options).reduce((acc, option) => acc.add(option).add(options[option].alias || option), new Set(yargsSpecialOptions));
  64. const unrecognizedOptions = Object.keys(argv).filter(arg => !allowedOptions.has(arg));
  65. if (unrecognizedOptions.length) {
  66. throw createCLIValidationError(unrecognizedOptions, allowedOptions);
  67. }
  68. const CLIDeprecations = Object.keys((_jestConfig || _load_jestConfig()).deprecationEntries).reduce((acc, entry) => {
  69. if (options[entry]) {
  70. acc[entry] = (_jestConfig || _load_jestConfig()).deprecationEntries[entry];
  71. if (options[entry].alias) {
  72. acc[options[entry].alias] = (_jestConfig || _load_jestConfig()).deprecationEntries[entry];
  73. }
  74. }
  75. return acc;
  76. }, {});
  77. const deprecations = new Set(Object.keys(CLIDeprecations));
  78. const deprecatedOptions = Object.keys(argv).filter(arg => deprecations.has(arg) && argv[arg] != null);
  79. if (deprecatedOptions.length) {
  80. logDeprecatedOptions(deprecatedOptions, CLIDeprecations, argv);
  81. }
  82. return true;
  83. }