-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.js
83 lines (65 loc) · 1.68 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
'use strict';
var path = require('path');
var PluginError = require('plugin-error');
var through = require('through2');
var tildify = require('tildify');
var Checker = require('jscs');
var loadConfigFile = require('jscs/lib/cli-config');
module.exports = function (opts) {
opts = opts || {};
var config;
var checker = new Checker();
try {
config = loadConfigFile.load(opts.configPath);
} catch (err) {
err.message = 'Unable to load JSCS config file';
if (opts.configPath) {
err.message += ' at ' + tildify(path.resolve(opts.configPath));
}
err.message += '\n' + err.message;
throw err;
}
// run autofix over as many errors as possible
if (opts.fix) {
config.maxErrors = Infinity;
}
checker.registerDefaultRules();
checker.configure(config);
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new PluginError('gulp-jscs', 'Streaming not supported'));
return;
}
if (checker.getConfiguration().isFileExcluded(file.path)) {
cb(null, file);
return;
}
var fixResults;
var errors;
var contents = file.contents.toString();
if (opts.fix) {
fixResults = checker.fixString(contents, file.path);
errors = fixResults.errors;
file.contents = new Buffer(fixResults.output);
} else {
errors = checker.checkString(contents, file.path);
}
var errorList = errors.getErrorList();
file.jscs = {
success: true,
errorCount: 0,
errors: []
};
if (errorList.length > 0) {
file.jscs.success = false;
file.jscs.errorCount = errorList.length;
file.jscs.errors = errors;
}
cb(null, file);
});
};
module.exports.reporter = require('./reporters');