-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlantUMLGenerator.ts
More file actions
50 lines (41 loc) · 1.85 KB
/
PlantUMLGenerator.ts
File metadata and controls
50 lines (41 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { ContextMapperGenerator } from './ContextMapperGenerator.js'
import path from 'node:path'
import fs from 'node:fs'
import { ContextMappingModel } from '../../generated/ast.js'
import { ComponentDiagramGenerator } from './plantuml/ComponentDiagramGenerator.js'
import { CancellationToken } from 'vscode-languageserver'
export class PlantUMLGenerator implements ContextMapperGenerator {
async generate (model: ContextMappingModel, filePath: string, args: unknown[], cancelToken: CancellationToken): Promise<string[]> {
// there must not be any extra spaces especially at the start, since the path will be treated as relative otherwise
const destination = (args[0] as string)?.trim()
if (destination == null || destination === '') {
throw Error('Destination must be specified')
}
if (cancelToken.isCancellationRequested) {
return []
}
const fileName = filePath.split('/').pop()!.split('.')[0]
if (!fs.existsSync(destination)) {
await fs.promises.mkdir(destination, { recursive: true })
}
const diagrams: string[] = []
const componentDiagram = await this.generateComponentDiagram(model, destination, fileName)
if (componentDiagram) {
diagrams.push(componentDiagram)
}
return diagrams
}
private async generateComponentDiagram (model: ContextMappingModel, destination: string, fileName: string): Promise<string | undefined> {
if (model.contextMap.length === 0) {
return
}
const generator = new ComponentDiagramGenerator()
const diagram = generator.createDiagram(model.contextMap[0])
return await this.createFile(destination, fileName, diagram)
}
private async createFile (destination: string, fileName: string, content: string) {
const filePath = `${path.join(destination, fileName)}.puml`
await fs.promises.writeFile(filePath, content)
return filePath
}
}