|
1 |
| -const {context} = require("../context"); |
2 |
| -const path = require("path"); |
3 |
| -const fs = require("fs"); |
4 |
| -const normalizePath = require("normalize-path"); |
5 |
| -const mapExported = new Map(); |
| 1 | +const {context} = require('../context') |
| 2 | +const path = require('path') |
| 3 | +const fs = require('fs') |
| 4 | +const normalizePath = require('normalize-path') |
| 5 | +const readPkgUp = require('read-pkg-up') |
| 6 | +const mapExported = new Map() |
6 | 7 |
|
7 | 8 | class DocFile {
|
8 | 9 | /**
|
9 | 10 | *
|
10 | 11 | * @param file
|
11 | 12 | */
|
12 | 13 | constructor(file) {
|
13 |
| - this.file = normalizePath(file); |
14 |
| - this.symbols = new Map(); |
15 |
| - this.contents = fs.readFileSync(file).toString(); |
| 14 | + this.file = normalizePath(file) |
| 15 | + this.symbols = new Map() |
| 16 | + this.contents = fs.readFileSync(file).toString() |
16 | 17 | }
|
17 | 18 |
|
18 | 19 | get path() {
|
19 |
| - return normalizePath((this.file || "").replace(".d.ts", ".ts")); |
| 20 | + return normalizePath((this.file || '').replace('.d.ts', '.ts')) |
20 | 21 | }
|
21 | 22 |
|
22 | 23 | get srcPath() {
|
23 |
| - return normalizePath(context.srcResolver(this.path)); |
| 24 | + return normalizePath(context.srcResolver(this.path)) |
24 | 25 | }
|
25 | 26 |
|
26 | 27 | get relativePackagePath() {
|
27 |
| - const p = normalizePath(path.join(context.rootDir, context.packagesDir)); |
| 28 | + const p = normalizePath(path.join(context.rootDir, context.packagesDir)) |
28 | 29 |
|
29 |
| - return normalizePath(this.srcPath.replace(p, "")); |
| 30 | + return normalizePath(this.srcPath.replace(p, '')) |
30 | 31 | }
|
31 | 32 |
|
32 | 33 | get relativePath() {
|
33 |
| - return normalizePath(this.srcPath.replace(`${context.rootDir}/`, "")); |
| 34 | + return normalizePath(this.srcPath.replace(`${context.rootDir}/`, '')) |
34 | 35 | }
|
35 | 36 |
|
36 | 37 | get module() {
|
37 |
| - const [pkgName, subPkgName] = this.relativePackagePath.replace(/^\//, "").split("/"); |
38 |
| - const pkgSettings = context.modules[pkgName]; |
39 |
| - let moduleName = pkgName; |
| 38 | + const [packagePath] = this.relativePackagePath.split('/src') |
| 39 | + const packageDir = context.rootDir + '/' + context.packagesDir + '/' + packagePath |
40 | 40 |
|
41 |
| - if (typeof pkgSettings === "object" && context.modules[pkgName][subPkgName]) { |
42 |
| - moduleName = pkgName + "/" + subPkgName; |
43 |
| - } |
| 41 | + const {packageJson: {name}} = readPkgUp.sync({cwd: packageDir}) |
44 | 42 |
|
45 | 43 | return {
|
46 |
| - modulePath: path.join(context.rootDir, context.packagesDir, pkgName), |
47 |
| - moduleName: `${context.scope}/${moduleName}`, |
48 |
| - importFrom: `${context.scope}/${pkgName}`, |
49 |
| - pkgName, |
50 |
| - subPkgName |
51 |
| - }; |
| 44 | + modulePath: packageDir, |
| 45 | + moduleName: name, |
| 46 | + importFrom: name, |
| 47 | + pkgName: name.replace(context.scope, '') |
| 48 | + } |
52 | 49 | }
|
53 | 50 |
|
54 | 51 | requireModule() {
|
55 |
| - const {modulePath} = this.module; |
56 |
| - let file = path.join(modulePath, "index.js"); |
| 52 | + const {modulePath} = this.module |
| 53 | + let file = path.join(modulePath, 'index.js') |
57 | 54 |
|
58 |
| - if (fs.existsSync(path.join(modulePath, "package.json"))) { |
59 |
| - const pkg = require(path.join(modulePath, "package.json")); |
60 |
| - file = path.join(modulePath, pkg.main); |
| 55 | + if (fs.existsSync(path.join(modulePath, 'package.json'))) { |
| 56 | + const pkg = require(path.join(modulePath, 'package.json')) |
| 57 | + file = path.join(modulePath, pkg.main) |
61 | 58 | }
|
62 | 59 |
|
63 | 60 | if (mapExported.has(file)) {
|
64 |
| - return mapExported.get(file); |
| 61 | + return mapExported.get(file) |
65 | 62 | }
|
66 | 63 |
|
67 | 64 | if (fs.existsSync(file)) {
|
68 |
| - mapExported.set(file, require(file)); |
69 |
| - return mapExported.get(file); |
| 65 | + mapExported.set(file, require(file)) |
| 66 | + return mapExported.get(file) |
70 | 67 | }
|
71 | 68 |
|
72 |
| - return undefined; |
| 69 | + return undefined |
73 | 70 | }
|
74 | 71 | }
|
75 | 72 |
|
76 |
| -module.exports.DocFile = DocFile; |
| 73 | +module.exports.DocFile = DocFile |
0 commit comments