UNPKG

2.78 kBJavaScriptView Raw
1/// <reference path="../typings/node.d.ts"/>
2/// <reference path="../typings/vinyl.d.ts"/>
3var Stylish;
4(function (Stylish) {
5 var chalk = require("chalk");
6 var table = require("text-table");
7 var logSymbols = require("log-symbols");
8 var _ = require("lodash");
9 var path = require("path");
10 var Reporter = (function () {
11 function Reporter(linterOutputArray, file, options) {
12 this.parseOptions(options);
13 this.parseFilename(file);
14 this.linterOutputArray = linterOutputArray;
15 this.count = " " + chalk.red(logSymbols.error) + " " +
16 this.linterOutputArray.length + " error" +
17 (this.linterOutputArray.length > 1 ? "s" : "");
18 }
19 Reporter.prototype.getFailures = function () {
20 var failures = [];
21 if (this.options.sort) {
22 this.linterOutputArray = _.sortBy(this.linterOutputArray, function (n) {
23 return n.startPosition.line;
24 });
25 }
26 this.linterOutputArray.forEach(function (error) {
27 failures.push([
28 " ",
29 chalk.gray("line " + (error.startPosition.line + 1)),
30 chalk.gray("col " + (error.startPosition.character + 1)),
31 chalk.red(error.failure)
32 ]);
33 });
34 return table(failures, { align: ["l", "l", "l", "l"] });
35 };
36 Reporter.prototype.publish = function () {
37 process.stdout.write("\n" + chalk.underline(this.fileName) + "\n");
38 process.stdout.write(this.getFailures());
39 process.stdout.write("\n\n" + this.count + "\n\n");
40 if (this.options.bell) {
41 process.stdout.write("\x07");
42 }
43 };
44 Reporter.prototype.parseOptions = function (options) {
45 this.options = options || {};
46 if (this.options.sort !== false) {
47 this.options.sort = true;
48 }
49 if (this.options.bell !== false) {
50 this.options.bell = true;
51 }
52 if (this.options.fullPath !== false) {
53 this.options.fullPath = true;
54 }
55 };
56 Reporter.prototype.parseFilename = function (file) {
57 this.fileName = file.path || file;
58 if (!this.options.fullPath) {
59 this.fileName = path.basename(this.fileName);
60 }
61 };
62 return Reporter;
63 })();
64 function generator(linterOut, file, options) {
65 var reporter = new Reporter(linterOut, file, options);
66 reporter.publish();
67 }
68 Stylish.generator = generator;
69})(Stylish || (Stylish = {}));
70module.exports = Stylish.generator;