forked from zuriby/Faker.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
walk_dir.js
43 lines (36 loc) · 1.35 KB
/
walk_dir.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
var fs = require('fs');
var methods = {
walk: function (dir, validation_function, cb) {
if (arguments.length === 2) {
cb = validation_function;
validation_function = null;
}
var results = [];
fs.readdir(dir, function (err, list) {
if (err) { return cb(err); }
var pending = list.length;
if (!pending) { return cb(null, results); }
list.forEach(function (file) {
file = dir + '/' + file;
fs.stat(file, function (err, stat) {
if (stat && stat.isDirectory()) {
methods.walk(file, validation_function, function (err, res) {
results = results.concat(res);
if (!--pending) { cb(null, results); }
});
} else {
if (typeof validation_function === 'function') {
if (validation_function(file)) {
results.push(file);
}
} else {
results.push(file);
}
if (!--pending) { cb(null, results); }
}
});
});
});
}
};
module.exports = methods;