Permalink
Browse files

requireAll() to load all modules in a directory into a hash.

  • Loading branch information...
cskr committed Jun 30, 2011
1 parent e58e608 commit e07fb281ef0a2748dd502eff59330b4b2302081b
Showing with 14 additions and 1 deletion.
  1. +14 −1 grasshopper/lib/grasshopper.js
@@ -14,9 +14,11 @@
* limitations under the License.
*/
+var fs = require('fs');
+
var configurables = [];
-require('fs').readdirSync(__dirname).forEach(function(file) {
+fs.readdirSync(__dirname).forEach(function(file) {
if(file == 'grasshopper.js')
return;
@@ -39,6 +41,17 @@ exports.configure = function(config) {
});
};
+exports.requireAll = function(dirName) {
+ var mods = {};
+ fs.readdirSync(dirName).forEach(function(file) {
+ if(file.substring(file.length - 3) == '.js') {
+ var modName = file.substring(0, file.length - 3);
+ mods[modName] = require(dirName + '/' + modName);
+ }
+ });
+ return mods;
+};
+
// Safety net
process.on('uncaughtException', function(err) {
console.log(err.stack);

0 comments on commit e07fb28

Please sign in to comment.