-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependencyGraphRegenerator.ts
More file actions
100 lines (83 loc) · 3.02 KB
/
Copy pathDependencyGraphRegenerator.ts
File metadata and controls
100 lines (83 loc) · 3.02 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { HtmlDataExtractor } from '@shared/visualServer/HtmlDataExtractor';
import { TemplateProcessor } from '@shared/visualServer/TemplateProcessor';
import * as fs from 'fs';
import * as path from 'path';
export class DependencyGraphRegenerator {
private readonly serviceKeys = [
'BUCHUNG',
'DATALAKE',
'DEVICETOKEN',
'KUNDENKONTO',
'LOCATION',
'PRIVATKUNDE',
'REISEN',
'REISEWUNSCH',
'STAMMDATEN',
'WARENKORB',
'ZAHLUNG',
];
constructor(
private readonly extractor: HtmlDataExtractor,
private readonly processor: TemplateProcessor,
) {}
public execute(): void {
try {
const templatePath = path.join(__dirname, 'graph.template.html');
const outputPath = path.join(process.cwd(), 'dependency-graph.html');
if (!fs.existsSync(templatePath)) {
console.error(`Error: Template not found at ${templatePath}`);
process.exit(1);
}
const existingData = this.getExistingData(outputPath);
const template = fs.readFileSync(templatePath, 'utf-8');
const newHtml = this.processTemplate(template, existingData);
fs.writeFileSync(outputPath, newHtml);
console.log(`Dependency graph HTML successfully regenerated at: ${outputPath}`);
console.log(`Preserved data for ${this.countPreservedServices(existingData)} services.`);
} catch (error) {
console.error('Error regenerating HTML graph:', error);
process.exit(1);
}
}
private getExistingData(outputPath: string): Map<string, string> {
const dataMap = new Map<string, string>();
if (fs.existsSync(outputPath)) {
console.log(`Reading existing data from ${outputPath}...`);
const content = fs.readFileSync(outputPath, 'utf-8');
this.serviceKeys.forEach((key) => {
const data = this.extractor.extractArray(content, `${key}_DATA`);
if (data) {
dataMap.set(key, data);
}
});
const definitions = this.extractor.extractGlobalDefinitions(content);
if (definitions) {
dataMap.set('GLOBAL_DEFINITIONS', definitions);
console.log('Preserved GLOBAL_DEFINITIONS.');
}
}
return dataMap;
}
private processTemplate(template: string, dataMap: Map<string, string>): string {
let result = template;
this.serviceKeys.forEach((key) => {
if (dataMap.has(key)) {
result = this.processor.injectServiceData(result, key, dataMap.get(key)!);
}
});
const definitions = dataMap.get('GLOBAL_DEFINITIONS') || '{}';
const globalDefRegex = /(?:let|const) GLOBAL_DEFINITIONS = \{\};(?: \/\/ \{\{ GLOBAL_DEFINITIONS_DATA \}\})?/;
if (!globalDefRegex.test(result)) {
console.warn('Could not find placeholder for GLOBAL_DEFINITIONS in template.');
}
result = this.processor.injectGlobalDefinitions(result, definitions);
return result;
}
private countPreservedServices(dataMap: Map<string, string>): number {
let count = 0;
this.serviceKeys.forEach((key) => {
if (dataMap.has(key)) count++;
});
return count;
}
}