-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalk.js
37 lines (35 loc) · 927 Bytes
/
walk.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
const fs = require("fs");
const path = require("path");
const walk = function (dir, done) {
let results = [];
fs.readdir(dir, function (err, list) {
if (err) return done(err);
let pending = list.length;
if (!pending) return done(null, results);
list.forEach(function (file) {
file = path.resolve(dir, file);
fs.stat(file, function (err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function (err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
results.push(file);
if (!--pending) done(null, results);
}
});
});
});
};
const walkPromisified = (dir) =>
new Promise((resolve, reject) =>
walk(dir, (err, res) => {
if (err) {
reject(err);
return;
}
resolve(res);
})
);
module.exports = walkPromisified;