walk.js
4.47 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
var path = require('path');
var fs = require('fs');
var minimatch = require('minimatch');
var fileMatcher = require('./filematcher.js');
var Matchers = {};
function isFolderExcluded(resource, opts) {
if (!opts.exclude) {
return false;
}
if (Matchers.exclude) {
return Matchers.exclude.some(function(item) {
if (item.indexOf(resource) === -1) {
return false;
}
try {
return fs.statSync(item).isDirectory();
} catch (err) {
return false;
}
});
}
return false;
}
function addMatcher(type, filePath, opts) {
Matchers[type] = Matchers[type] || [];
var matchers = fileMatcher[type].adaptFilePath(filePath, opts[type]);
if (Array.isArray(matchers)) {
Matchers[type].push.apply(Matchers[type], matchers);
} else {
Matchers[type].push(matchers);
}
}
function walk(dir, done, opts) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) {
return done(err);
}
var pending = list.length;
if (pending === 0) {
return done(null, results);
}
list.forEach(function(resource) {
resource = path.join(dir, resource);
fs.stat(resource, function(err, stat) {
if (stat && stat.isDirectory()) {
var dirName = resource.split(path.sep).slice(-1)[0];
var isExcluded = isFolderExcluded(resource, opts); // prevent loading of files from an excluded folder
var isSkipped = opts.skipgroup && opts.skipgroup.indexOf(dirName) > -1;
if (isExcluded || isSkipped) {
pending = pending-1;
} else {
walk(resource, function(err, res) {
results = results.concat(res);
pending = pending-1;
if (!pending) {
done(null, results);
}
}, opts);
}
} else {
results.push(resource);
pending = pending-1;
if (!pending) {
done(null, results);
}
}
});
});
});
}
module.exports = {
readPaths : function (paths, cb, opts) {
Matchers.exclude = [];
Matchers.filter = [];
var allmodules = [];
var extensionPattern = /\.js$/;
var modulePaths = paths.slice(0);
(function readSourcePaths() {
var sourcePath = modulePaths.shift();
fs.stat(sourcePath, function(err, stat) {
if (err) {
return cb(err);
}
if (stat.isFile() && extensionPattern.test(sourcePath)) {
sourcePath = sourcePath.replace(extensionPattern, '');
if (allmodules.indexOf(sourcePath) === -1) {
allmodules.push(sourcePath);
}
if (modulePaths.length) {
readSourcePaths();
} else {
cb(null, allmodules);
}
} else if (stat.isDirectory()) {
if (opts.exclude) {
addMatcher('exclude', sourcePath, opts);
}
if (opts.filter) {
addMatcher('filter', sourcePath, opts);
}
walk(sourcePath, function (err, list) {
if (err) {
return cb(err);
}
list.sort();
var modules = list.filter(function (filePath) {
if (!extensionPattern.test(filePath)) {
return false;
}
if (opts.exclude && fileMatcher.exclude.match(filePath, Matchers.exclude)) {
return false;
}
if (opts.filter && !fileMatcher.filter.match(filePath, Matchers.filter)) {
return false;
}
var filename = filePath.split(path.sep).slice(-1)[0];
if (opts.filename_filter) {
return minimatch(filename, opts.filename_filter);
}
if (opts.tag_filter || opts.skiptags) {
return fileMatcher.tags.match(filePath, opts);
}
return true;
});
modules.forEach(function(item) {
var filename = item.replace(extensionPattern, '');
if (allmodules.indexOf(filename) === -1) {
allmodules.push(filename);
}
});
if (modulePaths.length) {
readSourcePaths();
} else {
cb(null, allmodules);
}
}, opts);
} else {
if (modulePaths.length) {
readSourcePaths();
} else {
cb(null, allmodules);
}
}
});
})();
}
};