-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (78 loc) · 1.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*!
* helper-resolve <https://github.com/helpers/helper-resolve>
*
* Copyright (c) 2015-2018, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
const path = require('path');
const clone = require('clone-deep');
const relative = require('relative');
const red = require('ansi-red');
const bold = require('ansi-bold');
/**
* Asynchronously get the resolved path to "main" file for
* the given module.
*
* ```js
* resolve('micromatch', function(err, fp) {
* //=> 'node_modules/micromatch/index.js'
* });
* ```
*
* @param {String} `name` The name of the module to resolve.
* @param {Function} `next` Callback function
* @return {String} File path to the module
*/
function resolve(name, next) {
try {
next(null, resolveSync(name));
} catch (err) {
next(err);
}
}
/**
* Synchronously get the resolved path to "main" file for
* the given module.
*
* ```js
* var fp = resolve.sync('micromatch');
* //=> 'node_modules/micromatch/index.js'
* ```
*
* @param {String} `name` The name of the module to resolve.
* @param {Function} `next` Callback function
* @return {String} File path to the module
*/
function resolveSync(name) {
const base = path.resolve(process.cwd(), 'node_modules', name);
const pkg = tryResolve(path.join(base, 'package.json'));
const res = clone(pkg);
res.main = relative(path.join(base, pkg && pkg.main));
return res;
}
/**
* Try to require a file, fail silently if unsuccesful
*
* @param {String} `fp`
* @return {String} Resolved filepath
*/
function tryResolve(fp) {
if (typeof fp === 'undefined') {
throw new Error('helpers-resolve: tryResolve() requires a string.');
}
try {
return require(path.resolve(fp));
} catch (err) {
console.error(red('helper-resolve cannot find'), bold(fp), err);
}
return {};
};
/**
* Expose `resolve` helper
*/
module.exports = resolve;
/**
* Expose `resolve.sync` helper
*/
module.exports.sync = resolveSync;