utils.js 2.8 KB

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