utils.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 = undefined;
  6. var _chalk;
  7. function _load_chalk() {
  8. return (_chalk = _interopRequireDefault(require('chalk')));
  9. }
  10. var _prettyFormat;
  11. function _load_prettyFormat() {
  12. return (_prettyFormat = _interopRequireDefault(require('pretty-format')));
  13. }
  14. var _leven;
  15. function _load_leven() {
  16. return (_leven = _interopRequireDefault(require('leven')));
  17. }
  18. function _interopRequireDefault(obj) {
  19. return obj && obj.__esModule ? obj : {default: obj};
  20. }
  21. const BULLET = (_chalk || _load_chalk()).default.bold('\u25cf');
  22. /**
  23. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  24. *
  25. * This source code is licensed under the MIT license found in the
  26. * LICENSE file in the root directory of this source tree.
  27. *
  28. *
  29. */
  30. const DEPRECATION = (exports.DEPRECATION = `${BULLET} Deprecation Warning`);
  31. const ERROR = (exports.ERROR = `${BULLET} Validation Error`);
  32. const WARNING = (exports.WARNING = `${BULLET} Validation Warning`);
  33. const format = (exports.format = value =>
  34. typeof value === 'function'
  35. ? value.toString()
  36. : (0, (_prettyFormat || _load_prettyFormat()).default)(value, {min: true}));
  37. const formatPrettyObject = (exports.formatPrettyObject = value =>
  38. typeof value === 'function'
  39. ? value.toString()
  40. : JSON.stringify(value, null, 2)
  41. .split('\n')
  42. .join('\n '));
  43. class ValidationError extends Error {
  44. constructor(name, message, comment) {
  45. super();
  46. comment = comment ? '\n\n' + comment : '\n';
  47. this.name = '';
  48. this.message = (_chalk || _load_chalk()).default.red(
  49. (_chalk || _load_chalk()).default.bold(name) + ':\n\n' + message + comment
  50. );
  51. Error.captureStackTrace(this, () => {});
  52. }
  53. }
  54. exports.ValidationError = ValidationError;
  55. const logValidationWarning = (exports.logValidationWarning = (
  56. name,
  57. message,
  58. comment
  59. ) => {
  60. comment = comment ? '\n\n' + comment : '\n';
  61. console.warn(
  62. (_chalk || _load_chalk()).default.yellow(
  63. (_chalk || _load_chalk()).default.bold(name) + ':\n\n' + message + comment
  64. )
  65. );
  66. });
  67. const createDidYouMeanMessage = (exports.createDidYouMeanMessage = (
  68. unrecognized,
  69. allowedOptions
  70. ) => {
  71. const suggestion = allowedOptions.find(option => {
  72. const steps = (0, (_leven || _load_leven()).default)(option, unrecognized);
  73. return steps < 3;
  74. });
  75. return suggestion
  76. ? `Did you mean ${(_chalk || _load_chalk()).default.bold(
  77. format(suggestion)
  78. )}?`
  79. : '';
  80. });