optional-require
Advanced tools
Comparing version
44
index.js
"use strict"; | ||
function optionalModule(callerRequire, resolve, path, message) { | ||
const assert = require("assert"); | ||
function _optionalRequire(callerRequire, resolve, path, message) { | ||
let opts; | ||
if (typeof message === "object") { | ||
opts = message; | ||
assert(!(opts.hasOwnProperty("notFound") && opts.hasOwnProperty("default")), | ||
"optionalRequire: options set with both `notFound` and `default`"); | ||
} else { | ||
opts = {message}; | ||
} | ||
try { | ||
return resolve ? callerRequire.resolve(path) : callerRequire(path); | ||
} catch (e) { | ||
if (e.code !== "MODULE_NOT_FOUND" || e.message.indexOf(path) < 0) { | ||
if (typeof opts.fail === "function") { | ||
return opts.fail(e); | ||
} | ||
throw e; | ||
} | ||
if (message) { | ||
message = typeof message === "string" ? `${message} - ` : ""; | ||
console.log(`Just FYI: ${message}optional module not found; Path "${path}"`); | ||
if (opts.message) { | ||
const message = typeof opts.message === "string" ? `${opts.message} - ` : ""; | ||
const r = resolve ? "resolved" : "found"; | ||
optionalRequire.log(`${message}optional module not ${r}`, path); | ||
} | ||
if (typeof opts.notFound === "function") { | ||
return opts.notFound(e); | ||
} | ||
return opts.default; | ||
} | ||
} | ||
const tryRequire = (callerRequire, path, message) => _optionalRequire(callerRequire, false, path, message); | ||
const tryResolve = (callerRequire, path, message) => _optionalRequire(callerRequire, true, path, message); | ||
function optionalRequire(callerRequire) { | ||
const x = (path, message) => optionalModule(callerRequire, false, path, message); | ||
x.resolve = (path, message) => optionalModule(callerRequire, true, path, message); | ||
const x = (path, message) => tryRequire(callerRequire, path, message); | ||
x.resolve = (path, message) => tryResolve(callerRequire, path, message); | ||
return x; | ||
} | ||
optionalRequire.try = tryRequire; | ||
optionalRequire.tryResolve = tryResolve; | ||
optionalRequire.log = (message, path) => console.log(`Just FYI: ${message}; Path "${path}"`); | ||
module.exports = optionalRequire; | ||
optionalRequire.optionalModule = optionalModule; | ||
{ | ||
"name": "optional-require", | ||
"version": "0.1.2", | ||
"description": "Optional Require", | ||
"version": "0.1.3", | ||
"description": "NodeJS Require that let you handle module not found error without try/catch", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
# Optional Require | ||
Allows you to require a module only if it exists. | ||
NodeJS Require that let you handle module not found error without try/catch. Allows you to gracefully require a module only if it exists and contains no error. | ||
@@ -25,9 +25,9 @@ # Usage | ||
#### [optionalRequire(require)]() | ||
#### [optionalRequire(require)](#optionalrequirerequire) | ||
The single function this module exports. Call it with `require` to get a custom function for your file to do optional require. See [Usage](#usage) above. | ||
The single function this module exports. Call it with `require` to get a custom function for you to do optional require from your file's require context. See [Usage](#usage) above. | ||
#### [customOptionalRequire(path, [message])]() | ||
#### [customOptionalRequire(path, [message|options])](#customoptionalrequirepath-messageoptions) | ||
The function [optionalRequire]() returns for your file to do optional require in your file. | ||
The function [optionalRequire](#optionalrequirerequire) returns for you to do optional require from your file's require context. | ||
@@ -38,6 +38,16 @@ ##### Params | ||
- `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 `undefined` if not found | ||
- 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 | ||
@@ -47,6 +57,18 @@ | ||
#### [customOptionalRequire.resolve(path, [message])]() | ||
#### [customOptionalRequire.resolve(path, [message])](#customoptionalrequireresolvepath-message) | ||
Same as [customOptionalRequire]() but acts like `require.resolve` | ||
Same as [customOptionalRequire](#customoptionalrequirepath-messageoptions) but acts like `require.resolve` | ||
#### [optionalRequire.log(message, path)](#optionalrequirelogmessage-path) | ||
The function that will be called to log the message when optional module is not found. You can override this with your own function. | ||
#### [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.tryResolve(require, path, [message|options])](#optionalrequiretryresolverequire-path-messageoptions) | ||
Same as [customOptionalRequire.resolve](#customoptionalrequirepath-messageoptions) but you have to pass in `require` from your file's context. | ||
# LICENSE | ||
@@ -53,0 +75,0 @@ |
5290
89%42
100%75
41.51%