-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
84 lines (63 loc) · 1.77 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
var through = require("through2"),
gutil = require("gulp-util"),
fixmyjs = require("fixmyjs"),
RcLoader = require('rcloader'),
jshint = require('jshint').JSHINT;
module.exports = function (options) {
"use strict";
options = options || {};
if (options.lookup === void 0) {
options.lookup = true;
}
if (options.legacy === void 0) {
options.legacy = false;
}
if (options.lookup) {
var rcLoader = new RcLoader('.jshintrc', options, {});
}
var fixJS = function (contents, config) {
return fixmyjs.fix(contents, config);
};
if (options.legacy) {
fixJS = function (contents, config) {
jshint(contents, config);
return fixmyjs(jshint.data(), contents, config).run();
};
}
function error(message) {
return new gutil.PluginError("gulp-fixmyjs", message);
}
function gulpFixmyjs(file, enc, callback) {
/*jshint validthis:true*/
// Do nothing if no contents
if (file.isNull()) {
this.push(file);
return callback();
}
if (file.isStream()) {
this.emit("error", error("Stream content is not supported"));
return callback();
}
if (file.isBuffer()) {
var fix = function (err, config) {
if (err) {
this.emit("error", error("Error when reading .jshint config:\n" + err));
} else {
try {
file.contents = new Buffer(fixJS(String(file.contents), config));
this.push(file);
} catch (e) {
this.emit("error", error("Error when running fixmyjs:\n" + e.stack));
}
}
return callback();
}.bind(this);
if (options.lookup) {
rcLoader.for(file.path, fix);
} else {
fix(null, options);
}
}
}
return through.obj(gulpFixmyjs);
};