Skip to content
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

Merged
merged 7 commits into from
Apr 28, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
30 changes: 21 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
const path = require('path');
const Module = require('module');

module.exports = (fromDir, moduleId) => {
const _resolveFileName = (fromDir, fromFile, moduleId) => {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

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');
}
Expand All @@ -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);
Copy link
Owner

@sindresorhus sindresorhus Apr 27, 2017

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
because it didn't fall to the cache it just worked :)

} catch (err) {
return null;
}
}

return _resolveFileName(fromDir, fromFile, moduleId);
};

module.exports = _resolveFrom;

module.exports.silent = (fromDir, moduleId) => _resolveFrom(fromDir, moduleId, true);
8 changes: 6 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

> Resolve the path of a module like [`require.resolve()`](http://nodejs.org/api/globals.html#globals_require_resolve) but from a given path

Unlike `require.resolve()` it returns `null` instead of throwing when the module can't be found.


## Install

Expand All @@ -28,6 +26,12 @@ resolveFrom('foo', './bar');

### resolveFrom(fromDir, moduleId)

Like `require()`, throws when the module can't be found.

### resolveFrom.silent(fromDir, moduleId)

Returns `null` instead of throwing when the module can't be found.

#### fromDir

Type: `string`
Expand Down
12 changes: 11 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Owner

Choose a reason for hiding this comment

The 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$/);
});