1 import ts from "typescript"
2 import DmlKindGenerator from "../kinds/dml.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 toJsonFormatted from "../../utils/to-json-formatted.js"
8 import { DmlFile } from "types"
9
10 /**
11 * A class used to generate DML JSON files with descriptions of properties.
12 */
13 class DmlGenerator extends AbstractGenerator {
14 protected dmlKindGenerator?: DmlKindGenerator
15
16 async run() {
17 this.init()
18
19 this.dmlKindGenerator = new DmlKindGenerator({
20 checker: this.checker!,
21 generatorEventManager: this.generatorEventManager,
22 })
23
24 await Promise.all(
25 this.program!.getSourceFiles().map(async (file) => {
26 // Ignore .d.ts files
27 if (file.isDeclarationFile || !this.isFileIncluded(file.fileName)) {
28 return
29 }
30 const fileNodes: ts.Node[] = [file]
31
32 console.log(`[DML] Generating for ${file.fileName}...`)
33
34 // since typescript's compiler API doesn't support
35 // async processes, we have to retrieve the nodes first then
36 // traverse them separately.
37 const pushNodesToArr = (node: ts.Node) => {
38 fileNodes.push(node)
39
40 ts.forEachChild(node, pushNodesToArr)
41 }
42 ts.forEachChild(file, pushNodesToArr)
43
44 const documentChild = async (node: ts.Node) => {
45 if (
46 this.dmlKindGenerator!.isAllowed(node) &&
47 this.dmlKindGenerator!.canDocumentNode(node)
48 ) {
49 const dmlJson = await this.dmlKindGenerator!.getDocBlock(node)
50
51 if (!this.options.dryRun) {
52 const filePath =
53 this.dmlKindGenerator!.getAssociatedFileName(node)
54 this.writeJson(filePath, dmlJson)
55 }
56 }
57 }
58
59 await Promise.all(
60 fileNodes.map(async (node) => await documentChild(node))
61 )
62
63 this.generatorEventManager.emit(GeneratorEvent.FINISHED_GENERATE_EVENT)
64 console.log(`[DML] Finished generating DML for ${file.fileName}.`)
65 })
66 )
67 }
68
69 /**
70 * Checks whether the specified file path is included in the program
71 * and is an API file.
72 *
73 * @param fileName - The file path to check
74 * @returns Whether the DML generator can run on this file.
75 */
76 isFileIncluded(fileName: string): boolean {
77 return (
78 super.isFileIncluded(fileName) &&
79 minimatch(getBasePath(fileName), "packages/modules/**/models/**", {
80 matchBase: true,
81 })
82 )
83 }
84
85 /**
86 * This method writes the DML JSON file. If the file already exists, it only updates
87 * the data model's object in the JSON file.
88 *
89 * @param filePath - The path of the file to write the DML JSON to.
90 * @param dataModelJson - The DML JSON.
91 */
92 writeJson(filePath: string, dataModelJson: string) {
93 let moduleJson = ts.sys.readFile(filePath)
94 if (!moduleJson) {
95 moduleJson = dataModelJson
96 } else {
97 // parse the JSON and replace the data model's JSON
98 // with the new one
99 const parsedModuleJson = JSON.parse(moduleJson) as DmlFile
100 const parsedDataModelJson = JSON.parse(dataModelJson) as DmlFile
101 const dataModelName = Object.keys(parsedDataModelJson)[0]
102 parsedModuleJson[dataModelName] = parsedDataModelJson[dataModelName]
103
104 moduleJson = toJsonFormatted(parsedModuleJson)
105 }
106
107 ts.sys.writeFile(filePath, moduleJson)
108 }
109 }
110
111 export default DmlGenerator