Initial commit
This commit is contained in:
92
node_modules/optional-require/README.md
generated
vendored
Normal file
92
node_modules/optional-require/README.md
generated
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
[![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
|
||||
|
||||
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.
|
||||
|
||||
# Usage
|
||||
|
||||
```js
|
||||
const optionalRequire = require("optional-require")(require);
|
||||
|
||||
const foo = optionalRequire("foo") || {};
|
||||
const bar = optionalRequire("bar", true); // true enables console.log a message when not found
|
||||
const xyz = optionalRequire("xyz", "test"); // "test" enables console.log a message with "test" added.
|
||||
const fbPath = optionalRequire.resolve("foo", "foo doesn't exist");
|
||||
const rel = optionalRequire("../foo/bar"); // relative module path works
|
||||
```
|
||||
|
||||
# Install
|
||||
|
||||
```bash
|
||||
$ npm i optional-require --save
|
||||
```
|
||||
|
||||
# API
|
||||
|
||||
#### [optionalRequire(require)](#optionalrequirerequire)
|
||||
|
||||
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|options\])](#customoptionalrequirepath-messageoptions)
|
||||
|
||||
The function [optionalRequire](#optionalrequirerequire) returns for you to do optional require from your file's require context.
|
||||
|
||||
##### Params
|
||||
|
||||
- `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
|
||||
|
||||
##### Throws
|
||||
|
||||
- rethrows any error that's not `MODULE_NOT_FOUND` for the module `path`
|
||||
|
||||
#### [customOptionalRequire.resolve(path, \[message\])](#customoptionalrequireresolvepath-message)
|
||||
|
||||
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.resolve(require, path, \[message|options\])](#optionalrequireresolverequire-path-messageoptions)
|
||||
|
||||
Same as [customOptionalRequire.resolve](#customoptionalrequirepath-messageoptions) but you have to pass in `require` from your file's context.
|
||||
|
||||
# LICENSE
|
||||
|
||||
Apache-2.0 © [Joel Chen](https://github.com/jchip)
|
||||
|
||||
[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
|
||||
75
node_modules/optional-require/index.js
generated
vendored
Normal file
75
node_modules/optional-require/index.js
generated
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
"use strict";
|
||||
|
||||
const assert = require("assert");
|
||||
|
||||
function findModuleNotFound(err, name) {
|
||||
// Check the first line of the error message
|
||||
const msg = err.message.split("\n")[0];
|
||||
return msg && (
|
||||
// Check for "Cannot find module 'foo'"
|
||||
msg.includes(`'${name}'`)
|
||||
// Check for "Your application tried to access foo (a peer dependency) ..." (Yarn v2 PnP)
|
||||
// https://github.com/yarnpkg/berry/blob/e81dc0d29bb2f41818d9c5c1c74bab1406fb979b/packages/yarnpkg-pnp/sources/loader/makeApi.ts#L680
|
||||
|| msg.includes(` ${name} `)
|
||||
// Check for "Your application tried to access foo. While ..." (Yarn v2 PnP)
|
||||
// https://github.com/yarnpkg/berry/blob/e81dc0d29bb2f41818d9c5c1c74bab1406fb979b/packages/yarnpkg-pnp/sources/loader/makeApi.ts#L704
|
||||
|| msg.includes(` ${name}. `)
|
||||
// Check for "Your application tried to access foo, but ..." (Yarn v2 PnP)
|
||||
// https://github.com/yarnpkg/berry/blob/e81dc0d29bb2f41818d9c5c1c74bab1406fb979b/packages/yarnpkg-pnp/sources/loader/makeApi.ts#L718
|
||||
|| msg.includes(` ${name}, `)
|
||||
);
|
||||
}
|
||||
|
||||
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" || !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") {
|
||||
return opts.fail(e);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
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) => tryRequire(callerRequire, path, message);
|
||||
x.resolve = (path, message) => tryResolve(callerRequire, path, message);
|
||||
return x;
|
||||
}
|
||||
|
||||
optionalRequire.try = tryRequire;
|
||||
optionalRequire.tryResolve = tryResolve;
|
||||
optionalRequire.resolve = tryResolve;
|
||||
optionalRequire.log = (message, path) => console.log(`Just FYI: ${message}; Path "${path}"`);
|
||||
module.exports = optionalRequire;
|
||||
69
node_modules/optional-require/package.json
generated
vendored
Normal file
69
node_modules/optional-require/package.json
generated
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
{
|
||||
"_from": "optional-require@^1.0.3",
|
||||
"_id": "optional-require@1.0.3",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-RV2Zp2MY2aeYK5G+B/Sps8lW5NHAzE5QClbFP15j+PWmP+T9PxlJXBOOLoSAdgwFvS4t0aMR4vpedMkbHfh0nA==",
|
||||
"_location": "/optional-require",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "optional-require@^1.0.3",
|
||||
"name": "optional-require",
|
||||
"escapedName": "optional-require",
|
||||
"rawSpec": "^1.0.3",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^1.0.3"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/mongoose/mongodb"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/optional-require/-/optional-require-1.0.3.tgz",
|
||||
"_shasum": "275b8e9df1dc6a17ad155369c2422a440f89cb07",
|
||||
"_spec": "optional-require@^1.0.3",
|
||||
"_where": "C:\\Users\\anelissen\\Development\\tools\\node\\cbor\\node_modules\\mongoose\\node_modules\\mongodb",
|
||||
"author": {
|
||||
"name": "Joel Chen"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/jchip/optional-require/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"dependencies": {},
|
||||
"deprecated": false,
|
||||
"description": "NodeJS Require that let you handle module not found error without try/catch",
|
||||
"devDependencies": {
|
||||
"chai": "^3.5.0",
|
||||
"istanbul": "^0.4.5",
|
||||
"mocha": "^3.2.0",
|
||||
"prettier": "1.19.1",
|
||||
"require-at": "^1.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
},
|
||||
"files": [
|
||||
"index.js"
|
||||
],
|
||||
"homepage": "https://github.com/jchip/optional-require#readme",
|
||||
"keywords": [
|
||||
"optional require",
|
||||
"optional",
|
||||
"require"
|
||||
],
|
||||
"license": "Apache-2.0",
|
||||
"main": "index.js",
|
||||
"name": "optional-require",
|
||||
"prettier": {
|
||||
"printWidth": 120
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/jchip/optional-require.git"
|
||||
},
|
||||
"scripts": {
|
||||
"coverage": "istanbul cover _mocha -- test/spec/*.js",
|
||||
"test": "mocha test/spec"
|
||||
},
|
||||
"version": "1.0.3"
|
||||
}
|
||||
Reference in New Issue
Block a user