UNPKG

2.86 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.createDidYouMeanMessage =
7 exports.logValidationWarning =
8 exports.ValidationError =
9 exports.formatPrettyObject =
10 exports.format =
11 exports.WARNING =
12 exports.ERROR =
13 exports.DEPRECATION =
14 void 0;
15
16function _chalk() {
17 const data = _interopRequireDefault(require('chalk'));
18
19 _chalk = function () {
20 return data;
21 };
22
23 return data;
24}
25
26function _leven() {
27 const data = _interopRequireDefault(require('leven'));
28
29 _leven = function () {
30 return data;
31 };
32
33 return data;
34}
35
36function _prettyFormat() {
37 const data = require('pretty-format');
38
39 _prettyFormat = function () {
40 return data;
41 };
42
43 return data;
44}
45
46function _interopRequireDefault(obj) {
47 return obj && obj.__esModule ? obj : {default: obj};
48}
49
50function _defineProperty(obj, key, value) {
51 if (key in obj) {
52 Object.defineProperty(obj, key, {
53 value: value,
54 enumerable: true,
55 configurable: true,
56 writable: true
57 });
58 } else {
59 obj[key] = value;
60 }
61 return obj;
62}
63
64const BULLET = _chalk().default.bold('\u25cf');
65
66const DEPRECATION = `${BULLET} Deprecation Warning`;
67exports.DEPRECATION = DEPRECATION;
68const ERROR = `${BULLET} Validation Error`;
69exports.ERROR = ERROR;
70const WARNING = `${BULLET} Validation Warning`;
71exports.WARNING = WARNING;
72
73const format = value =>
74 typeof value === 'function'
75 ? value.toString()
76 : (0, _prettyFormat().format)(value, {
77 min: true
78 });
79
80exports.format = format;
81
82const formatPrettyObject = value =>
83 typeof value === 'function'
84 ? value.toString()
85 : JSON.stringify(value, null, 2).split('\n').join('\n ');
86
87exports.formatPrettyObject = formatPrettyObject;
88
89class ValidationError extends Error {
90 constructor(name, message, comment) {
91 super();
92
93 _defineProperty(this, 'name', void 0);
94
95 _defineProperty(this, 'message', void 0);
96
97 comment = comment ? '\n\n' + comment : '\n';
98 this.name = '';
99 this.message = _chalk().default.red(
100 _chalk().default.bold(name) + ':\n\n' + message + comment
101 );
102 Error.captureStackTrace(this, () => {});
103 }
104}
105
106exports.ValidationError = ValidationError;
107
108const logValidationWarning = (name, message, comment) => {
109 comment = comment ? '\n\n' + comment : '\n';
110 console.warn(
111 _chalk().default.yellow(
112 _chalk().default.bold(name) + ':\n\n' + message + comment
113 )
114 );
115};
116
117exports.logValidationWarning = logValidationWarning;
118
119const createDidYouMeanMessage = (unrecognized, allowedOptions) => {
120 const suggestion = allowedOptions.find(option => {
121 const steps = (0, _leven().default)(option, unrecognized);
122 return steps < 3;
123 });
124 return suggestion
125 ? `Did you mean ${_chalk().default.bold(format(suggestion))}?`
126 : '';
127};
128
129exports.createDidYouMeanMessage = createDidYouMeanMessage;