-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (109 loc) · 3.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*jshint node:true */
'use strict';
var gutil = require('gulp-util');
var c = gutil.colors;
var es = require('event-stream');
var fs = require('fs');
var csslint = require('csslint').CSSLint;
var formatOutput = function(report, file, options) {
if (!report.messages.length) {
return {
success: true
};
}
var filePath = (file.path || 'stdin');
// Handle errors
var results = report.messages.map(function(err) {
if (!err) return;
return { file: filePath, error: err };
}).filter(function(err) {
return err;
});
var output = {
errorCount: results.length,
success: false,
results: results,
options: options
};
return output;
};
var cssLintPlugin = function(options) {
if (!options) options = {};
var ruleset = {};
// Read CSSLint options from a specified csslintrc file.
if (typeof options === 'string') {
// Don't catch readFile errors, let them bubble up
var externalOptions = fs.readFileSync('./'+options);
try {
options = JSON.parse(externalOptions);
}
catch(err) {
throw new Error('Error parsing csslintrc: '+err);
}
}
// Build a list of all available rules
csslint.getRules().forEach(function(rule) {
ruleset[rule.id] = 1;
});
for (var rule in options) {
if (!options[rule]) {
// Remove rules that are turned off
delete ruleset[rule];
}
else {
ruleset[rule] = options[rule];
}
}
return es.map(function(file, cb) {
var report = csslint.verify(String(file.contents), ruleset);
// send status down-stream
file.csslint = formatOutput(report, file, options);
cb(null, file);
});
};
var defaultReporter = function(file) {
var errorCount = file.csslint.errorCount;
var plural = errorCount === 1 ? '' : 's';
gutil.log(c.cyan(errorCount)+' error'+plural+' found in '+c.magenta(file.path));
file.csslint.results.forEach(function(result) {
var message = result.error;
gutil.log(
c.red('[') +
(
typeof message.line !== 'undefined' ?
c.yellow( 'L' + message.line ) +
c.red(':') +
c.yellow( 'C' + message.col )
:
c.yellow('GENERAL')
) +
c.red('] ') +
message.message + ' ' + message.rule.desc + ' (' + message.rule.id + ')');
});
};
cssLintPlugin.reporter = function(customReporter) {
var reporter = defaultReporter;
if (typeof customReporter === 'function') {
reporter = customReporter;
}
if (typeof reporter === 'undefined') {
throw new Error('Invalid reporter');
}
return es.map(function(file, cb) {
// Only report if CSSLint was ran and errors were found
if (file.csslint && !file.csslint.success) {
reporter(file);
}
return cb(null, file);
});
};
cssLintPlugin.failReporter = function(){
return es.map(function (file, cb) {
// Nothing to report or no errors
if (!file.csslint || file.csslint.success) {
return cb(null, file);
}
return cb(new gutil.PluginError('gulp-csslint', 'CSSLint failed for '+file.relative), file);
});
};
module.exports = cssLintPlugin;