-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
135 lines (101 loc) · 3.49 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
'use strict';
const PluginError = require('plugin-error');
const Lesshint = require('lesshint').Lesshint;
const through = require('through2');
const { isError, isExcluded, isWarning, pluralize, severityCount } = require('./utils');
const lesshintPlugin = (options = {}) => {
const lesshint = new Lesshint();
const config = lesshint.getConfig(options.configPath);
lesshint.configure(config);
let warningCount = 0;
let maxWarnings;
let error;
if (options.maxWarnings) {
maxWarnings = parseInt(options.maxWarnings) || 0;
}
return through.obj(function (file, enc, cb) {
if (file.isStream()) {
return cb(new PluginError('gulp-lesshint', 'Streaming not supported'));
}
if (file.isNull() || isExcluded(config, file.path)) {
return cb(null, file);
}
try {
const contents = file.contents.toString();
const results = lesshint.checkString(contents, file.path);
warningCount += severityCount(results, isWarning);
file.lesshint = {
resultCount: results.length,
results,
success: !results.length,
};
} catch (e) {
error = e.stack.replace('null:', `${ file.path }:`);
}
this.push(file);
return cb();
}, function (cb) {
if (error) {
this.emit('error', new PluginError('gulp-lesshint', error, {
showStack: false,
}));
} else if (warningCount > maxWarnings) {
const message = `Failed with ${ warningCount } ${ pluralize('warning', warningCount) }. Maximum allowed is ${ options.maxWarnings }.`;
this.emit('error', new PluginError('gulp-lesshint', message, {
name: 'LesshintError',
}));
}
return cb();
});
};
lesshintPlugin.reporter = (reporter = 'lesshint-reporter-stylish') => {
const lesshint = new Lesshint();
reporter = lesshint.getReporter(reporter);
let results = [];
return through.obj((file, enc, cb) => {
if (file.lesshint && !file.lesshint.success) {
results = results.concat(file.lesshint.results);
}
return cb(null, file);
}, (cb) => {
reporter.report(results);
return cb();
});
};
lesshintPlugin.failOnError = () => {
let errorCount = 0;
return through.obj((file, enc, cb) => {
if (file.lesshint) {
errorCount += severityCount(file.lesshint.results, isError);
}
return cb(null, file);
}, function (cb) {
if (!errorCount) {
return cb();
}
const message = `Failed with ${ errorCount } ${ pluralize('error', errorCount) }`;
this.emit('error', new PluginError('gulp-lesshint', message, {
name: 'LesshintError',
}));
return cb();
});
};
lesshintPlugin.failOnWarning = () => {
let warningCount = 0;
return through.obj((file, enc, cb) => {
if (file.lesshint) {
warningCount += severityCount(file.lesshint.results, isWarning);
}
return cb(null, file);
}, function (cb) {
if (!warningCount) {
return cb();
}
const message = `Failed with ${ warningCount } ${ pluralize('warning', warningCount) }`;
this.emit('error', new PluginError('gulp-lesshint', message, {
name: 'LesshintError',
}));
return cb();
});
};
module.exports = lesshintPlugin;