diff --git a/README.md b/README.md index b102855..6801a8e 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/index.js b/index.js index d923b98..9561e80 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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; + var path = filesMinusDirs[file]; if (path) { @@ -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(); diff --git a/test/directoryAsModule.js b/test/directoryAsModule.js new file mode 100644 index 0000000..afb0fc9 --- /dev/null +++ b/test/directoryAsModule.js @@ -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') diff --git a/test/directoryAsModule/a.js b/test/directoryAsModule/a.js new file mode 100644 index 0000000..2e7700b --- /dev/null +++ b/test/directoryAsModule/a.js @@ -0,0 +1 @@ +module.exports = 'a'; diff --git a/test/directoryAsModule/b/anotherfile.js b/test/directoryAsModule/b/anotherfile.js new file mode 100644 index 0000000..3df3888 --- /dev/null +++ b/test/directoryAsModule/b/anotherfile.js @@ -0,0 +1 @@ +module.exports = 'b'; diff --git a/test/directoryAsModule/b/index.js b/test/directoryAsModule/b/index.js new file mode 100644 index 0000000..b855bec --- /dev/null +++ b/test/directoryAsModule/b/index.js @@ -0,0 +1 @@ +module.exports = 'b' diff --git a/test/directoryAsModule/c/hello.txt b/test/directoryAsModule/c/hello.txt new file mode 100644 index 0000000..cbf86dc --- /dev/null +++ b/test/directoryAsModule/c/hello.txt @@ -0,0 +1 @@ +Not a module