utils.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createDidYouMeanMessage = exports.logValidationWarning = exports.ValidationError = 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) { return obj && obj.__esModule ? obj : { default: obj }; }
  19. const BULLET = (_chalk || _load_chalk()).default.bold('\u25cf'); /**
  20. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  21. *
  22. * This source code is licensed under the MIT license found in the
  23. * LICENSE file in the root directory of this source tree.
  24. *
  25. *
  26. */
  27. const DEPRECATION = exports.DEPRECATION = `${BULLET} Deprecation Warning`;
  28. const ERROR = exports.ERROR = `${BULLET} Validation Error`;
  29. const WARNING = exports.WARNING = `${BULLET} Validation Warning`;
  30. const format = exports.format = value => typeof value === 'function' ? value.toString() : (0, (_prettyFormat || _load_prettyFormat()).default)(value, { min: true });
  31. class ValidationError extends Error {
  32. constructor(name, message, comment) {
  33. super();
  34. comment = comment ? '\n\n' + comment : '\n';
  35. this.name = '';
  36. this.message = (_chalk || _load_chalk()).default.red((_chalk || _load_chalk()).default.bold(name) + ':\n\n' + message + comment);
  37. Error.captureStackTrace(this, () => {});
  38. }
  39. }
  40. exports.ValidationError = ValidationError;
  41. const logValidationWarning = exports.logValidationWarning = (name, message, comment) => {
  42. comment = comment ? '\n\n' + comment : '\n';
  43. console.warn((_chalk || _load_chalk()).default.yellow((_chalk || _load_chalk()).default.bold(name) + ':\n\n' + message + comment));
  44. };
  45. const createDidYouMeanMessage = exports.createDidYouMeanMessage = (unrecognized, allowedOptions) => {
  46. const suggestion = allowedOptions.find(option => {
  47. const steps = (0, (_leven || _load_leven()).default)(option, unrecognized);
  48. return steps < 3;
  49. });
  50. return suggestion ? `Did you mean ${(_chalk || _load_chalk()).default.bold(format(suggestion))}?` : '';
  51. };