Skip to content

Commit

Permalink
Add Readme and initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
felixge committed Dec 14, 2011
1 parent 198b90e commit db1c1c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
16 changes: 16 additions & 0 deletions 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() {...}, ...}
```
17 changes: 17 additions & 0 deletions 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.