-
Notifications
You must be signed in to change notification settings - Fork 58
Added check to see if folder is a valid node module #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accidental whitespace change I'm guessing. |
||
| 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; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: let's |
||
| } | ||
| } | ||
|
|
||
| function toCamelCase(str) { | ||
| return str.replace(/[_-][a-z]/ig, function (s) { | ||
| return s.substring(1).toUpperCase(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| var assert = require('assert'); | ||
| var requireDir = require('..'); | ||
|
|
||
| assert.deepEqual(requireDir('./directoryAsModule'), { | ||
| a: 'a', b: 'b' | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: let's keep to the code style of having these object keys be on their own lines. |
||
| }); | ||
|
|
||
| console.log('Automatic Index tests passed') | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: let's have this |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module.exports = 'a'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module.exports = 'b' | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done :) |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Not a module |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Assuming for now we keep your design that this should be the default behavior) Should this happen only if
opts.recurseisn't specified?Put another way: what should happen when
opts.recurseis specified? Currently, we'drequirethe dir normally, and then that would get overwritten on recurse, is that right? So harmless, but inefficient?Can we add a test case for what should happen when
opts.recurseis specified and a (sub)directory is a module? Thanks!