Last active
October 1, 2018 21:40
-
-
Save cliff76/4ed3da58217dc083a0e034ca8306e9bb to your computer and use it in GitHub Desktop.
Recursive Directory walk w/ promises
This file contains hidden or 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
| const fs = require('fs'); | |
| const util = require('util'); | |
| const readdir = util.promisify(fs.readdir); | |
| const stat = util.promisify(fs.stat); | |
| const path = require('path'); | |
| const ls = (curDir, count) => readdir(curDir) | |
| .then((entries) => { | |
| return Promise.resolve(entries.map(each => path.resolve(curDir,each))); | |
| }) | |
| .then(resolvedEntries => { | |
| return Promise.all(resolvedEntries.map(each => { | |
| return stat(each).then(detail => { | |
| return Promise.resolve({name:each, isDirectory:detail.isDirectory()}); | |
| }) | |
| })); | |
| }) | |
| .then(eachWithMeta => { | |
| console.log(' '.repeat(count), './' + path.basename(curDir)); | |
| eachWithMeta.filter(it => ! it.isDirectory).forEach(each => console.log(' '.repeat(count) + '+', path.basename(each.name))); | |
| eachWithMeta.filter(it => it.isDirectory).forEach(each => ls(each.name, ++count)); | |
| }); | |
| ls('./build', 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment