|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
-
- "use strict";
-
- const lodash = require("lodash");
- const fs = require("fs");
- const path = require("path");
-
-
-
-
-
- const pageTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-page.html"), "utf-8"));
- const messageTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-message.html"), "utf-8"));
- const resultTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-result.html"), "utf-8"));
-
-
- function pluralize(word, count) {
- return (count === 1 ? word : `${word}s`);
- }
-
-
- function renderSummary(totalErrors, totalWarnings) {
- const totalProblems = totalErrors + totalWarnings;
- let renderedText = `${totalProblems} ${pluralize("problem", totalProblems)}`;
-
- if (totalProblems !== 0) {
- renderedText += ` (${totalErrors} ${pluralize("error", totalErrors)}, ${totalWarnings} ${pluralize("warning", totalWarnings)})`;
- }
- return renderedText;
- }
-
-
- function renderColor(totalErrors, totalWarnings) {
- if (totalErrors !== 0) {
- return 2;
- }
- if (totalWarnings !== 0) {
- return 1;
- }
- return 0;
- }
-
-
- function renderMessages(messages, parentIndex) {
-
-
-
- return lodash.map(messages, message => {
- const lineNumber = message.line || 0;
- const columnNumber = message.column || 0;
-
- return messageTemplate({
- parentIndex,
- lineNumber,
- columnNumber,
- severityNumber: message.severity,
- severityName: message.severity === 1 ? "Warning" : "Error",
- message: message.message,
- ruleId: message.ruleId
- });
- }).join("\n");
- }
-
-
- function renderResults(results) {
- return lodash.map(results, (result, index) => resultTemplate({
- index,
- color: renderColor(result.errorCount, result.warningCount),
- filePath: result.filePath,
- summary: renderSummary(result.errorCount, result.warningCount)
-
- }) + renderMessages(result.messages, index)).join("\n");
- }
-
-
-
-
-
- module.exports = function(results) {
- let totalErrors,
- totalWarnings;
-
- totalErrors = 0;
- totalWarnings = 0;
-
-
- results.forEach(result => {
- totalErrors += result.errorCount;
- totalWarnings += result.warningCount;
- });
-
- return pageTemplate({
- date: new Date(),
- reportColor: renderColor(totalErrors, totalWarnings),
- reportSummary: renderSummary(totalErrors, totalWarnings),
- results: renderResults(results)
- });
- };
|