Last active
March 17, 2020 12:40
-
-
Save sigmasoldi3r/d982ef5c19bd3f0b066108ee6c2e6cc3 to your computer and use it in GitHub Desktop.
Asynchronous directory tree builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const fs = require('fs'); | |
module.exports = (path, solver) => { | |
fs.readdir(path, (err, childs) => { | |
let prom = childs.map((child) => { | |
return new Promise((r, j) => { | |
fs.stat(`${path}/${child}`, (err, stats) => { | |
if (stats.isDirectory()){ | |
walk(`${path}/${child}`, (sub) => { | |
if (sub.length == 0) | |
r({path: `${path}/${child}`}); | |
else | |
r({path: `${path}/${child}`, childs: sub}); | |
}); | |
} else { | |
r({path: `${path}/${child}`}); | |
} | |
});//End stat | |
});//End promise | |
});//End map | |
Promise.all(prom).then((childs) => { | |
solver(childs); | |
}); | |
});//End readdir | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of usage
To use this simple snippet, require it in your nodejs project.