1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | 'use strict'
|
13 |
|
14 | const path = require('path'),
|
15 | _isModule = require('./_is_module');
|
16 |
|
17 |
|
18 | function resolve (name, options = {}) {
|
19 | if (_isModule(name)) {
|
20 | return name
|
21 | }
|
22 | let cwd = options.cwd || process.cwd()
|
23 | let safe = !!options.safe
|
24 | do {
|
25 | let fromDir = path.join(cwd, name)
|
26 | if (_isModule(fromDir)) {
|
27 | return fromDir
|
28 | }
|
29 | var fromNodeModulesDir = path.join(cwd, 'node_modules', name)
|
30 | if (_isModule(fromNodeModulesDir)) {
|
31 | return fromNodeModulesDir
|
32 | }
|
33 |
|
34 | var parentBasedir = path.dirname(cwd)
|
35 | if (parentBasedir === cwd) {
|
36 | break
|
37 | }
|
38 | cwd = parentBasedir
|
39 | } while (true)
|
40 | if (safe) {
|
41 | return null
|
42 | } else {
|
43 | throw new Error(`Module not found: ${name}`)
|
44 | }
|
45 | }
|
46 |
|
47 | module.exports = resolve
|