-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetFiles.js
80 lines (66 loc) · 1.76 KB
/
getFiles.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
'use strict';
var path = require('path');
var glob = require('glob');
function Dict () {
this.dict = {};
this.size = 0;
}
Dict.prototype = {
add: function (pattern) {
var files = toFiles(pattern);
files.forEach(function (name) {
if (!this.dict.hasOwnProperty(name)) {
this.dict[name] = 1;
++this.size;
}
}, this);
},
remove: function (pattern) {
if (this.size) {
var files = toFiles(pattern);
for (var i = 0; i < files.length; ++i) {
var name = files[i];
if (this.dict.hasOwnProperty(name)) {
delete this.dict[name];
if (!--this.size) {
return;
}
}
}
}
},
filter: function (files) {
return this.size ? files.filter(function (name) { return this.dict.hasOwnProperty(name); }, this) : [];
}
};
function toFiles (pattern) {
return pattern instanceof Array ? pattern : glob.sync(pattern, {follow: true});
}
function normalizePattern (pattern) {
if (pattern) {
// pattern = (pattern.charAt(0) === '/' ? '**' : '**/') + pattern;
pattern += (pattern.charAt(pattern.length - 1) === '/' ? '**' : '/**');
}
return pattern;
}
module.exports = function (bowerJson, globals) {
var dict = new Dict(), files = glob.sync(path.join(globals.getFrom(), '**/*.js'));
dict.add(files);
dict.remove(normalizePattern('node_modules'));
dict.remove(normalizePattern('bower_components'));
dict.remove(normalizePattern(globals.getDist()));
if (bowerJson && bowerJson.ignore && bowerJson.ignore instanceof Array) {
bowerJson.ignore.forEach(function (pattern) {
if (pattern) {
if (!/^\*\*\//.test(pattern) && pattern.charAt(0) !== '/') {
pattern = '**/' + pattern;
}
dict.remove(pattern);
if (!/\/\*\*$/.test(pattern)) {
dict.remove(pattern + '/**');
}
}
});
}
return dict.filter(files);
};