Browse files

Add Readme and initial implementation

  • Loading branch information...
1 parent 198b90e commit db1c1c55b45c49adfb61c8081aebd795ad1eebc6 @felixge committed Dec 14, 2011
Showing with 33 additions and 0 deletions.
  1. +16 −0 Readme.md
  2. +17 −0 index.js
View
16 Readme.md
@@ -0,0 +1,16 @@
+# require-all
+
+An easy way to require all files within a directory.
+
+## Usage
+
+```js
+var controllers = require('require-all')({
+ directory: __dirname + '/controllers',
+ filter: /(.+Controller)\.js$/,
+});
+
+// controllers now is an object with references to all modules matching the filter
+// for example:
+// { HomeController: function HomeController() {...}, ...}
+```
View
17 index.js
@@ -0,0 +1,17 @@
+var fs = require('fs');
+
+module.exports = function requireAll(options) {
+ var files = fs.readdirSync(options.dirname);
+ var modules = {};
+
+ files.forEach(function(file) {
+ var match = file.match(options.filter);
+ if (!match) return;
+
+ var moduleName = match[1];
+ modules[moduleName] = require(options.dirname + '/' + moduleName);
+ });
+
+ return modules;
+};
+

0 comments on commit db1c1c5

Please sign in to comment.