Skip to content

Commit 91af9d9

Browse files
committed
feat(app): create graph using BFS if source or target missing
1 parent 0f76144 commit 91af9d9

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

src/app/workers/graph.worker.ts

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,40 @@ ctx.addEventListener('message', (ev: MessageEvent<Args>) => {
1515
function createDepGraph(args: Args): DepGraph {
1616
const { moduleDeps, sourceModules, targetModules } = args;
1717

18+
if (sourceModules.length === 0) {
19+
const { vertices, edges } = bfs(
20+
targetModules,
21+
module => moduleDeps.importedBy[module]?.map(({ path }) => path) ?? [],
22+
);
23+
return {
24+
modules: vertices.map(module => moduleDeps.modules[module]),
25+
imports: edges.map(({ from, to }) => ({
26+
fromPath: from,
27+
toPath: to,
28+
isDynamic:
29+
moduleDeps.importedBy[from].find(({ path }) => path === to)
30+
?.isDynamic ?? false,
31+
})),
32+
};
33+
}
34+
35+
if (targetModules.length === 0) {
36+
const { vertices, edges } = bfs(
37+
sourceModules,
38+
module => moduleDeps.deps[module]?.map(({ path }) => path) ?? [],
39+
);
40+
return {
41+
modules: vertices.map(module => moduleDeps.modules[module]),
42+
imports: edges.map(({ from, to }) => ({
43+
fromPath: from,
44+
toPath: to,
45+
isDynamic:
46+
moduleDeps.deps[from].find(({ path }) => path === to)?.isDynamic ??
47+
false,
48+
})),
49+
};
50+
}
51+
1852
const paths = findAllPaths(
1953
targetModules,
2054
sourceModules,
@@ -48,7 +82,36 @@ function createDepGraph(args: Args): DepGraph {
4882
};
4983
}
5084

51-
function findAllPaths<T>(from: T[], to: T[], adjacent: (vertex: T) => T[]) {
85+
function bfs<T>(
86+
from: T[],
87+
adjacent: (vertex: T) => T[],
88+
): {
89+
vertices: T[];
90+
edges: { from: T; to: T }[];
91+
} {
92+
const queue = from;
93+
const discovered = new Set(from);
94+
const vertices: T[] = [];
95+
const edges: { from: T; to: T }[] = [];
96+
while (queue.length > 0) {
97+
const vertex = queue.shift()!;
98+
vertices.push(vertex);
99+
for (const other of adjacent(vertex)) {
100+
edges.push({ from: vertex, to: other });
101+
if (!discovered.has(other)) {
102+
queue.push(other);
103+
discovered.add(other);
104+
}
105+
}
106+
}
107+
return { vertices, edges };
108+
}
109+
110+
function findAllPaths<T>(
111+
from: T[],
112+
to: T[],
113+
adjacent: (vertex: T) => T[],
114+
): T[][] {
52115
const ends = new Set(to);
53116
const isPathEnd = (vertex: T) => ends.has(vertex);
54117

@@ -69,7 +132,7 @@ function findAllPathsUtil<T>(
69132
visited: Set<T>,
70133
path: T[],
71134
paths: T[][],
72-
) {
135+
): void {
73136
visited.add(vertex);
74137
path.push(vertex);
75138

src/cli/reporter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ export function createReporterOutput(): void {
6969
);
7070
fsSpinner.succeed('Created dependency graph');
7171
}
72+
73+
if (require.main === module) {
74+
createReporterOutput();
75+
}

0 commit comments

Comments
 (0)