-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
Throw on the main export and be silent when using resolveFrom.silent() #4
Changes from 3 commits
1ecbdcc
20d3d2e
d34b915
6a5f120
c31357f
ae264bb
5823a7d
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 |
---|---|---|
|
@@ -2,7 +2,15 @@ | |
const path = require('path'); | ||
const Module = require('module'); | ||
|
||
module.exports = (fromDir, moduleId) => { | ||
const _resolveFileName = (fromDir, fromFile, moduleId) => { | ||
return Module._resolveFilename(moduleId, { | ||
id: fromFile, | ||
filename: fromFile, | ||
paths: Module._nodeModulePaths(fromDir) | ||
}); | ||
}; | ||
|
||
const _resolveFrom = (fromDir, moduleId, silent) => { | ||
if (typeof fromDir !== 'string' || typeof moduleId !== 'string') { | ||
throw new TypeError('Expected `fromDir` and `moduleId` to be strings'); | ||
} | ||
|
@@ -11,13 +19,17 @@ module.exports = (fromDir, moduleId) => { | |
|
||
const fromFile = path.join(fromDir, 'noop.js'); | ||
|
||
try { | ||
return Module._resolveFilename(moduleId, { | ||
id: fromFile, | ||
filename: fromFile, | ||
paths: Module._nodeModulePaths(fromDir) | ||
}); | ||
} catch (err) { | ||
return null; | ||
if (silent) { | ||
try { | ||
_resolveFileName(fromDir, fromFile, moduleId); | ||
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. This needs to return. Probably needs a test to confirm this is working. 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. Funny, the tests were covering it and it worked, there was another return statement at the end of the function, for the "non silent" case. |
||
} catch (err) { | ||
return null; | ||
} | ||
} | ||
|
||
return _resolveFileName(fromDir, fromFile, moduleId); | ||
}; | ||
|
||
module.exports = _resolveFrom; | ||
|
||
module.exports.silent = (fromDir, moduleId) => _resolveFrom(fromDir, moduleId, true); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,15 @@ import m from './'; | |
|
||
test(t => { | ||
t.regex(m('fixture', './fixture'), /fixture\/fixture\.js$/); | ||
t.is(m('fixture', './fixture2'), null); | ||
const moduleNotFoundError = t.throws(() => { | ||
m('fixture', './nonexistent'); | ||
}, Error); | ||
t.is(moduleNotFoundError.code, 'MODULE_NOT_FOUND'); | ||
t.is(moduleNotFoundError.message, 'Cannot find module \'./nonexistent\''); | ||
t.regex(m.silent('fixture', './fixture'), /fixture\/fixture\.js$/); | ||
t.is(m.silent('fixture', './nonexistent'), null); | ||
const resolveFromfixture = m.bind(null, 'fixture'); | ||
t.regex(resolveFromfixture('./fixture'), /fixture\/fixture\.js$/); | ||
const silentResolveFromfixture = m.silent.bind(null, 'fixture'); | ||
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. The silent assertions should be in a separate test. |
||
t.regex(silentResolveFromfixture('./fixture'), /fixture\/fixture\.js$/); | ||
}); |
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.
Wondering why you prefixed this with underscores? Might have missed something in the conversation though.
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.
You're right, I removed the underscores.