Skip to content

Loading…

Allow to exclude dirs from being recursed into #4

Merged
merged 1 commit into from

2 participants

@thlorenz
Collaborator
  • excludeDirs regex can be specified to exclude directories from being recursed into
  • added fixtures test/filterdir and related tests inside tests.js
  • updated readme to show this options

Example:

var controllers = require('require-all')({
  dirname     :  __dirname + '/controllers',
  filter      :  /(.+Controller)\.js$/,
  excludeDirs :  /^\.(git|svn)$/
});

This feature becomes especially important if you are stuck with a version of svn that adds '.svn' folders everywhere. In order to prevent exports['.svn'] to be added, I can now exclude it.

This would also be of use if I'd like to make my index.js auto require all modules in lib folders, etc., In this case I'd need to be able to exclude '.git' and 'node_modules'.

Thorsten Lorenz added option to exclude dirs:
- excludeDirs regex can be specified to exclude directories from being recursed into
- added fixtures test/filterdir and related tests inside tests.js
- updated readme to show this options
e076918
@felixge
Owner

Hi @thlorenz,

I just added you as a collaborator on the github repo and have given you publish access on the npm module. So feel free to merge this yourself / release a new version.

Thanks for the help!

Cheers,
Felix

@thlorenz
Collaborator

Thanks, in the middle of nko, but once I get a breather I'll merge it in.

@thlorenz thlorenz merged commit f046a4d into felixge:master
@felixge
Owner

@thlorenz :heart: thanks for the patch!

@thlorenz
Collaborator

My pleasure ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 9, 2012
  1. added option to exclude dirs:

    Thorsten Lorenz committed
    - excludeDirs regex can be specified to exclude directories from being recursed into
    - added fixtures test/filterdir and related tests inside tests.js
    - updated readme to show this options
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
5 Readme.md
@@ -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
12 index.js
@@ -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 {
View
1 test/filterdir/.svn/stuff.js
@@ -0,0 +1 @@
+module.exports = { gute: 'nacht' };
View
1 test/filterdir/root.js
@@ -0,0 +1 @@
+module.exports = { guten: 'tag' };
View
1 test/filterdir/sub/hello.js
@@ -0,0 +1 @@
+module.exports = { guten: 'abend' };
View
29 test/test.js
@@ -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);
Something went wrong with that request. Please try again.