-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
103 lines (83 loc) · 2.47 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
'use strict';
var PluginError = require('plugin-error');
var through = require('through2');
var stylint = require('stylint');
var failReporter = function (options) {
options = options || {};
var failOnWarning = options.failOnWarning;
return through.obj(function (file, enc, cb) {
if (file.stylint && (file.stylint.errors || (failOnWarning && file.stylint.warnings))) {
return cb(new PluginError('gulp-stylint', 'Stylint failed for ' + file.relative), file);
}
this.push(file);
cb();
});
};
module.exports = function (options) {
options = options || {};
var reporter = options.reporter;
var rules = options.rules;
var reporterOptions;
if (reporter) {
delete options.reporter;
if (typeof reporter === 'string') {
reporter = require(reporter);
} else if (typeof reporter === 'object') {
reporterOptions = reporter.reporterOptions;
reporter = require(reporter.reporter);
}
}
return through.obj(function (file, enc, cb) {
var that = this;
if (file.isNull()) {
return cb(null, file); // pass along
}
if (file.isStream()) {
return cb(new PluginError('gulp-stylint', 'Streaming not supported'), file);
}
stylint(file.path, rules)
.methods({
read: function () {
this.cache.filesLen = 1;
this.cache.fileNo = 1;
this.cache.file = file.path;
this.cache.files = [file.path];
this.state.quiet = true;
if (reporter) {
this.reporter = reporter;
this.config.reporterOptions = reporterOptions;
}
this.parse(null, [file.contents.toString(enc)]);
},
done: function () {
var warningsOrErrors = [].concat(this.cache.errs, this.cache.warnings);
if (warningsOrErrors.length) {
var msg = warningsOrErrors.filter(Boolean).join('\n\n');
msg += '\n' + this.cache.msg;
file.stylint = {msg: msg, errors: this.cache.errs.length > 0, warnings: this.cache.warnings.length > 0};
}
// HACK: reset stylint, since it accidentally shares global state
this.resetOnChange();
that.push(file);
cb();
}
})
.create({}, options);
});
};
module.exports.reporter = function (reporter, options) {
if (typeof reporter === 'string') {
if (reporter === 'fail') {
return failReporter(options);
}
throw new PluginError('gulp-stylint', reporter + ' is not a reporter');
}
var logger = (reporter || {}).logger || console.log;
return through.obj(function (file, enc, cb) {
if (file.stylint) {
logger(file.stylint.msg);
}
this.push(file);
cb();
});
};