Last active
August 29, 2015 14:16
-
-
Save danrpts/6be2b39de87027b8b63e to your computer and use it in GitHub Desktop.
An asynchronous directory walk for targeting files.
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
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