utils.js 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
'use strict';

Object.defineProperty(exports, '__esModule', {
  value: true
});
exports.createDidYouMeanMessage = exports.logValidationWarning = exports.ValidationError = exports.formatPrettyObject = exports.format = exports.WARNING = exports.ERROR = exports.DEPRECATION = void 0;

function _chalk() {
  const data = _interopRequireDefault(require('chalk'));

  _chalk = function () {
    return data;
  };

  return data;
}

function _prettyFormat() {
  const data = _interopRequireDefault(require('pretty-format'));

  _prettyFormat = function () {
    return data;
  };

  return data;
}

function _leven() {
  const data = _interopRequireDefault(require('leven'));

  _leven = function () {
    return data;
  };

  return data;
}

function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : {default: obj};
}

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }
  return obj;
}

const BULLET = _chalk().default.bold('\u25cf');

const DEPRECATION = `${BULLET} Deprecation Warning`;
exports.DEPRECATION = DEPRECATION;
const ERROR = `${BULLET} Validation Error`;
exports.ERROR = ERROR;
const WARNING = `${BULLET} Validation Warning`;
exports.WARNING = WARNING;

const format = value =>
  typeof value === 'function'
    ? value.toString()
    : (0, _prettyFormat().default)(value, {
        min: true
      });

exports.format = format;

const formatPrettyObject = value =>
  typeof value === 'function'
    ? value.toString()
    : JSON.stringify(value, null, 2).split('\n').join('\n    ');

exports.formatPrettyObject = formatPrettyObject;

class ValidationError extends Error {
  constructor(name, message, comment) {
    super();

    _defineProperty(this, 'name', void 0);

    _defineProperty(this, 'message', void 0);

    comment = comment ? '\n\n' + comment : '\n';
    this.name = '';
    this.message = _chalk().default.red(
      _chalk().default.bold(name) + ':\n\n' + message + comment
    );
    Error.captureStackTrace(this, () => {});
  }
}

exports.ValidationError = ValidationError;

const logValidationWarning = (name, message, comment) => {
  comment = comment ? '\n\n' + comment : '\n';
  console.warn(
    _chalk().default.yellow(
      _chalk().default.bold(name) + ':\n\n' + message + comment
    )
  );
};

exports.logValidationWarning = logValidationWarning;

const createDidYouMeanMessage = (unrecognized, allowedOptions) => {
  const suggestion = allowedOptions.find(option => {
    const steps = (0, _leven().default)(option, unrecognized);
    return steps < 3;
  });
  return suggestion
    ? `Did you mean ${_chalk().default.bold(format(suggestion))}?`
    : '';
};

exports.createDidYouMeanMessage = createDidYouMeanMessage;