Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,10 +27,34 @@ folder var will contains:
}
```

## Recursion

You can include files recursive using the `recurse` parameter:


```javascript
var includeFolder = require('include-folder'),
folder = includeFolder("./aFolder",/^a.*/, { recurse: true });
```

This include files that start with 'a', in any subfolder, subfolders are created as object properties.

## Filter included files

You can filter which files to include using the filter parameter:

### Using Globs

```javascript
var includeFolder = require('include-folder'),
folder = includeFolder("./aFolder", [ '**/*.txt', '!README*' ]);
```

This includes every *.txt recursively (if a pattern references subfolders, `options.recurse` is enabled automatically) but excludes files starting with "README".

(see https://github.com/sindresorhus/multimatch for details on matching with multiple globs)

### Using a Regular Expression

```javascript
var includeFolder = require('include-folder'),
Expand DownExpand Up@@ -60,4 +84,3 @@ Add unit tests for any new or changed functionality.
Copyright (c) 2013 Andrea Parodi

Licensed under the MIT license.

1 change: 1 addition & 0 deletions example/files/subfolder/file4.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
hello
2 changes: 2 additions & 0 deletions example/files/subfolder/file5.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
world
=====
53 changes: 51 additions & 2 deletions lib/include-folder.js
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
'use strict';

var fs = require('fs');
var path = require('path');
var micromatch = require('micromatch');

function includeFolder (folderName, filter, options) {
folderName = folderName.replace(/\/$/, '');
Expand DownExpand Up@@ -41,10 +43,52 @@ includeFolder.stripExtension = function (fileName) {
};

includeFolder.buildSource = function (folderName, filter, options) {
var self = this;
var files = fs.readdirSync(folderName);
var allProps = {};
filter = filter.test.bind(filter);
var fields = files.filter(filter).map(function (fileName) {

options = options || {};
options.basePath = options.basePath || folderName;

if (filter && typeof filter !== 'function') {
if (filter.test) {
filter = filter.test.bind(filter);
} else {
var globs = typeof filter === 'string' ? [filter] : filter;
if (globs.filter(function (glob) {
return /(\/|\*\*)/.test(glob);
}).length > 0) {
// a glob that includes subfolders was used, enable recursion
options.recurse = true;
}
filter = function (name) {
return micromatch([name], globs).length > 0;
};
}
}

var fields = files.map(function (fileName) {
var fullname = path.join(folderName, fileName);
var relative = path.relative(options.basePath, fullname);

if (fs.lstatSync(fullname).isDirectory()) {
if (!options.recurse) {
return;
}
var folderSource = self.buildSource(fullname, filter, options);
if (!/readFileSync/.test(folderSource)) {
// folder is empty
return;
}
return 'self["' + fileName + '"] = (function(){' +
folderSource +
'})();';
}

if (filter && !filter(relative)) {
return;
}

var name = includeFolder.normalize(
includeFolder.stripExtension(fileName, options),
options
Expand All@@ -62,8 +106,13 @@ includeFolder.buildSource = function (folderName, filter, options) {
}

return 'self["' + name + '"] = fs.readFileSync("' + folderName + '/' + fileName + '","utf8");';
}).filter(function (source) {
// remove filtered entries
return !!source;
});

/*
*/
return 'var self={},' +
'fs = require("fs");\n' +
fields.join('\n') +
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line numberDiff line numberDiff line change
Expand Up@@ -17,7 +17,7 @@
"bugs": {
"url": "https://github.com/parro-it/include-folder/issues"
},
"license": "MIT",
"license": "MIT",
"main": "lib/include-folder",
"engines": {
"node": ">= 0.10.0"
Expand All@@ -42,5 +42,7 @@
"require",
"readFileSync"
],
"dependencies": {}
"dependencies": {
"micromatch": "^2.3.8"
}
}
1 change: 1 addition & 0 deletions test/files/subfolder/file4.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
hello
2 changes: 2 additions & 0 deletions test/files/subfolder/file5.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
world
=====
27 changes: 27 additions & 0 deletions test/include-folder_test.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -88,4 +88,31 @@ describe('include_folder', function () {
});
});

describe('when the recurse option is true', function () {
beforeEach(function () {
this.folderModule = includeFolder('./test/files', null, { recurse: true });
});

it('subfolders are included', function () {
expect('subfolder' in this.folderModule).to.be.equal(true);
expect('file4' in this.folderModule.subfolder).to.be.equal(true);
expect('file5' in this.folderModule.subfolder).to.be.equal(true);
});
});

describe('when using globs', function () {
beforeEach(function () {
this.folderModule = includeFolder('./test/files', [
'!file*',
'subfolder/*.txt'
]);
});

it('filters, and recursion is automatic', function () {
expect('file1' in this.folderModule).to.be.equal(false);
expect('subfolder' in this.folderModule).to.be.equal(true);
expect('file4' in this.folderModule.subfolder).to.be.equal(true);
expect('file5' in this.folderModule.subfolder).to.be.equal(false);
});
});
});