optional-require
Advanced tools
Comparing version
19
index.js
@@ -5,2 +5,8 @@ "use strict"; | ||
function findModuleNotFound(err, name) { | ||
// First line is "Cannot find module 'foo'" | ||
const msg = err.message.split("\n")[0]; | ||
return msg && msg.includes(`'${name}'`); | ||
} | ||
function _optionalRequire(callerRequire, resolve, path, message) { | ||
@@ -11,6 +17,8 @@ let opts; | ||
opts = message; | ||
assert(!(opts.hasOwnProperty("notFound") && opts.hasOwnProperty("default")), | ||
"optionalRequire: options set with both `notFound` and `default`"); | ||
assert( | ||
!(opts.hasOwnProperty("notFound") && opts.hasOwnProperty("default")), | ||
"optionalRequire: options set with both `notFound` and `default`" | ||
); | ||
} else { | ||
opts = {message}; | ||
opts = { message }; | ||
} | ||
@@ -21,4 +29,5 @@ | ||
} catch (e) { | ||
if (e.code !== "MODULE_NOT_FOUND" || e.message.indexOf(path) < 0) { | ||
if (e.code !== "MODULE_NOT_FOUND" || !findModuleNotFound(e, path)) { | ||
// if the module we are requiring fail because it try to require a | ||
// module that's not found, then we have to report this as failed. | ||
if (typeof opts.fail === "function") { | ||
@@ -25,0 +34,0 @@ return opts.fail(e); |
{ | ||
"name": "optional-require", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "NodeJS Require that let you handle module not found error without try/catch", | ||
@@ -8,3 +8,3 @@ "main": "index.js", | ||
"test": "mocha test/spec", | ||
"cover": "istanbul cover _mocha -- test/spec/*.js" | ||
"coverage": "istanbul cover _mocha -- test/spec/*.js" | ||
}, | ||
@@ -15,3 +15,10 @@ "repository": { | ||
}, | ||
"keywords": [], | ||
"keywords": [ | ||
"optional require", | ||
"optional", | ||
"require" | ||
], | ||
"files": [ | ||
"index.js" | ||
], | ||
"engines": { | ||
@@ -25,5 +32,11 @@ "node": ">=4" | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.2.0" | ||
"mocha": "^3.2.0", | ||
"require-at": "^1.0.0" | ||
}, | ||
"dependencies": {} | ||
"dependencies": { | ||
"prettier": "1.19.1" | ||
}, | ||
"prettier": { | ||
"printWidth": 120 | ||
} | ||
} |
@@ -0,1 +1,4 @@ | ||
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] | ||
[![Dependency Status][daviddm-image]][daviddm-url] [![devDependency Status][daviddm-dev-image]][daviddm-dev-url] | ||
# Optional Require | ||
@@ -29,3 +32,3 @@ | ||
#### [customOptionalRequire(path, [message|options])](#customoptionalrequirepath-messageoptions) | ||
#### [customOptionalRequire(path, \[message|options\])](#customoptionalrequirepath-messageoptions) | ||
@@ -36,23 +39,23 @@ The function [optionalRequire](#optionalrequirerequire) returns for you to do optional require from your file's require context. | ||
- `path` - name/path to the module your want to optionally require | ||
- `message` - optional flag/message to enable `console.log` a message when module is not found | ||
- `options` - an optional object with the following fields | ||
- `message` - see above | ||
- `fail` - callback for when an error that's *not* `MODULE_NOT_FOUND` for `path` occurred | ||
- `notFound` - callback for when `path` was not found | ||
- The value from this is returned | ||
- `default` - default value to returned when not found - not allowed with `notFound` together | ||
- `path` - name/path to the module your want to optionally require | ||
- `message` - optional flag/message to enable `console.log` a message when module is not found | ||
- `options` - an optional object with the following fields | ||
- `message` - see above | ||
- `fail` - callback for when an error that's _not_ `MODULE_NOT_FOUND` for `path` occurred | ||
- `notFound` - callback for when `path` was not found | ||
- The value from this is returned | ||
- `default` - default value to returned when not found - not allowed with `notFound` together | ||
##### Returns | ||
- module required or one of the following if not found | ||
- `undefined` or | ||
- return value from `options.notFound` if it's specified | ||
- `options.default` if it's specified | ||
- module required or one of the following if not found | ||
- `undefined` or | ||
- return value from `options.notFound` if it's specified | ||
- `options.default` if it's specified | ||
##### Throws | ||
- rethrows any error that's not `MODULE_NOT_FOUND` for the module `path` | ||
- rethrows any error that's not `MODULE_NOT_FOUND` for the module `path` | ||
#### [customOptionalRequire.resolve(path, [message])](#customoptionalrequireresolvepath-message) | ||
#### [customOptionalRequire.resolve(path, \[message\])](#customoptionalrequireresolvepath-message) | ||
@@ -65,7 +68,7 @@ Same as [customOptionalRequire](#customoptionalrequirepath-messageoptions) but acts like `require.resolve` | ||
#### [optionalRequire.try(require, path, [message|options])](#optionalrequiretryrequire-path-messageoptions) | ||
#### [optionalRequire.try(require, path, \[message|options\])](#optionalrequiretryrequire-path-messageoptions) | ||
Same as [customOptionalRequire](#customoptionalrequirepath-messageoptions) but you have to pass in `require` from your file's context. | ||
#### [optionalRequire.resolve(require, path, [message|options])](#optionalrequireresolverequire-path-messageoptions) | ||
#### [optionalRequire.resolve(require, path, \[message|options\])](#optionalrequireresolverequire-path-messageoptions) | ||
@@ -78,1 +81,16 @@ Same as [customOptionalRequire.resolve](#customoptionalrequirepath-messageoptions) but you have to pass in `require` from your file's context. | ||
[travis-image]: https://travis-ci.org/jchip/optional-require.svg?branch=master | ||
[travis-url]: https://travis-ci.org/jchip/optional-require | ||
[npm-image]: https://badge.fury.io/js/optional-require.svg | ||
[npm-url]: https://npmjs.org/package/optional-require | ||
[daviddm-image]: https://david-dm.org/jchip/optional-require/status.svg | ||
[daviddm-url]: https://david-dm.org/jchip/optional-require | ||
[daviddm-dev-image]: https://david-dm.org/jchip/optional-require/dev-status.svg | ||
[daviddm-dev-url]: https://david-dm.org/jchip/optional-require?type=dev |
6578
23.6%52
20.93%93
24%1
Infinity%4
33.33%3
-25%+ Added
+ Added