forked from bmeck/dotignore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (47 loc) · 1.4 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
var minimatch = require('minimatch');
function IgnoreMatcher(str) {
var negated = this.negated = [];
var rooted = this.rooted = [];
this.matchers = (str.split(/\r?\n|\r/)).map(function (line) {
var negatedLine = line[0] === '!';
var commentLine = line[0] === '#';
var rootedLine = line[0] === '/';
if (negatedLine || commentLine || rootedLine) {
line = line.substring(1);
}
var emptyLine = line === '';
if (emptyLine) {
return null;
}
var isShellGlob = line.indexOf('/') >= 0;
negated[negated.length] = negatedLine;
rooted[rooted.length] = rootedLine || isShellGlob;
return minimatch.makeRe(line, {
comment: commentLine,
empty: emptyLine,
matchBase: !rootedLine,
negated: true //negated
});
}).filter(Boolean);
return this;
}
IgnoreMatcher.prototype.delimiter = '/';
IgnoreMatcher.prototype.shouldIgnore = function (filename) {
var isMatching = false;
for (var i = 0; i < this.matchers.length; i++) {
var matcher = this.matchers[i];
if (this.rooted[i]) {
if (matcher.test(filename)) {
isMatching = !this.negated[i];
}
} else if (filename.split(this.delimiter).some(function (part) {
return matcher.test(part);
})) {
isMatching = !this.negated[i];
}
}
return isMatching;
};
exports.createMatcher = function (ignoreFileStr) {
return new IgnoreMatcher(ignoreFileStr);
};