format.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.formatResult = exports.format = void 0;
  7. const chalk_1 = __importDefault(require("chalk"));
  8. const DEFAULT_SIGNS = [' ', '⚠', '✖'];
  9. const DEFAULT_COLORS = ['white', 'yellow', 'red'];
  10. function format(report = {}, options = {}) {
  11. const { results = [] } = report;
  12. const fi = (result) => formatInput(result, options);
  13. const fr = (result) => formatResult(result, options);
  14. return results
  15. .filter((r) => Array.isArray(r.warnings) || Array.isArray(r.errors))
  16. .map((result) => [...fi(result), ...fr(result)])
  17. .reduce((acc, item) => (Array.isArray(item) ? [...acc, ...item] : [...acc, item]), [])
  18. .join('\n');
  19. }
  20. exports.format = format;
  21. function formatInput(result, options = {}) {
  22. const { color: enabled = true } = options;
  23. const { errors = [], warnings = [], input = '' } = result;
  24. if (!input) {
  25. return [''];
  26. }
  27. const sign = '⧗';
  28. const decoration = enabled ? chalk_1.default.gray(sign) : sign;
  29. const commitText = errors.length > 0 ? input : input.split('\n')[0];
  30. const decoratedInput = enabled ? chalk_1.default.bold(commitText) : commitText;
  31. const hasProblems = errors.length > 0 || warnings.length > 0;
  32. return options.verbose || hasProblems
  33. ? [`${decoration} input: ${decoratedInput}`]
  34. : [];
  35. }
  36. function formatResult(result = {}, options = {}) {
  37. const { signs = DEFAULT_SIGNS, colors = DEFAULT_COLORS, color: enabled = true, } = options;
  38. const { errors = [], warnings = [] } = result;
  39. const problems = [...errors, ...warnings].map((problem) => {
  40. const sign = signs[problem.level] || '';
  41. const color = colors[problem.level] || 'white';
  42. const decoration = enabled ? chalk_1.default[color](sign) : sign;
  43. const name = enabled
  44. ? chalk_1.default.grey(`[${problem.name}]`)
  45. : `[${problem.name}]`;
  46. return `${decoration} ${problem.message} ${name}`;
  47. });
  48. const sign = selectSign(result);
  49. const color = selectColor(result);
  50. const deco = enabled ? chalk_1.default[color](sign) : sign;
  51. const el = errors.length;
  52. const wl = warnings.length;
  53. const hasProblems = problems.length > 0;
  54. const summary = options.verbose || hasProblems
  55. ? `${deco} found ${el} problems, ${wl} warnings`
  56. : undefined;
  57. const fmtSummary = enabled && typeof summary === 'string' ? chalk_1.default.bold(summary) : summary;
  58. const help = hasProblems && options.helpUrl
  59. ? `ⓘ Get help: ${options.helpUrl}`
  60. : undefined;
  61. return [
  62. ...problems,
  63. hasProblems ? '' : undefined,
  64. fmtSummary,
  65. help,
  66. hasProblems ? '' : undefined,
  67. ].filter((line) => typeof line === 'string');
  68. }
  69. exports.formatResult = formatResult;
  70. exports.default = format;
  71. function selectSign(result) {
  72. if ((result.errors || []).length > 0) {
  73. return '✖';
  74. }
  75. return (result.warnings || []).length ? '⚠' : '✔';
  76. }
  77. function selectColor(result) {
  78. if ((result.errors || []).length > 0) {
  79. return 'red';
  80. }
  81. return (result.warnings || []).length ? 'yellow' : 'green';
  82. }
  83. //# sourceMappingURL=format.js.map