-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
31 lines (26 loc) · 868 Bytes
/
util.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
const FileSystem = require('fs');
const Path = require('path');
const readDir = promisify(FileSystem.readdir);
const fileStat = promisify(FileSystem.stat);
function promisify(nodeFunction) {
return function(...args) {
return new Promise((resolve, reject) => {
nodeFunction(...args, function(err, data) {
if(err) {
reject(err);
} else {
resolve(data);
}
})
});
};
}
function readDirDeep(dir) {
return readDir(dir).then(files => files.map(file => {
let path = Path.join(dir, file);
return fileStat(path).then(stat => stat.isDirectory() ? readDirDeep(path) : path);
}))
.then(result => Promise.all(result))
.then(files => Array.prototype.concat(...files));
}
module.exports = {readDirDeep};