Skip to content

Instantly share code, notes, and snippets.

@kkemple
Created February 13, 2014 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkemple/8983625 to your computer and use it in GitHub Desktop.
Save kkemple/8983625 to your computer and use it in GitHub Desktop.
Function for walking over files in node are requiring them. Great for loading groups of objects such as routes, models, etc.
var walk = function(path) {
fs.readdirSync(path).forEach(function(file) {
var newPath = path + '/' + file;
var stat = fs.statSync(newPath);
if (stat.isFile()) {
if (/(.*)\.(js$|coffee$)/.test(file)) {
require(newPath);
}
} else if (stat.isDirectory()) {
walk(newPath);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment