-
Notifications
You must be signed in to change notification settings - Fork 67
/
fileIgnored.js
70 lines (61 loc) · 1.88 KB
/
fileIgnored.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
var fs = require('fs');
var memoize = require('lodash/memoize');
var Minimatch = require('minimatch').Minimatch;
var resolve = require('path').resolve;
var dirname = require('path').dirname;
var join = require('path').join;
var sep = require('path').sep;
var RcLoader = require('rcloader');
var PluginError = require('plugin-error');
var ignoreLoader = new RcLoader('.jshintignore', {}, {
loader: function (path) {
// .jshintignore is a line-delimited list of patterns
// convert to an array and filter empty lines
var contents = fs.readFileSync(path, 'utf8');
var dir = dirname(path);
return {
file: path,
patterns:
contents.toString('utf8')
.split(/\r?\n/)
.filter(function (line) { return !!line.trim(); })
.map(function (line) {
return resolve(dir, line.trim());
})
};
}
});
// get the .jshintignore closest to the current working directory,
// but since RcLoader expect to start searching at a file we need to
// fake a file name
var ignore = ignoreLoader.for(join(process.cwd(), 'index.js'));
module.exports = (function () {
if (!ignore.file || !ignore.patterns.length) {
return function (file, cb) {
cb(null, false);
};
}
var minimatch = memoize(function (pattern) {
return new Minimatch(pattern, { nocase: true });
});
return function check(file, cb) {
if (file.isNull()) return cb(null, true);
if (file.isStream()) return cb(new PluginError('gulp-jshint', 'Streaming not supported'));
var path = file.path;
cb(null, ignore.patterns.some(function (pattern) {
if (minimatch(pattern).match(path)) {
return true;
}
if (pattern === path) {
return true;
}
var prefix = pattern;
if (prefix.substr(-1) !== sep) {
pattern += sep;
}
if (path.indexOf(prefix) === 0) {
return true;
}
}));
};
}());