|
1
|
import ts from "typescript" |
|
2
|
import EventsKindGenerator from "../kinds/events.js" |
|
3
|
import AbstractGenerator from "./index.js" |
|
4
|
import { GeneratorEvent } from "../helpers/generator-event-manager.js" |
|
5
|
import { minimatch } from "minimatch" |
|
6
|
import getBasePath from "../../utils/get-base-path.js" |
|
7
|
import { getEventsOutputBasePath } from "../../utils/get-output-base-paths.js" |
|
8
|
|
|
9
|
class EventsGenerator extends AbstractGenerator { |
|
10
|
protected eventsKindGenerator?: EventsKindGenerator |
|
11
|
|
|
12
|
async run() { |
|
13
|
this.init() |
|
14
|
|
|
15
|
this.eventsKindGenerator = new EventsKindGenerator({ |
|
16
|
checker: this.checker!, |
|
17
|
generatorEventManager: this.generatorEventManager, |
|
18
|
}) |
|
19
|
const events: Record<string, unknown>[] = [] |
|
20
|
|
|
21
|
await Promise.all( |
|
22
|
this.program!.getSourceFiles().map(async (file) => { |
|
23
|
// Ignore .d.ts files |
|
24
|
if (file.isDeclarationFile || !this.isFileIncluded(file.fileName)) { |
|
25
|
return |
|
26
|
} |
|
27
|
|
|
28
|
const fileNodes: ts.Node[] = [file] |
|
29
|
|
|
30
|
console.log(`[EVENTS] Generating for ${file.fileName}...`) |
|
31
|
|
|
32
|
// since typescript's compiler API doesn't support |
|
33
|
// async processes, we have to retrieve the nodes first then |
|
34
|
// traverse them separately. |
|
35
|
const pushNodesToArr = (node: ts.Node) => { |
|
36
|
fileNodes.push(node) |
|
37
|
|
|
38
|
ts.forEachChild(node, pushNodesToArr) |
|
39
|
} |
|
40
|
ts.forEachChild(file, pushNodesToArr) |
|
41
|
|
|
42
|
await this.eventsKindGenerator!.populateWorkflows() |
|
43
|
|
|
44
|
const documentChild = async (node: ts.Node) => { |
|
45
|
if ( |
|
46
|
this.eventsKindGenerator!.isAllowed(node) && |
|
47
|
this.eventsKindGenerator!.canDocumentNode(node) |
|
48
|
) { |
|
49
|
const eventsJson = await this.eventsKindGenerator!.getDocBlock(node) |
|
50
|
events.push(...JSON.parse(eventsJson)) |
|
51
|
} |
|
52
|
} |
|
53
|
|
|
54
|
await Promise.all( |
|
55
|
fileNodes.map(async (node) => await documentChild(node)) |
|
56
|
) |
|
57
|
|
|
58
|
this.generatorEventManager.emit(GeneratorEvent.FINISHED_GENERATE_EVENT) |
|
59
|
console.log(`[EVENTS] Finished generating for ${file.fileName}.`) |
|
60
|
}) |
|
61
|
) |
|
62
|
|
|
63
|
if (!this.options.dryRun) { |
|
64
|
this.writeJson(events) |
|
65
|
} |
|
66
|
} |
|
67
|
|
|
68
|
/** |
|
69
|
* Checks whether the specified file path is included in the program |
|
70
|
* and is an API file. |
|
71
|
* |
|
72
|
* @param fileName - The file path to check |
|
73
|
* @returns Whether the OAS generator can run on this file. |
|
74
|
*/ |
|
75
|
isFileIncluded(fileName: string): boolean { |
|
76
|
return ( |
|
77
|
super.isFileIncluded(fileName) && |
|
78
|
minimatch( |
|
79
|
getBasePath(fileName), |
|
80
|
"packages/core/utils/src/**/events.ts", |
|
81
|
{ |
|
82
|
matchBase: true, |
|
83
|
} |
|
84
|
) |
|
85
|
) |
|
86
|
} |
|
87
|
|
|
88
|
/** |
|
89
|
* This method writes the DML JSON file. If the file already exists, it only updates |
|
90
|
* the data model's object in the JSON file. |
|
91
|
* |
|
92
|
* @param filePath - The path of the file to write the DML JSON to. |
|
93
|
* @param dataModelJson - The DML JSON. |
|
94
|
*/ |
|
95
|
writeJson(events: Record<string, unknown>[]) { |
|
96
|
const filePath = getEventsOutputBasePath() |
|
97
|
const eventsJson = JSON.stringify(events, null, 2) |
|
98
|
|
|
99
|
ts.sys.writeFile(filePath, eventsJson) |
|
100
|
} |
|
101
|
} |
|
102
|
|
|
103
|
export default EventsGenerator |