|
1
|
import { minimatch } from "minimatch" |
|
2
|
import AbstractGenerator from "./index.js" |
|
3
|
import ts from "typescript" |
|
4
|
import OasKindGenerator from "../kinds/oas.js" |
|
5
|
import { GeneratorEvent } from "../helpers/generator-event-manager.js" |
|
6
|
import getBasePath from "../../utils/get-base-path.js" |
|
7
|
|
|
8
|
/** |
|
9
|
* A class used to generate OAS yaml comments. The comments are written |
|
10
|
* in different files than the specified files. |
|
11
|
*/ |
|
12
|
class OasGenerator extends AbstractGenerator { |
|
13
|
protected oasKindGenerator?: OasKindGenerator |
|
14
|
|
|
15
|
async run() { |
|
16
|
this.init() |
|
17
|
|
|
18
|
const { generateExamples } = this.options |
|
19
|
|
|
20
|
this.oasKindGenerator = new OasKindGenerator({ |
|
21
|
checker: this.checker!, |
|
22
|
generatorEventManager: this.generatorEventManager, |
|
23
|
additionalOptions: { |
|
24
|
generateExamples, |
|
25
|
}, |
|
26
|
}) |
|
27
|
|
|
28
|
await Promise.all( |
|
29
|
this.program!.getSourceFiles().map(async (file) => { |
|
30
|
// Ignore .d.ts files |
|
31
|
if (file.isDeclarationFile || !this.isFileIncluded(file.fileName)) { |
|
32
|
return |
|
33
|
} |
|
34
|
const fileNodes: ts.Node[] = [file] |
|
35
|
|
|
36
|
console.log(`[OAS] Generating for ${file.fileName}...`) |
|
37
|
|
|
38
|
// since typescript's compiler API doesn't support |
|
39
|
// async processes, we have to retrieve the nodes first then |
|
40
|
// traverse them separately. |
|
41
|
const pushNodesToArr = (node: ts.Node) => { |
|
42
|
fileNodes.push(node) |
|
43
|
|
|
44
|
ts.forEachChild(node, pushNodesToArr) |
|
45
|
} |
|
46
|
ts.forEachChild(file, pushNodesToArr) |
|
47
|
|
|
48
|
const documentChild = async (node: ts.Node) => { |
|
49
|
if ( |
|
50
|
this.oasKindGenerator!.isAllowed(node) && |
|
51
|
this.oasKindGenerator!.canDocumentNode(node) |
|
52
|
) { |
|
53
|
const oas = await this.oasKindGenerator!.getDocBlock(node) |
|
54
|
|
|
55
|
if (!this.options.dryRun) { |
|
56
|
const filename = |
|
57
|
this.oasKindGenerator!.getAssociatedFileName(node) |
|
58
|
ts.sys.writeFile( |
|
59
|
filename, |
|
60
|
this.formatter.addCommentsToSourceFile(oas, "") |
|
61
|
) |
|
62
|
} |
|
63
|
} |
|
64
|
} |
|
65
|
|
|
66
|
await Promise.all( |
|
67
|
fileNodes.map(async (node) => await documentChild(node)) |
|
68
|
) |
|
69
|
|
|
70
|
this.generatorEventManager.emit(GeneratorEvent.FINISHED_GENERATE_EVENT) |
|
71
|
console.log(`[OAS] Finished generating OAS for ${file.fileName}.`) |
|
72
|
}) |
|
73
|
) |
|
74
|
} |
|
75
|
|
|
76
|
/** |
|
77
|
* Checks whether the specified file path is included in the program |
|
78
|
* and is an API file. |
|
79
|
* |
|
80
|
* @param fileName - The file path to check |
|
81
|
* @returns Whether the OAS generator can run on this file. |
|
82
|
*/ |
|
83
|
isFileIncluded(fileName: string): boolean { |
|
84
|
return ( |
|
85
|
super.isFileIncluded(fileName) && |
|
86
|
minimatch(getBasePath(fileName), "**/api**/**", { |
|
87
|
matchBase: true, |
|
88
|
}) |
|
89
|
) |
|
90
|
} |
|
91
|
} |
|
92
|
|
|
93
|
export default OasGenerator |