UNPKG

1.4 kBJavaScriptView Raw
1var fs = require('fs'), path = require('path');
2
3supportedFileRegex = /\.(js|coffee)$/
4
5module.exports = exports = function walk(dir, regExcludes, cb) {
6
7 var results = [];
8
9 fs.readdir(dir, function (err, list) {
10 if (err) return cb(err);
11
12 var pending = list.length;
13 if (!pending) return cb(null, results);
14
15 list.forEach(function (file) {
16 file = path.join(dir, file);
17
18 var included = isIncluded(file, regExcludes);
19 // Add if not in regExcludes
20 if(included) {
21 // Check if its a folder
22 fs.stat(file, function (err, stat) {
23 if (stat && stat.isDirectory()) {
24 // If it is, walk again
25 walk(file, regExcludes, function (err, res) {
26 results = results.concat(res);
27
28 if (!--pending) { cb(null, results); }
29
30 });
31 } else {
32 if (supportedFileRegex.test(path.basename(file))) results.push(file)
33 if (!--pending) { cb(null, results); }
34 }
35 });
36 } else {
37 if (!--pending) { cb(null, results); }
38 }
39 });
40 });
41};
42
43
44function isIncluded (file, regExcludes) {
45 return !((regExcludes.length > 0) && matchingRegexs(file, regExcludes));
46}
47
48
49function matchingRegexs(file, regexs) {
50 var i = 0, len = regexs.length;
51 for (; i < len; i++) {
52 if (regexs[i].test(file)) {
53 return true
54 }
55 }
56 return false;
57}