Skip to content

Commit

Permalink
Support a filter option to exclude particular files
Browse files Browse the repository at this point in the history
We're using require-dir to load a directory of gulp tasks. However, we want to be able to exclude certain files from being required when executed in a production environment as they include dependencies that might not be installed.

Being able to apply a filter like `function (f) { return process.env.NODE_ENV !== 'production' && !f.match(/$dev/); }` would mean we can just ignore any dev-only task from the directory when run in a prod environment.
  • Loading branch information
lennym committed Dec 23, 2015
1 parent 824b9f1 commit 0c801eb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ be the same by default, but specifying `duplicates: true` would yield:
}
```

`filter`: Apply a filter on the filename before require-ing. For example:

```js
requiredir('./dir', function (f) { return process.env.NODE_ENV !== 'production' && !f.match(/$dev/); })
```

This will ignore files prefixed with `dev` if running in a production environment.

There might be more options in the future. ;)

## Tips
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module.exports = function requireDir(dir, opts) {
dir = dir || '.';
opts = opts || {};

opts.filter = opts.filter || function (file) { return true; };

// resolve the path to an absolute one:
dir = Path.resolve(parentDir, dir);

Expand Down Expand Up @@ -66,6 +68,10 @@ module.exports = function requireDir(dir, opts) {
if (path === parentFile) {
continue;
}
// apply file filter:
if (!opts.filter(path)) {
continue;
}

if (FS.statSync(path).isDirectory()) {
if (opts.recurse) {
Expand Down

0 comments on commit 0c801eb

Please sign in to comment.