1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | const lodash = require("lodash");
|
8 | const fs = require("fs");
|
9 | const path = require("path");
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | const pageTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-page.html"), "utf-8"));
|
16 | const messageTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-message.html"), "utf-8"));
|
17 | const resultTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-result.html"), "utf-8"));
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function pluralize(word, count) {
|
26 | return (count === 1 ? word : `${word}s`);
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function renderSummary(totalErrors, totalWarnings) {
|
36 | const totalProblems = totalErrors + totalWarnings;
|
37 | let renderedText = `${totalProblems} ${pluralize("problem", totalProblems)}`;
|
38 |
|
39 | if (totalProblems !== 0) {
|
40 | renderedText += ` (${totalErrors} ${pluralize("error", totalErrors)}, ${totalWarnings} ${pluralize("warning", totalWarnings)})`;
|
41 | }
|
42 | return renderedText;
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | function renderColor(totalErrors, totalWarnings) {
|
52 | if (totalErrors !== 0) {
|
53 | return 2;
|
54 | }
|
55 | if (totalWarnings !== 0) {
|
56 | return 1;
|
57 | }
|
58 | return 0;
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | function renderMessages(messages, parentIndex) {
|
68 |
|
69 | |
70 |
|
71 |
|
72 |
|
73 |
|
74 | return lodash.map(messages, message => {
|
75 | const lineNumber = message.line || 0;
|
76 | const columnNumber = message.column || 0;
|
77 |
|
78 | return messageTemplate({
|
79 | parentIndex,
|
80 | lineNumber,
|
81 | columnNumber,
|
82 | severityNumber: message.severity,
|
83 | severityName: message.severity === 1 ? "Warning" : "Error",
|
84 | message: message.message,
|
85 | ruleId: message.ruleId
|
86 | });
|
87 | }).join("\n");
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | function renderResults(results) {
|
95 | return lodash.map(results, (result, index) => resultTemplate({
|
96 | index,
|
97 | color: renderColor(result.errorCount, result.warningCount),
|
98 | filePath: result.filePath,
|
99 | summary: renderSummary(result.errorCount, result.warningCount)
|
100 |
|
101 | }) + renderMessages(result.messages, index)).join("\n");
|
102 | }
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | module.exports = function(results) {
|
109 | let totalErrors,
|
110 | totalWarnings;
|
111 |
|
112 | totalErrors = 0;
|
113 | totalWarnings = 0;
|
114 |
|
115 |
|
116 | results.forEach(result => {
|
117 | totalErrors += result.errorCount;
|
118 | totalWarnings += result.warningCount;
|
119 | });
|
120 |
|
121 | return pageTemplate({
|
122 | date: new Date(),
|
123 | reportColor: renderColor(totalErrors, totalWarnings),
|
124 | reportSummary: renderSummary(totalErrors, totalWarnings),
|
125 | results: renderResults(results)
|
126 | });
|
127 | };
|