-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
graph-utils.ts
156 lines (130 loc) · 3.76 KB
/
graph-utils.ts
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import type {
FileData,
ProjectGraph,
ProjectGraphProjectNode,
} from '@nrwl/devkit';
interface Reach {
graph: ProjectGraph;
matrix: Record<string, Array<string>>;
adjList: Record<string, Array<string>>;
}
const reach: Reach = {
graph: null,
matrix: null,
adjList: null,
};
function buildMatrix(graph: ProjectGraph) {
const nodes = Object.keys(graph.nodes);
const nodesLength = nodes.length;
const adjList = {};
const matrix = {};
// create matrix value set
for (let i = 0; i < nodesLength; i++) {
const v = nodes[i];
adjList[v] = [];
// meeroslav: turns out this is 10x faster than spreading the pre-generated initMatrixValues
matrix[v] = {};
}
const projectsWithDependencies = Object.keys(graph.dependencies);
for (let i = 0; i < projectsWithDependencies.length; i++) {
const dependencies = graph.dependencies[projectsWithDependencies[i]];
for (let j = 0; j < dependencies.length; j++) {
const dependency = dependencies[j];
if (graph.nodes[dependency.target]) {
adjList[dependency.source].push(dependency.target);
}
}
}
const traverse = (s: string, v: string) => {
matrix[s][v] = true;
const adjListLength = adjList[v].length;
for (let i = 0; i < adjListLength; i++) {
const adj = adjList[v][i];
if (!matrix[s][adj]) {
traverse(s, adj);
}
}
};
for (let i = 0; i < nodesLength; i++) {
const v = nodes[i];
traverse(v, v);
}
return {
matrix,
adjList,
};
}
export function getPath(
graph: ProjectGraph,
sourceProjectName: string,
targetProjectName: string
): Array<ProjectGraphProjectNode> {
if (sourceProjectName === targetProjectName) return [];
if (reach.graph !== graph) {
const { matrix, adjList } = buildMatrix(graph);
reach.graph = graph;
reach.matrix = matrix;
reach.adjList = adjList;
}
const adjList = reach.adjList;
let path: string[] = [];
const queue: Array<[string, string[]]> = [[sourceProjectName, path]];
const visited: string[] = [sourceProjectName];
while (queue.length > 0) {
const [current, p] = queue.pop();
path = [...p, current];
if (current === targetProjectName) break;
if (!adjList[current]) break;
adjList[current]
.filter((adj) => visited.indexOf(adj) === -1)
.filter((adj) => reach.matrix[adj][targetProjectName])
.forEach((adj) => {
visited.push(adj);
queue.push([adj, [...path]]);
});
}
if (path.length > 1) {
return path.map((n) => graph.nodes[n] as ProjectGraphProjectNode);
} else {
return [];
}
}
export function pathExists(
graph: ProjectGraph,
sourceProjectName: string,
targetProjectName: string
): boolean {
if (sourceProjectName === targetProjectName) return true;
if (reach.graph !== graph) {
const { matrix, adjList } = buildMatrix(graph);
reach.graph = graph;
reach.matrix = matrix;
reach.adjList = adjList;
}
return !!reach.matrix[sourceProjectName][targetProjectName];
}
export function checkCircularPath(
graph: ProjectGraph,
sourceProject: ProjectGraphProjectNode,
targetProject: ProjectGraphProjectNode
): ProjectGraphProjectNode[] {
if (!graph.nodes[targetProject.name]) return [];
return getPath(graph, targetProject.name, sourceProject.name);
}
export function findFilesInCircularPath(
circularPath: ProjectGraphProjectNode[]
): Array<string[]> {
const filePathChain = [];
for (let i = 0; i < circularPath.length - 1; i++) {
const next = circularPath[i + 1].name;
const files: FileData[] = circularPath[i].data.files;
filePathChain.push(
Object.keys(files)
.filter(
(key) => files[key].deps && files[key].deps.indexOf(next) !== -1
)
.map((key) => files[key].file)
);
}
return filePathChain;
}