Skip to content

Instantly share code, notes, and snippets.

@danrpts
Last active August 29, 2015 14:15
Show Gist options
  • Save danrpts/8b900084d27c6c06b60b to your computer and use it in GitHub Desktop.
Save danrpts/8b900084d27c6c06b60b to your computer and use it in GitHub Desktop.
A synchronous directory walk for targeting files.
var fs = require('fs');
var path = require('path');
function followSync (start, fun) {
var found = [];
var isfun = (typeof fun === "function");
(function sync (trail) {
fs.readdirSync(trail).forEach(function (breadcrumb) {
var next = path.join(trail, breadcrumb);
fs.statSync(next).isDirectory() ?
sync(next) : found.push(next) && isfun && fun(breadcrumb, trail);
});
})(path.normalize(start));
return found;
}
followSync(".", function (filename, trailname) {
console.log("%s/%s", trailname, filename);
});
// or...
// var found = followSync(".");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment