|
1 | | -import _ from 'lodash'; |
2 | | -import fs from 'fs'; |
3 | | -import glob from 'glob'; |
4 | | -import MapCache from './MapCache'; |
5 | | -import Module from 'module'; |
6 | | -import { normalizePath } from './util'; |
7 | | -import path from 'path'; |
| 1 | +import _ from 'lodash' |
| 2 | +import fs from 'fs' |
| 3 | +import glob from 'glob' |
| 4 | +import MapCache from './MapCache' |
| 5 | +import Module from 'module' |
| 6 | +import { normalizePath } from './util' |
| 7 | +import path from 'path' |
8 | 8 |
|
9 | 9 | /*----------------------------------------------------------------------------*/ |
10 | 10 |
|
11 | 11 | export default class ModuleCache extends MapCache { |
12 | 12 | constructor(moduleRoot) { |
13 | | - super(); |
| 13 | + super() |
14 | 14 |
|
15 | | - moduleRoot = _.toString(moduleRoot); |
16 | | - if (!moduleRoot) { |
17 | | - return; |
| 15 | + moduleRoot = _.toString(moduleRoot) |
| 16 | + |
| 17 | + if (! moduleRoot) { |
| 18 | + return |
18 | 19 | } |
19 | | - const pkgPath = path.join(moduleRoot, 'package.json'); |
20 | | - const pkgMain = fs.existsSync(pkgPath) && require(pkgPath).main || 'index.js'; |
21 | | - const mainPath = normalizePath(path.dirname(path.resolve(moduleRoot, pkgMain))); |
| 20 | + |
| 21 | + const pkgPath = path.join(moduleRoot, 'package.json') |
| 22 | + const pkgMain = fs.existsSync(pkgPath) && require(pkgPath).main || 'index.js' |
| 23 | + const mainPath = normalizePath(path.dirname(path.resolve(moduleRoot, pkgMain))) |
22 | 24 |
|
23 | 25 | // Sort paths by the “main” entry first. |
24 | 26 | const dirPaths = _.orderBy(glob.sync(path.join(moduleRoot, '**/'), { |
25 | 27 | 'ignore': path.join(moduleRoot, 'node_modules/**/') |
26 | | - }), dirPath => _.startsWith(dirPath, mainPath), ['desc']); |
27 | | - |
28 | | - _.each(dirPaths, dirPath => { |
29 | | - const base = path.relative(moduleRoot, dirPath); |
30 | | - const filePaths = glob.sync(path.join(dirPath, '*.js')); |
31 | | - const pairs = _.map(filePaths, filePath => { |
32 | | - const name = path.basename(filePath, '.js'); |
33 | | - return [name.toLowerCase(), name]; |
34 | | - }); |
35 | | - this.set(base, new MapCache(pairs)); |
36 | | - }); |
| 28 | + }), dirPath => _.startsWith(dirPath, mainPath), ['desc']) |
| 29 | + |
| 30 | + _.each(dirPaths, (dirPath) => { |
| 31 | + const base = path.relative(moduleRoot, dirPath) |
| 32 | + const filePaths = glob.sync(path.join(dirPath, '*.js')) |
| 33 | + const pairs = _.map(filePaths, (filePath) => { |
| 34 | + const name = path.basename(filePath, '.js') |
| 35 | + return [name.toLowerCase(), name] |
| 36 | + }) |
| 37 | + this.set(base, new MapCache(pairs)) |
| 38 | + }) |
37 | 39 | } |
38 | 40 |
|
39 | | - static resolve(id, from=process.cwd()) { |
| 41 | + static resolve(id, from = process.cwd()) { |
40 | 42 | const dirs = path.dirname(Module._resolveFilename(id, _.assign(new Module, { |
41 | 43 | 'paths': Module._nodeModulePaths(from) |
42 | | - }))).split(path.sep); |
| 44 | + }))).split(path.sep) |
| 45 | + |
| 46 | + let { length } = dirs |
43 | 47 |
|
44 | | - let { length } = dirs; |
45 | 48 | while (length--) { |
46 | | - const dirSub = dirs.slice(0, length + 1); |
47 | | - const dirPath = dirSub.join('/'); |
48 | | - const pkgPath = path.join(dirPath, 'package.json'); |
| 49 | + const dirSub = dirs.slice(0, length + 1) |
| 50 | + const dirPath = dirSub.join('/') |
| 51 | + const pkgPath = path.join(dirPath, 'package.json') |
49 | 52 |
|
50 | | - if ((length && dirs[length - 1] == 'node_modules') || |
51 | | - (fs.existsSync(pkgPath) && require(pkgPath).name == id)) { |
52 | | - return dirPath; |
| 53 | + if ((length && dirs[length - 1] === 'node_modules') || |
| 54 | + (fs.existsSync(pkgPath) && require(pkgPath).name === id)) { |
| 55 | + return dirPath |
53 | 56 | } |
54 | 57 | } |
55 | | - return dirs.join('/'); |
| 58 | + |
| 59 | + return dirs.join('/') |
56 | 60 | } |
57 | | -}; |
| 61 | +} |
0 commit comments