|
1
|
import ts from "typescript" |
|
2
|
import Formatter from "../helpers/formatter.js" |
|
3
|
import KindsRegistry from "../kinds/registry.js" |
|
4
|
import GeneratorEventManager from "../helpers/generator-event-manager.js" |
|
5
|
import { CommonCliOptions } from "../../types/index.js" |
|
6
|
import { existsSync, readdirSync, statSync } from "node:fs" |
|
7
|
import path from "node:path" |
|
8
|
import getBasePath from "../../utils/get-base-path.js" |
|
9
|
import getMonorepoRoot from "../../utils/get-monorepo-root.js" |
|
10
|
|
|
11
|
export type Options = { |
|
12
|
paths: string[] |
|
13
|
dryRun?: boolean |
|
14
|
} & Pick<CommonCliOptions, "generateExamples"> |
|
15
|
|
|
16
|
abstract class AbstractGenerator { |
|
17
|
protected options: Options |
|
18
|
protected program?: ts.Program |
|
19
|
protected checker?: ts.TypeChecker |
|
20
|
protected formatter: Formatter |
|
21
|
protected kindsRegistry?: KindsRegistry |
|
22
|
protected generatorEventManager: GeneratorEventManager |
|
23
|
|
|
24
|
constructor(options: Options) { |
|
25
|
this.options = options |
|
26
|
this.formatter = new Formatter() |
|
27
|
this.generatorEventManager = new GeneratorEventManager() |
|
28
|
} |
|
29
|
|
|
30
|
init() { |
|
31
|
const files: string[] = [] |
|
32
|
|
|
33
|
this.options.paths.forEach((optionPath) => { |
|
34
|
if (!existsSync(optionPath)) { |
|
35
|
return |
|
36
|
} |
|
37
|
|
|
38
|
if (!statSync(optionPath).isDirectory()) { |
|
39
|
files.push(optionPath) |
|
40
|
return |
|
41
|
} |
|
42
|
|
|
43
|
// read files recursively from directory |
|
44
|
files.push( |
|
45
|
...readdirSync(optionPath, { |
|
46
|
recursive: true, |
|
47
|
encoding: "utf-8", |
|
48
|
}) |
|
49
|
.map((filePath) => path.join(optionPath, filePath)) |
|
50
|
.filter((filePath) => !statSync(filePath).isDirectory()) |
|
51
|
) |
|
52
|
}) |
|
53
|
|
|
54
|
this.program = ts.createProgram(files, this.getBaseCompilerOptions()) |
|
55
|
|
|
56
|
this.checker = this.program.getTypeChecker() |
|
57
|
|
|
58
|
const { generateExamples } = this.options |
|
59
|
|
|
60
|
this.kindsRegistry = new KindsRegistry({ |
|
61
|
checker: this.checker, |
|
62
|
generatorEventManager: this.generatorEventManager, |
|
63
|
additionalOptions: { |
|
64
|
generateExamples, |
|
65
|
}, |
|
66
|
}) |
|
67
|
} |
|
68
|
|
|
69
|
/** |
|
70
|
* Generate the docblock for the paths specified in the {@link options} class property. |
|
71
|
*/ |
|
72
|
abstract run(): void |
|
73
|
|
|
74
|
/** |
|
75
|
* Checks whether a file is included in the specified files. |
|
76
|
* |
|
77
|
* @param {string} fileName - The file to check for. |
|
78
|
* @returns {boolean} Whether the file can have docblocks generated for it. |
|
79
|
*/ |
|
80
|
isFileIncluded(fileName: string): boolean { |
|
81
|
const baseFilePath = getBasePath(fileName) |
|
82
|
return this.options.paths.some((path) => |
|
83
|
baseFilePath.startsWith(getBasePath(path)) |
|
84
|
) |
|
85
|
} |
|
86
|
|
|
87
|
/** |
|
88
|
* Reset the generator's properties for new usage. |
|
89
|
*/ |
|
90
|
reset() { |
|
91
|
this.program = undefined |
|
92
|
this.checker = undefined |
|
93
|
} |
|
94
|
|
|
95
|
getBaseCompilerOptions(): ts.CompilerOptions { |
|
96
|
const monorepoPath = getMonorepoRoot() |
|
97
|
const tsconfigBasePath = path.join(monorepoPath, "_tsconfig.base.json") |
|
98
|
|
|
99
|
const configStr = ts.sys.readFile(tsconfigBasePath) |
|
100
|
|
|
101
|
if (!configStr) { |
|
102
|
return {} |
|
103
|
} |
|
104
|
|
|
105
|
return ts.parseJsonConfigFileContent( |
|
106
|
JSON.parse(configStr), |
|
107
|
ts.sys, |
|
108
|
monorepoPath |
|
109
|
).options |
|
110
|
} |
|
111
|
} |
|
112
|
|
|
113
|
export default AbstractGenerator |