-
Notifications
You must be signed in to change notification settings - Fork 340
/
dependency-graph.mjs
81 lines (69 loc) · 1.81 KB
/
dependency-graph.mjs
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
// @ts-check
import { digraph } from 'graphviz'
export class DependencyGraph {
/** @type {Map<string, Set<string>>} */
graph = new Map()
hasFile(fileName) {
return this.graph.has(fileName)
}
/**
* Adds a node to the graph
* @param {string} fileName
* @returns {void}
*/
addFile(fileName) {
if (!this.graph.has(fileName)) {
this.graph.set(fileName, new Set())
}
}
addDependency(parent, child) {
if (!this.graph.has(parent)) {
this.addFile(parent)
}
if (!this.graph.has(child)) {
this.addFile(child)
}
this.graph.set(parent, this.graph.get(parent).add(child))
}
/**
* Provide a list of all affected files inside the graph based on the provided files
* @param {string[]} files
* @param {(file:string) => boolean} filterFunction
* @returns {Set<string>}
*/
affected(files, filterFunction) {
const affectedFiles = new Set()
const findParents = (leaf) => {
if (((filterFunction && filterFunction(leaf)) || !filterFunction) && this.graph.has(leaf)) {
affectedFiles.add(leaf)
}
this.graph.forEach((value, key) => {
if (value.has(leaf)) {
if ((filterFunction && filterFunction(leaf)) || !filterFunction) {
affectedFiles.add(key)
}
findParents(key)
}
})
}
files.forEach((file) => {
findParents(file)
})
return affectedFiles
}
/**
* Visualizes a dependency graph the output is a graphviz graph
* that can be printed to `.to_dot()` or rendered to a png file
* @returns {import('graphviz').Graph}
*/
visualize() {
const graph = digraph('G')
this.graph.forEach((edges, node) => {
graph.addNode(node)
edges.forEach((edge) => {
graph.addEdge(node, edge)
})
})
return graph
}
}