UNPKG

2.82 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.logValidationWarning =
7 exports.formatPrettyObject =
8 exports.format =
9 exports.createDidYouMeanMessage =
10 exports.WARNING =
11 exports.ValidationError =
12 exports.ERROR =
13 exports.DEPRECATION =
14 void 0;
15function _chalk() {
16 const data = _interopRequireDefault(require('chalk'));
17 _chalk = function () {
18 return data;
19 };
20 return data;
21}
22function _leven() {
23 const data = _interopRequireDefault(require('leven'));
24 _leven = function () {
25 return data;
26 };
27 return data;
28}
29function _prettyFormat() {
30 const data = require('pretty-format');
31 _prettyFormat = function () {
32 return data;
33 };
34 return data;
35}
36function _interopRequireDefault(obj) {
37 return obj && obj.__esModule ? obj : {default: obj};
38}
39/**
40 * Copyright (c) Meta Platforms, Inc. and affiliates.
41 *
42 * This source code is licensed under the MIT license found in the
43 * LICENSE file in the root directory of this source tree.
44 */
45
46const BULLET = _chalk().default.bold('\u25cf');
47const DEPRECATION = `${BULLET} Deprecation Warning`;
48exports.DEPRECATION = DEPRECATION;
49const ERROR = `${BULLET} Validation Error`;
50exports.ERROR = ERROR;
51const WARNING = `${BULLET} Validation Warning`;
52exports.WARNING = WARNING;
53const format = value =>
54 typeof value === 'function'
55 ? value.toString()
56 : (0, _prettyFormat().format)(value, {
57 min: true
58 });
59exports.format = format;
60const formatPrettyObject = value =>
61 typeof value === 'function'
62 ? value.toString()
63 : typeof value === 'undefined'
64 ? 'undefined'
65 : JSON.stringify(value, null, 2).split('\n').join('\n ');
66exports.formatPrettyObject = formatPrettyObject;
67class ValidationError extends Error {
68 name;
69 message;
70 constructor(name, message, comment) {
71 super();
72 comment = comment ? `\n\n${comment}` : '\n';
73 this.name = '';
74 this.message = _chalk().default.red(
75 `${_chalk().default.bold(name)}:\n\n${message}${comment}`
76 );
77 // eslint-disable-next-line @typescript-eslint/no-empty-function
78 Error.captureStackTrace(this, () => {});
79 }
80}
81exports.ValidationError = ValidationError;
82const logValidationWarning = (name, message, comment) => {
83 comment = comment ? `\n\n${comment}` : '\n';
84 console.warn(
85 _chalk().default.yellow(
86 `${_chalk().default.bold(name)}:\n\n${message}${comment}`
87 )
88 );
89};
90exports.logValidationWarning = logValidationWarning;
91const createDidYouMeanMessage = (unrecognized, allowedOptions) => {
92 const suggestion = allowedOptions.find(option => {
93 const steps = (0, _leven().default)(option, unrecognized);
94 return steps < 3;
95 });
96 return suggestion
97 ? `Did you mean ${_chalk().default.bold(format(suggestion))}?`
98 : '';
99};
100exports.createDidYouMeanMessage = createDidYouMeanMessage;