-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependencyGraphHelper.ts
More file actions
49 lines (41 loc) · 1.51 KB
/
Copy pathDependencyGraphHelper.ts
File metadata and controls
49 lines (41 loc) · 1.51 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
import * as fs from 'fs';
import * as path from 'path';
import { NormalizedGraph, GraphTree } from '@shared/visualServer/GraphNormalizer';
export class DependencyGraphHelper {
private readonly jsonPath: string;
constructor(jsonPath?: string) {
this.jsonPath = jsonPath || path.join(process.cwd(), 'dependency-graph.json');
}
public load(): NormalizedGraph {
if (!fs.existsSync(this.jsonPath)) {
return { definitions: {}, trees: [] };
}
try {
const existingContent = fs.readFileSync(this.jsonPath, 'utf-8');
const parsed = JSON.parse(existingContent);
if (Array.isArray(parsed)) {
return { definitions: {}, trees: parsed };
} else if (parsed.definitions && parsed.trees) {
return parsed;
}
} catch {
console.warn('Failed to parse existing dependency-graph.json, starting fresh.');
}
return { definitions: {}, trees: [] };
}
public save(graphData: NormalizedGraph): void {
fs.writeFileSync(this.jsonPath, JSON.stringify(graphData));
console.log(`Dependency graph data saved at: ${this.jsonPath}`);
}
public pruneOldServiceData(graphData: NormalizedGraph, containerName: string): void {
graphData.trees = graphData.trees.filter((tree) => {
if (Array.isArray(tree) && tree.length > 0 && tree[0].node) {
return tree[0].node.containerName !== containerName;
}
return true;
});
}
public addTree(graphData: NormalizedGraph, newTree: GraphTree[]): void {
graphData.trees.push(newTree);
}
}