1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.DOCUMENTATION_NOTE = void 0;
|
7 | exports.default = validateCLIOptions;
|
8 | function _camelcase() {
|
9 | const data = _interopRequireDefault(require('camelcase'));
|
10 | _camelcase = function () {
|
11 | return data;
|
12 | };
|
13 | return data;
|
14 | }
|
15 | function _chalk() {
|
16 | const data = _interopRequireDefault(require('chalk'));
|
17 | _chalk = function () {
|
18 | return data;
|
19 | };
|
20 | return data;
|
21 | }
|
22 | var _utils = require('./utils');
|
23 | function _interopRequireDefault(obj) {
|
24 | return obj && obj.__esModule ? obj : {default: obj};
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
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/cli
|
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/cli\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 = ` Unrecognized option ${_chalk().default.bold(
|
56 | (0, _utils.format)(unrecognized)
|
57 | )}.${didYouMeanMessage ? ` ${didYouMeanMessage}` : ''}`;
|
58 | } else {
|
59 | title += 's';
|
60 | message =
|
61 | ' Following options were not recognized:\n' +
|
62 | ` ${_chalk().default.bold((0, _utils.format)(unrecognizedOptions))}`;
|
63 | }
|
64 | return new _utils.ValidationError(title, message, comment);
|
65 | };
|
66 | const validateDeprecatedOptions = (
|
67 | deprecatedOptions,
|
68 | deprecationEntries,
|
69 | argv
|
70 | ) => {
|
71 | deprecatedOptions.forEach(opt => {
|
72 | const name = opt.name;
|
73 | const message = deprecationEntries[name](argv);
|
74 | const comment = DOCUMENTATION_NOTE;
|
75 | if (opt.fatal) {
|
76 | throw new _utils.ValidationError(name, message, comment);
|
77 | } else {
|
78 | (0, _utils.logValidationWarning)(name, message, comment);
|
79 | }
|
80 | });
|
81 | };
|
82 | function validateCLIOptions(argv, options = {}, rawArgv = []) {
|
83 | const yargsSpecialOptions = ['$0', '_', 'help', 'h'];
|
84 | const allowedOptions = Object.keys(options).reduce(
|
85 | (acc, option) => acc.add(option).add(options[option].alias || option),
|
86 | new Set(yargsSpecialOptions)
|
87 | );
|
88 | const deprecationEntries = options.deprecationEntries ?? {};
|
89 | const CLIDeprecations = Object.keys(deprecationEntries).reduce(
|
90 | (acc, entry) => {
|
91 | acc[entry] = deprecationEntries[entry];
|
92 | if (options[entry]) {
|
93 | const alias = options[entry].alias;
|
94 | if (alias) {
|
95 | acc[alias] = deprecationEntries[entry];
|
96 | }
|
97 | }
|
98 | return acc;
|
99 | },
|
100 | {}
|
101 | );
|
102 | const deprecations = new Set(Object.keys(CLIDeprecations));
|
103 | const deprecatedOptions = Object.keys(argv)
|
104 | .filter(arg => deprecations.has(arg) && argv[arg] != null)
|
105 | .map(arg => ({
|
106 | fatal: !allowedOptions.has(arg),
|
107 | name: arg
|
108 | }));
|
109 | if (deprecatedOptions.length) {
|
110 | validateDeprecatedOptions(deprecatedOptions, CLIDeprecations, argv);
|
111 | }
|
112 | const unrecognizedOptions = Object.keys(argv).filter(
|
113 | arg =>
|
114 | !allowedOptions.has(
|
115 | (0, _camelcase().default)(arg, {
|
116 | locale: 'en-US'
|
117 | })
|
118 | ) &&
|
119 | !allowedOptions.has(arg) &&
|
120 | (!rawArgv.length || rawArgv.includes(arg)),
|
121 | []
|
122 | );
|
123 | if (unrecognizedOptions.length) {
|
124 | throw createCLIValidationError(unrecognizedOptions, allowedOptions);
|
125 | }
|
126 | return true;
|
127 | }
|