Skip to content

Instantly share code, notes, and snippets.

@bugventure
Created October 20, 2014 21:28
Show Gist options
  • Save bugventure/70e593a011f4f37caf1d to your computer and use it in GitHub Desktop.
Save bugventure/70e593a011f4f37caf1d to your computer and use it in GitHub Desktop.
node.js requiredir()
function requiredir(dir) {
var fullpath = path.resolve(__dirname, dir),
ret = {},
filepath,
stat,
key;
fs.readdirSync(fullpath).forEach(function forEachFile(file) {
filepath = path.join(fullpath, file);
stat = fs.statSync(filepath);
key = file.split('.').shift();
if (stat.isFile()) {
ret[key] = require(filepath);
}
else if (stat.isDirectory()) {
ret[key] = requiredir(filepath);
}
});
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment