Skip to content

Instantly share code, notes, and snippets.

@cliff76
Last active October 1, 2018 21:40
Show Gist options
  • Select an option

  • Save cliff76/4ed3da58217dc083a0e034ca8306e9bb to your computer and use it in GitHub Desktop.

Select an option

Save cliff76/4ed3da58217dc083a0e034ca8306e9bb to your computer and use it in GitHub Desktop.
Recursive Directory walk w/ promises
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