Skip to content

Commit

Permalink
Throw on the main export and be silent when using resolveFrom.silent() (
Browse files Browse the repository at this point in the history
#4)

Basically, `resolveFrom()` is now `resolveFrom.silent()`.
  • Loading branch information
ranyitz authored and sindresorhus committed Apr 28, 2017
1 parent 29e86da commit 2277c65
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
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) => {
return Module._resolveFilename(moduleId, {
id: fromFile,
filename: fromFile,
paths: Module._nodeModulePaths(fromDir)
});
};

const resolveFrom = (fromDir, moduleId, silent) => {
if (typeof fromDir !== 'string') {
throw new TypeError(`Expected \`fromDir\` to be of type \`string\`, got \`${typeof fromDir}\``);
}
Expand All @@ -15,13 +23,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 {
return resolveFileName(fromDir, fromFile, moduleId);
} catch (err) {
return null;
}
}

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

module.exports = (fromDir, moduleId) => resolveFrom(fromDir, moduleId);

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
17 changes: 15 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import test from 'ava';
import m from './';

test(t => {
test('test resolveFrom', t => {
t.throws(() => m(1, './fixture'), 'Expected `fromDir` to be of type `string`, got `number`');
t.throws(() => m('fixture'), 'Expected `moduleId` to be of type `string`, got `undefined`');
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\'');
const resolveFromfixture = m.bind(null, 'fixture');
t.regex(resolveFromfixture('./fixture'), /fixture\/fixture\.js$/);
});

test('test resolveFrom.silent', t => {
t.regex(m.silent('fixture', './fixture'), /fixture\/fixture\.js$/);
t.is(m.silent('fixture', './nonexistent'), null);
const silentResolveFromfixture = m.silent.bind(null, 'fixture');
t.regex(silentResolveFromfixture('./fixture'), /fixture\/fixture\.js$/);
});

0 comments on commit 2277c65

Please sign in to comment.