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
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var fs = require('fs'),
defaultOptions = {
extensions: ['js', 'json', 'coffee'],
recurse: true,
maxDepth: -1, // no max depth
rename: function (name) {
return name;
},
Expand Down Expand Up @@ -34,7 +35,7 @@ function checkFileInclusion(path, filename, options) {
);
}

function requireDirectory(m, path, options) {
function _requireDirectory(m, path, options, depth) {
var retval = {};

// path is optional
Expand Down Expand Up @@ -62,9 +63,9 @@ function requireDirectory(m, path, options) {
key,
obj;

if (fs.statSync(joined).isDirectory() && options.recurse) {
if (fs.statSync(joined).isDirectory() && options.recurse && (options.maxDepth < 0 || depth < options.maxDepth)) {
// this node is a directory; recurse
files = requireDirectory(m, joined, options);
files = _requireDirectory(m, joined, options, depth + 1);
// exclude empty directories
if (Object.keys(files).length) {
retval[options.rename(filename, joined, filename)] = files;
Expand All @@ -82,5 +83,9 @@ function requireDirectory(m, path, options) {
return retval;
}

function requireDirectory(m, path, options) {
return _requireDirectory(m, path, options, 0);
}

module.exports = requireDirectory;
module.exports.defaults = defaultOptions;
1 change: 1 addition & 0 deletions test/example/level1/level2/level3/three.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "three";
1 change: 1 addition & 0 deletions test/example/level1/level2/two.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "two";
1 change: 1 addition & 0 deletions test/example/level1/one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "one";
28 changes: 28 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,34 @@
assert.equal('baz!', bar.baz);
assert.equal('gone and done it', funResult);
});

test('should recurse 0 times', function () {
//arrange
//act
var test = reqdir(module, PATH_TO_EXAMPLE, {maxDepth: 0});
//assert
assert.equal(undefined, test.level1);
});

test('should only recurse 2 times', function () {
//arrange
//act
var test = reqdir(module, PATH_TO_EXAMPLE, {maxDepth: 2});
//assert
assert.equal('one', test.level1.one);
assert.equal('two', test.level1.level2.two);
assert.equal(undefined, test.level1.level2.level3);
});

test('should still be able to disable recursion with maxDepth set', function () {
//arrange
//act
var test = reqdir(module, PATH_TO_EXAMPLE, {recurse: false, maxDepth: 2});

//assert
assert.equal('foo!', test.foo);
assert.equal(undefined, test.bar);
});
});

}());
Expand Down