Skip to content

Instantly share code, notes, and snippets.

@danrpts
Last active August 29, 2015 14:16
Show Gist options
  • Save danrpts/6be2b39de87027b8b63e to your computer and use it in GitHub Desktop.
Save danrpts/6be2b39de87027b8b63e to your computer and use it in GitHub Desktop.
An asynchronous directory walk for targeting files.
var fs = require("fs");
var path = require("path");
function asyncForEach (array, fun) {
array.forEach(function (element) {
process.nextTick(function () {
fun(element);
});
});
}
function followAsync (start, fun) {
(function async (trail) {
fs.readdir(trail, function (err, found) {
!err && asyncForEach(found, function (breadcrumb) {
var next = path.join(trail, breadcrumb);
fs.stat(next, function(err, stats) {
!err && stats.isDirectory() ?
async(next) : fun(breadcrumb, trail);
});
});
});
})(path.normalize(start));
}
follow(".", function (filename, trailname) {
console.log("%s/%s", trailname, filename);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment