Permalink
Browse files

Merge pull request #4 from thlorenz/master

Allow to exclude dirs from being recursed into
  • Loading branch information...
2 parents 41e1029 + e076918 commit f046a4de0c75b3a931691189ec7a7ba6787de820 @thlorenz thlorenz committed Nov 14, 2012
Showing with 45 additions and 4 deletions.
  1. +3 −2 Readme.md
  2. +10 −2 index.js
  3. +1 −0 test/filterdir/.svn/stuff.js
  4. +1 −0 test/filterdir/root.js
  5. +1 −0 test/filterdir/sub/hello.js
  6. +29 −0 test/test.js
View
@@ -6,8 +6,9 @@ An easy way to require all files within a directory.
```js
var controllers = require('require-all')({
- dirname: __dirname + '/controllers',
- filter: /(.+Controller)\.js$/,
+ dirname : __dirname + '/controllers',
+ filter : /(.+Controller)\.js$/,
+ excludeDirs : /^\.(git|svn)$/
});
// controllers now is an object with references to all modules matching the filter
View
@@ -4,12 +4,20 @@ module.exports = function requireAll(options) {
var files = fs.readdirSync(options.dirname);
var modules = {};
+ function excludeDirectory(dirname) {
+ return options.excludeDirs && dirname.match(options.excludeDirs);
+ }
+
files.forEach(function(file) {
var filepath = options.dirname + '/' + file;
if (fs.statSync(filepath).isDirectory()) {
+
+ if (excludeDirectory(file)) return;
+
modules[file] = requireAll({
- dirname: filepath,
- filter: options.filter
+ dirname : filepath,
+ filter : options.filter,
+ excludeDirs : options.excludeDirs
});
} else {
@@ -0,0 +1 @@
+module.exports = { gute: 'nacht' };
View
@@ -0,0 +1 @@
+module.exports = { guten: 'tag' };
@@ -0,0 +1 @@
+module.exports = { guten: 'abend' };
View
@@ -36,3 +36,32 @@ if (process.version > 'v0.6.0') {
}
});
}
+
+var unfiltered = requireAll({
+ dirname: __dirname + '/filterdir',
+ filter: /(.+)\.js$/
+});
+
+assert(unfiltered['.svn']);
+assert(unfiltered['root']);
+assert(unfiltered['sub']);
+
+var excludedSvn = requireAll({
+ dirname: __dirname + '/filterdir',
+ filter: /(.+)\.js$/,
+ excludeDirs: /^\.svn$/
+});
+
+assert.equal(excludedSvn['.svn'], undefined);
+assert.ok(excludedSvn['root']);
+assert.ok(excludedSvn['sub']);
+
+var excludedSvnAndSub = requireAll({
+ dirname: __dirname + '/filterdir',
+ filter: /(.+)\.js$/,
+ excludeDirs: /^(\.svn|sub)$/
+});
+
+assert.equal(excludedSvnAndSub['.svn'], undefined);
+assert.ok(excludedSvnAndSub['root']);
+assert.equal(excludedSvnAndSub['sub'], undefined);

0 comments on commit f046a4d

Please sign in to comment.