Created
October 20, 2014 21:28
-
-
Save bugventure/70e593a011f4f37caf1d to your computer and use it in GitHub Desktop.
node.js requiredir()
This file contains 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
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