59e01d0b3ef5632f9e20fa22cdc8bd114ec54d75
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Stylish reporter
3  * @author Sindre Sorhus
4  */
5 "use strict";
6
7 var chalk = require("chalk"),
8     table = require("text-table");
9
10 //------------------------------------------------------------------------------
11 // Helpers
12 //------------------------------------------------------------------------------
13
14 /**
15  * Given a word and a count, append an s if count is not one.
16  * @param {string} word A word in its singular form.
17  * @param {int} count A number controlling whether word should be pluralized.
18  * @returns {string} The original word with an s on the end if count is not one.
19  */
20 function pluralize(word, count) {
21     return (count === 1 ? word : word + "s");
22 }
23
24 //------------------------------------------------------------------------------
25 // Public Interface
26 //------------------------------------------------------------------------------
27
28 module.exports = function(results) {
29
30     var output = "\n",
31         total = 0,
32         errors = 0,
33         warnings = 0,
34         summaryColor = "yellow";
35
36     results.forEach(function(result) {
37         var messages = result.messages;
38
39         if (messages.length === 0) {
40             return;
41         }
42
43         total += messages.length;
44         output += chalk.underline(result.filePath) + "\n";
45
46         output += table(
47             messages.map(function(message) {
48                 var messageType;
49
50                 if (message.fatal || message.severity === 2) {
51                     messageType = chalk.red("error");
52                     summaryColor = "red";
53                     errors++;
54                 } else {
55                     messageType = chalk.yellow("warning");
56                     warnings++;
57                 }
58
59                 return [
60                     "",
61                     message.line || 0,
62                     message.column || 0,
63                     messageType,
64                     message.message.replace(/\.$/, ""),
65                     chalk.gray(message.ruleId || "")
66                 ];
67             }),
68             {
69                 align: ["", "r", "l"],
70                 stringLength: function(str) {
71                     return chalk.stripColor(str).length;
72                 }
73             }
74         ).split("\n").map(function(el) {
75             return el.replace(/(\d+)\s+(\d+)/, function(m, p1, p2) {
76                 return chalk.gray(p1 + ":" + p2);
77             });
78         }).join("\n") + "\n\n";
79     });
80
81     if (total > 0) {
82         output += chalk[summaryColor].bold([
83             "\u2716 ", total, pluralize(" problem", total),
84             " (", errors, pluralize(" error", errors), ", ",
85             warnings, pluralize(" warning", warnings), ")\n"
86         ].join(""));
87     }
88
89     return total > 0 ? output : "";
90 };