-
-
Save ThisIsMissEm/3047711 to your computer and use it in GitHub Desktop.
Node.js require directory
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 requireDir(dir){ | |
var modules = {}; | |
fs.readdirSync(dir).map(function(name){ | |
return Path.join(dir, name); | |
}).filter(function(path){ | |
return fs.statSync(path).isFile(); | |
}).filter(function(path){ | |
return !/\.js$/.test(path); | |
}).forEach(function(path){ | |
modules[Path.basename(path, "js")] = require(path); | |
}); | |
return modules; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment