chushihua
This commit is contained in:
+6
@@ -0,0 +1,6 @@
|
||||
ISC License (ISC)
|
||||
Copyright 2018 Shinnosuke Watanabe
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
# is-resolvable
|
||||
|
||||
[](https://www.npmjs.com/package/is-resolvable)
|
||||
[](https://travis-ci.org/shinnn/is-resolvable)
|
||||
[](https://ci.appveyor.com/project/ShinnosukeWatanabe/is-resolvable)
|
||||
[](https://coveralls.io/r/shinnn/is-resolvable)
|
||||
|
||||
A [Node.js](https://nodejs.org/) module to check if a given module ID is resolvable with [`require()`](https://nodejs.org/api/globals.html#globals_require)
|
||||
|
||||
```javascript
|
||||
const isResolvable = require('is-resolvable');
|
||||
|
||||
isResolvable('fs'); //=> true
|
||||
isResolvable('path'); //=> true
|
||||
|
||||
// When ./index.js exists
|
||||
isResolvable('./index.js') //=> true
|
||||
isResolvable('./index') //=> true
|
||||
isResolvable('.') //=> true
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/getting-started/what-is-npm).
|
||||
|
||||
```
|
||||
npm install is-resolvable
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
```javascript
|
||||
const isResolvable = require('is-resolvable');
|
||||
```
|
||||
|
||||
### isResolvable(*moduleId* [, *options*])
|
||||
|
||||
*moduleId*: `string` (module ID)
|
||||
*options*: `Object` ([`require.resolve`](https://nodejs.org/api/modules.html#modules_require_resolve_request_options) options)
|
||||
Return: `boolean`
|
||||
|
||||
It returns `true` if `require()` can load a file form a given module ID, otherwise `false`.
|
||||
|
||||
```javascript
|
||||
const isResolvable = require('is-resolvable');
|
||||
|
||||
// When ./foo.json exists
|
||||
isResolvable('./foo.json'); //=> true
|
||||
isResolvable('./foo'); //=> true
|
||||
|
||||
isResolvable('./foo.js'); //=> false
|
||||
|
||||
// When `eslint` module is installed but `jshint` isn't
|
||||
isResolvable('eslint'); //=> true
|
||||
isResolvable('jshint'); //=> false
|
||||
|
||||
// When `lodash` module is installed
|
||||
isResolvable('lodash/isObject'); //=> true
|
||||
isResolvable('lodash/fp/reject.js'); //=> true
|
||||
```
|
||||
|
||||
The second argument accepts an options object for `require.resolve()`.
|
||||
|
||||
```javascript
|
||||
// When ./bar/baz.js exists
|
||||
|
||||
isResolvable('./baz.js'); //=> false
|
||||
isResolvable('./baz.js', {paths: ['bar']}); //=> true
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
[ISC License](./LICENSE) © 2018 Shinnosuke Watanabe
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
var inspect = require('util').inspect;
|
||||
|
||||
module.exports = function isResolvable(moduleId, options) {
|
||||
if (typeof moduleId !== 'string') {
|
||||
throw new TypeError(inspect(moduleId) + ' is not a string. Expected a valid Node.js module identifier (<string>), for example \'eslint\', \'./index.js\', \'./lib\'.');
|
||||
}
|
||||
|
||||
try {
|
||||
require.resolve(moduleId, options);
|
||||
return true;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "is-resolvable",
|
||||
"version": "1.1.0",
|
||||
"description": "Check if a module ID is resolvable with require()",
|
||||
"repository": "shinnn/is-resolvable",
|
||||
"author": "Shinnosuke Watanabe (https://github.com/shinnn)",
|
||||
"scripts": {
|
||||
"pretest": "eslint --fix --format=codeframe index.js test.js",
|
||||
"test": "node --throw-deprecation --track-heap-objects test.js",
|
||||
"coverage": "istanbul cover --print=both test.js"
|
||||
},
|
||||
"license": "ISC",
|
||||
"files": [
|
||||
"index.js"
|
||||
],
|
||||
"keywords": [
|
||||
"file",
|
||||
"path",
|
||||
"resolve",
|
||||
"resolvable",
|
||||
"check",
|
||||
"module"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@shinnn/eslint-config-node": "^5.0.0",
|
||||
"eslint": "^4.16.0",
|
||||
"istanbul": "^0.4.5",
|
||||
"tape": "^4.8.0"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"extends": "@shinnn/node",
|
||||
"rules": {
|
||||
"no-var": "off",
|
||||
"prefer-template": "off"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user