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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ You can optionally customize the behavior by passing an extra options object:
var dir = requireDir('./path/to/dir', {recurse: true});
```

## Directory as Modules

As per [the node documentation](https://nodejs.org/api/modules.html#modules_folders_as_modules)
directories that resolve to a module will be treated as such when recursion is **not** enabled.
For example, if you have a folder called 'foo' with 'index.js' inside, requireDir will return the
contents of index.js on its object.

## Options

`recurse`: Whether to recursively `require()` subdirectories too.
Expand Down
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ module.exports = function requireDir(dir, opts) {
if (opts.duplicates) {
map[file] = map[base];
}
} else {
// If we're not recursing through the files then we check to see
// if the folder is a valid node module

// Checks to see if there is an index file and returns the extension
var modulePath = getModulePath(path);

if (modulePath && !opts.recurse) {
filesMinusDirs[file + Path.extname(modulePath)] = modulePath;
}

}
} else {
filesMinusDirs[file] = path;
Expand All @@ -100,6 +111,7 @@ module.exports = function requireDir(dir, opts) {

// if a file exists with this extension, we'll require() it:
var file = base + ext;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accidental whitespace change I'm guessing.

var path = filesMinusDirs[file];

if (path) {
Expand Down Expand Up @@ -134,6 +146,14 @@ module.exports = function requireDir(dir, opts) {
return map;
};

function getModulePath(path) {
try {
return require.resolve(path);
} catch (err) {
return null;
}
}

function toCamelCase(str) {
return str.replace(/[_-][a-z]/ig, function (s) {
return s.substring(1).toUpperCase();
Expand Down
18 changes: 18 additions & 0 deletions test/directoryAsModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var assert = require('assert');
var requireDir = require('..');

assert.deepEqual(requireDir('./directoryAsModule'), {
a: 'a',
b: 'b'
});

assert.deepEqual(requireDir('./directoryAsModule', {recurse: true}), {
a: 'a',
b: {
"index":"b",
"anotherfile": "b"
},
c: {}
});

console.log('Directory as module tests passed')
1 change: 1 addition & 0 deletions test/directoryAsModule/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'a';
1 change: 1 addition & 0 deletions test/directoryAsModule/b/anotherfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'b';
1 change: 1 addition & 0 deletions test/directoryAsModule/b/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'b'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a second file to this directory, to explicitly test (and convey) that it isn't getting required?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

1 change: 1 addition & 0 deletions test/directoryAsModule/c/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Not a module