Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

table.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @fileoverview "table reporter.
  3. * @author Gajus Kuizinas <gajus@gajus.com>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const chalk = require("chalk"),
  10. table = require("table").table,
  11. pluralize = require("pluralize");
  12. //------------------------------------------------------------------------------
  13. // Helpers
  14. //------------------------------------------------------------------------------
  15. /**
  16. * Draws text table.
  17. * @param {Array<Object>} messages Error messages relating to a specific file.
  18. * @returns {string} A text table.
  19. */
  20. function drawTable(messages) {
  21. const rows = [];
  22. if (messages.length === 0) {
  23. return "";
  24. }
  25. rows.push([
  26. chalk.bold("Line"),
  27. chalk.bold("Column"),
  28. chalk.bold("Type"),
  29. chalk.bold("Message"),
  30. chalk.bold("Rule ID")
  31. ]);
  32. messages.forEach(message => {
  33. let messageType;
  34. if (message.fatal || message.severity === 2) {
  35. messageType = chalk.red("error");
  36. } else {
  37. messageType = chalk.yellow("warning");
  38. }
  39. rows.push([
  40. message.line || 0,
  41. message.column || 0,
  42. messageType,
  43. message.message,
  44. message.ruleId || ""
  45. ]);
  46. });
  47. return table(rows, {
  48. columns: {
  49. 0: {
  50. width: 8,
  51. wrapWord: true
  52. },
  53. 1: {
  54. width: 8,
  55. wrapWord: true
  56. },
  57. 2: {
  58. width: 8,
  59. wrapWord: true
  60. },
  61. 3: {
  62. paddingRight: 5,
  63. width: 50,
  64. wrapWord: true
  65. },
  66. 4: {
  67. width: 20,
  68. wrapWord: true
  69. }
  70. },
  71. drawHorizontalLine(index) {
  72. return index === 1;
  73. }
  74. });
  75. }
  76. /**
  77. * Draws a report (multiple tables).
  78. * @param {Array} results Report results for every file.
  79. * @returns {string} A column of text tables.
  80. */
  81. function drawReport(results) {
  82. let files;
  83. files = results.map(result => {
  84. if (!result.messages.length) {
  85. return "";
  86. }
  87. return `\n${result.filePath}\n\n${drawTable(result.messages)}`;
  88. });
  89. files = files.filter(content => content.trim());
  90. return files.join("");
  91. }
  92. //------------------------------------------------------------------------------
  93. // Public Interface
  94. //------------------------------------------------------------------------------
  95. module.exports = function(report) {
  96. let result,
  97. errorCount,
  98. warningCount;
  99. result = "";
  100. errorCount = 0;
  101. warningCount = 0;
  102. report.forEach(fileReport => {
  103. errorCount += fileReport.errorCount;
  104. warningCount += fileReport.warningCount;
  105. });
  106. if (errorCount || warningCount) {
  107. result = drawReport(report);
  108. }
  109. result += `\n${table([
  110. [
  111. chalk.red(pluralize("Error", errorCount, true))
  112. ],
  113. [
  114. chalk.yellow(pluralize("Warning", warningCount, true))
  115. ]
  116. ], {
  117. columns: {
  118. 0: {
  119. width: 110,
  120. wrapWord: true
  121. }
  122. },
  123. drawHorizontalLine() {
  124. return true;
  125. }
  126. })}`;
  127. return result;
  128. };