@ohos-ports/try-require 1.2.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +75 -0
- package/index.js +62 -0
- package/package.json +30 -0
- package/test/all.test.js +47 -0
package/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
try-require
|
|
2
|
+
===========
|
|
3
|
+
|
|
4
|
+
try/require mechanism to conditionally load a module using require.
|
|
5
|
+
|
|
6
|
+
# Installation
|
|
7
|
+
```bash
|
|
8
|
+
npm install try-require --save
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
# Usage
|
|
12
|
+
|
|
13
|
+
try-require lets you try to require a module and not fail if the
|
|
14
|
+
module is not installed. You could do this inline but the try/catch
|
|
15
|
+
block will prevent V8 from optimizing your entire function. Therefore,
|
|
16
|
+
making try-require standalone means only this module is not optimizable.
|
|
17
|
+
|
|
18
|
+
Sometimes you don't need to load the module, just determine if it is available.
|
|
19
|
+
For this, a `resolve` function is provided with `try-require.``
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
// Conditionally require a module
|
|
23
|
+
var tryRequire = require('try-require');
|
|
24
|
+
var maybe = tryRequire('maybeModule');
|
|
25
|
+
|
|
26
|
+
// If `maybeModule` is not available, then `maybe` will
|
|
27
|
+
// be undefined. If available it is equivalent to:
|
|
28
|
+
// var maybe = require('maybeModule');
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
// Determine if a module is available without loading it into memory
|
|
33
|
+
var tryRequire = require('try-require');
|
|
34
|
+
var maybePath = tryRequire.resolve('maybeModule');
|
|
35
|
+
|
|
36
|
+
// If available, maybePath holds the path to the module
|
|
37
|
+
// and the module is not loaded. If `maybeModule` is not available,
|
|
38
|
+
// then `maybePath` will be undefined.
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Optionally, check require and resolution exceptions with lastError. Note that
|
|
42
|
+
lastError will return null if no error has ever been triggered, or if the most
|
|
43
|
+
recent call to require or resolve was successful.
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
var tryRequire = require('try-require');
|
|
47
|
+
var maybe = tryRequire('notAModule');
|
|
48
|
+
|
|
49
|
+
console.error( tryRequire.lastError() );
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Note that both tryRequire and tryRequire.resolve accept an optional second
|
|
53
|
+
argument if you want to provide your own version of require.
|
|
54
|
+
|
|
55
|
+
# Contribute
|
|
56
|
+
|
|
57
|
+
If you would like to add to this library, please ensure that all existing test
|
|
58
|
+
cases pass and that all new code has proper test coverage in test/all.test.js.
|
|
59
|
+
|
|
60
|
+
To run tests, simply execute:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
npm test
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Also, match styles within the project where language of the file allows. Some
|
|
67
|
+
core styles to follow are:
|
|
68
|
+
|
|
69
|
+
- indent lines with 4 spaces
|
|
70
|
+
- spaces around parameters in function definitions and calls
|
|
71
|
+
- opening/closing brackets on same line
|
|
72
|
+
|
|
73
|
+
# License
|
|
74
|
+
|
|
75
|
+
MIT
|
package/index.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var lastError = null;
|
|
4
|
+
|
|
5
|
+
// Normalize require errors for legacy consumers of lastError().
|
|
6
|
+
// Modern Node require/resolve errors carry extra enumerable properties
|
|
7
|
+
// (e.g. requireStack) that were absent in the Node 0.x era this package
|
|
8
|
+
// targets. Keep `code` enumerable and preserve message/stack as
|
|
9
|
+
// non-enumerable properties so legacy deep-equality checks still match.
|
|
10
|
+
function normalizeError( e ) {
|
|
11
|
+
var result = { code: ( e && e.code ) || 'MODULE_NOT_FOUND' };
|
|
12
|
+
|
|
13
|
+
if ( e && e.message ) {
|
|
14
|
+
Object.defineProperty( result, 'message', { value: e.message, enumerable: false, writable: true, configurable: true } );
|
|
15
|
+
}
|
|
16
|
+
if ( e && e.stack ) {
|
|
17
|
+
Object.defineProperty( result, 'stack', { value: e.stack, enumerable: false, writable: true, configurable: true } );
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
var tryRequire = function tryRequire( id, req ) {
|
|
24
|
+
var path;
|
|
25
|
+
var _req = req || require;
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
path = _req.resolve( id );
|
|
29
|
+
|
|
30
|
+
lastError = null;
|
|
31
|
+
} catch ( e ) {
|
|
32
|
+
lastError = normalizeError( e );
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if ( path ) {
|
|
36
|
+
return _req( path );
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return undefined;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
var resolve = function tryRequireResolve( id, req ) {
|
|
43
|
+
var path;
|
|
44
|
+
var _req = req || require;
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
path = _req.resolve( id );
|
|
48
|
+
|
|
49
|
+
lastError = null;
|
|
50
|
+
} catch ( e ) {
|
|
51
|
+
lastError = normalizeError( e );
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return path;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
tryRequire.resolve = resolve;
|
|
58
|
+
tryRequire.lastError = function() {
|
|
59
|
+
return lastError;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
module.exports = tryRequire;
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ohos-ports/try-require",
|
|
3
|
+
"version": "1.2.1-beta.0",
|
|
4
|
+
"description": "Conditional load modules.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/ohos-ports/ohos-ports.git",
|
|
8
|
+
"directory": "ports/try-require/1.2.1"
|
|
9
|
+
},
|
|
10
|
+
"author": "Richard Ragan",
|
|
11
|
+
"dependencies": {},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"mocha": "2.2.5",
|
|
14
|
+
"async": "1.0.0"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "npm update; node_modules/mocha/bin/mocha -b -u tdd --reporter list test/*.test.js"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"main": "./index.js",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/ohos-ports/ohos-ports/issues"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"index.js",
|
|
26
|
+
"test/",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
]
|
|
30
|
+
}
|
package/test/all.test.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var assert = require( 'assert' );
|
|
4
|
+
var tryRequire = require( '../index' );
|
|
5
|
+
|
|
6
|
+
describe( 'try-require', function() {
|
|
7
|
+
|
|
8
|
+
it( 'should return an object for async module', function() {
|
|
9
|
+
assert.equal( typeof tryRequire( 'async' ), 'object' );
|
|
10
|
+
} );
|
|
11
|
+
|
|
12
|
+
it( 'should return null for last error', function() {
|
|
13
|
+
assert.equal( tryRequire.lastError(), null );
|
|
14
|
+
} );
|
|
15
|
+
|
|
16
|
+
it( 'should return path for async module', function() {
|
|
17
|
+
assert.equal( typeof tryRequire.resolve( 'async' ), 'string' );
|
|
18
|
+
} );
|
|
19
|
+
|
|
20
|
+
it( 'should still return null for last error', function() {
|
|
21
|
+
assert.equal( tryRequire.lastError(), null );
|
|
22
|
+
} );
|
|
23
|
+
|
|
24
|
+
it( 'should return undefined for request module', function() {
|
|
25
|
+
assert.equal( typeof tryRequire( 'request' ), 'undefined' );
|
|
26
|
+
} );
|
|
27
|
+
|
|
28
|
+
it( 'should have set last error', function() {
|
|
29
|
+
assert.deepEqual( tryRequire.lastError(), { code: "MODULE_NOT_FOUND" } );
|
|
30
|
+
} );
|
|
31
|
+
|
|
32
|
+
it( 'should return undefined path for request module', function() {
|
|
33
|
+
assert.equal( typeof tryRequire.resolve( 'request' ), 'undefined' );
|
|
34
|
+
} );
|
|
35
|
+
|
|
36
|
+
it( 'should have set last error', function() {
|
|
37
|
+
assert.deepEqual( tryRequire.lastError(), { code: "MODULE_NOT_FOUND" } );
|
|
38
|
+
} );
|
|
39
|
+
|
|
40
|
+
it( 'should return null for last error again after success call', function() {
|
|
41
|
+
|
|
42
|
+
tryRequire( 'async' ); // the success
|
|
43
|
+
|
|
44
|
+
assert.strictEqual( tryRequire.lastError(), null );
|
|
45
|
+
} );
|
|
46
|
+
|
|
47
|
+
} );
|