From 7b42238495c3fa9bc12c533ce614599da30580f9 Mon Sep 17 00:00:00 2001 From: Alex Perry Date: Tue, 10 Nov 2015 17:20:36 +0000 Subject: [PATCH 1/4] Added check to see if folder is a valid node module --- index.js | 20 ++++++++++++++++++++ test/directoryAsModule.js | 8 ++++++++ test/directoryAsModule/a.js | 1 + test/directoryAsModule/b/index.js | 1 + test/directoryAsModule/c/hello.txt | 1 + 5 files changed, 31 insertions(+) create mode 100644 test/directoryAsModule.js create mode 100644 test/directoryAsModule/a.js create mode 100644 test/directoryAsModule/b/index.js create mode 100644 test/directoryAsModule/c/hello.txt diff --git a/index.js b/index.js index d923b98..1f45717 100644 --- a/index.js +++ b/index.js @@ -68,6 +68,17 @@ module.exports = function requireDir(dir, opts) { } if (FS.statSync(path).isDirectory()) { + + // 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; + } + if (opts.recurse) { if (base === 'node_modules') { continue; @@ -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 (_) { + return false; + } +} + 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..b67df29 --- /dev/null +++ b/test/directoryAsModule.js @@ -0,0 +1,8 @@ +var assert = require('assert'); +var requireDir = require('..'); + +assert.deepEqual(requireDir('./directoryAsModule'), { + a: 'a', b: 'b' +}); + +console.log('Automatic Index 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/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 From 1f73372567f1b4efe431f601f636b8fa6d58e1c9 Mon Sep 17 00:00:00 2001 From: Alex Perry Date: Thu, 12 Nov 2015 14:36:09 +0000 Subject: [PATCH 2/4] Refactored directory as a module functionality --- index.js | 24 +++++++++++++----------- test/directoryAsModule.js | 14 ++++++++++++-- test/directoryAsModule/b/anotherfile.js | 1 + 3 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 test/directoryAsModule/b/anotherfile.js diff --git a/index.js b/index.js index 1f45717..eca45d8 100644 --- a/index.js +++ b/index.js @@ -69,15 +69,6 @@ module.exports = function requireDir(dir, opts) { if (FS.statSync(path).isDirectory()) { - // 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; - } if (opts.recurse) { if (base === 'node_modules') { @@ -90,6 +81,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; @@ -149,8 +151,8 @@ module.exports = function requireDir(dir, opts) { function getModulePath(path) { try { return require.resolve(path); - } catch (_) { - return false; + } catch (err) { + return null; } } diff --git a/test/directoryAsModule.js b/test/directoryAsModule.js index b67df29..afb0fc9 100644 --- a/test/directoryAsModule.js +++ b/test/directoryAsModule.js @@ -2,7 +2,17 @@ var assert = require('assert'); var requireDir = require('..'); assert.deepEqual(requireDir('./directoryAsModule'), { - a: 'a', b: 'b' + a: 'a', + b: 'b' }); -console.log('Automatic Index tests passed') +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/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'; From dd305a5ed27fc356a8c12bb84c3442df5b013bf7 Mon Sep 17 00:00:00 2001 From: Alex Perry Date: Thu, 12 Nov 2015 14:36:26 +0000 Subject: [PATCH 3/4] Added readme about directory as modules --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) 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. From fa5f81062f06fdce0f27c2e7faac7bb0670a12cf Mon Sep 17 00:00:00 2001 From: Alex Perry Date: Thu, 12 Nov 2015 14:52:58 +0000 Subject: [PATCH 4/4] Removed white space --- index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/index.js b/index.js index eca45d8..9561e80 100644 --- a/index.js +++ b/index.js @@ -68,8 +68,6 @@ module.exports = function requireDir(dir, opts) { } if (FS.statSync(path).isDirectory()) { - - if (opts.recurse) { if (base === 'node_modules') { continue;