Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Readme and initial implementation
- Loading branch information
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() {...}, ...} | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; | ||
|