Last active
August 29, 2015 14:15
-
-
Save danrpts/8b900084d27c6c06b60b to your computer and use it in GitHub Desktop.
A synchronous 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 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