-
Notifications
You must be signed in to change notification settings - Fork 0
/
imports-graph.ts
229 lines (198 loc) · 6.24 KB
/
imports-graph.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// For JS/TS files in a given dir (or current dir), creates the DOT graph
// If git presents, respect git (git ls-files)
// deno --allow-run=git --alow-read imports-graph.ts
import { walk } from "jsr:@std/fs@1.0.4";
import * as path from "jsr:@std/path@1.0.6";
type FileName = string;
type Label = string;
type DirName = string;
type Heights = Record<FileName, number>;
type DirToFiles = Record<DirName, FileName[]>;
type Edge = { sourceNode: FileName; targetNode: FileName; label: Label };
async function findTsJsFiles(dir: DirName): Promise<FileName[]> {
const gitTrackedFiles = await getGitTrackedFiles(dir);
const files: string[] = [];
for await (const entry of walk(dir, { exts: [".ts", ".js"] })) {
if (entry.isFile && gitTrackedFiles.includes(entry.path)) {
files.push(entry.path);
}
}
return files;
}
async function getGitTrackedFiles(dir: DirName): Promise<FileName[]> {
try {
const process = Deno.run({
cmd: ["git", "ls-files", "-co", "--exclude-standard"],
cwd: dir,
stdout: "piped",
});
const output = await process.output();
const files = new TextDecoder().decode(output).trim().split("\n");
process.close();
return files.map((file) => path.join(dir, file));
} catch (error) {
console.error("Error running git command:", error);
const files: string[] = [];
for await (const file of walk(dir)) {
files.push(file.path);
}
return files;
}
}
function extractImports(content: string): Array<[FileName, Label]> {
const importRegex =
/import\s+(?:type\s+)?(?:(\w+)(?:\s+as\s+(\w+))?|{([\s\S]*?)}|\*\s+as\s+(\w+))?\s*(?:from\s*)?["']([^"']+)["']/g;
const imports: Array<[string, string]> = [];
let match;
while ((match = importRegex.exec(content)) !== null) {
const [
,
defaultImport,
aliasedImport,
namedImports,
namespaceImport,
importPath,
] = match;
let importItems = "";
if (defaultImport) {
importItems = aliasedImport
? `${defaultImport} as ${aliasedImport}`
: defaultImport;
} else if (namedImports) {
importItems = namedImports
.split(",")
.map((item) => item.trim())
.join("\\n");
} else if (namespaceImport) {
importItems = `* as ${namespaceImport}`;
}
imports.push([importItems, importPath]);
}
return imports;
}
function normalizeImportPath(
basePath: DirName,
importPath: FileName
): FileName {
if (importPath.startsWith(".")) {
return path.normalize(path.join(path.dirname(basePath), importPath));
}
return importPath;
}
function escapeDoubleQuotes(str: FileName): FileName {
return str.replace(/"/g, '\\"');
}
function getDirectoryPath(filePath: FileName): DirName {
return path.dirname(filePath);
}
function createSubgraphName(dirPath: DirName): string {
return `cluster_${dirPath.replace(/[^\w]/g, "_")}`;
}
/** for given directory (or current directory), creates import dependency graph in DOT notation and dumps to stdio */
export async function toDot(rootDir: DirName = ".") {
const files = await findTsJsFiles(rootDir);
const dirToFiles = groupFilesByDirectory(files);
const edges = await createEdges(files);
const heights = calculateHeights(edges, files);
console.log("strict digraph TypeScriptImports {");
console.log(" node [shape=box, fontsize=16];");
console.log(" edge [fontsize=12];");
console.log(' rankdir="LR"; nodesep="0.5"; ranksep="2"; labelloc="b";');
outputSubgraphs(dirToFiles, heights);
// output each edge
edges.forEach(({ sourceNode, targetNode, label }) =>
console.log(` "${targetNode}" -> "${sourceNode}" ${label};`)
);
console.log("}");
}
function calculateHeights(edges: Edge[], files: FileName[]): Heights {
const sourceCounts: Record<FileName, number> = {};
const targetCounts: Record<FileName, number> = {};
const heights: Heights = {};
// make initial values zero
files.forEach((file) => {
sourceCounts[file] = 0;
targetCounts[file] = 0;
});
for (const { sourceNode, targetNode } of edges) {
sourceCounts[sourceNode]++;
targetCounts[targetNode]++;
}
files.forEach((file) => {
const height =
Math.max(sourceCounts[file], targetCounts[file], 1) * 0.5;
heights[file] = height;
});
return heights;
}
function outputSubgraphs(dirToFiles: DirToFiles, heights: Heights) {
// Create subgraphs and edges
for (const [dirPath, dirFiles] of Object.entries(dirToFiles)) {
const subgraphName = createSubgraphName(dirPath);
console.log(` subgraph ${subgraphName} {`);
console.log(` label = "${escapeDoubleQuotes(dirPath)}";`);
console.log(` color = "blue"; fontcolor="blue"; fontsize=24;`);
for (const file of dirFiles) {
// strip file from directory and extension
const fileName = file.split("/").pop()?.split(".").at(0) as string;
// put files into dir subgraphs
console.log(
` "${escapeDoubleQuotes(
file
)}"[label="${fileName}", height=${
heights[file]
}, href="${file}"];`
);
}
console.log(" }");
}
}
function groupFilesByDirectory(files: FileName[]): DirToFiles {
const dirToFiles: DirToFiles = {};
// Group files by directory
for (const file of files) {
const dirPath = getDirectoryPath(file);
if (!dirToFiles[dirPath]) {
dirToFiles[dirPath] = [];
}
dirToFiles[dirPath].push(file);
}
return dirToFiles;
}
async function createEdges(files: string[]): Promise<Edge[]> {
const edges: Edge[] = [];
// create edges for each file
for (const file of files) {
const content = await Deno.readTextFile(file);
const imports = extractImports(content)
// Normalize path
.map(([importItem, importPath]) => {
const normalizedImportPath = normalizeImportPath(
file,
importPath
);
return [importItem, normalizedImportPath];
})
// filter non internal modules (like node_modules)
.filter(([_, importPath]) => files.includes(importPath));
// create edges for this file
imports.map(([importItems, importPath]) => {
const edge = createEdge(file, importItems, importPath);
edges.push(edge);
});
}
return edges;
}
function createEdge(file: string, importItems: string, importPath: string) {
const sourceNode = escapeDoubleQuotes(file);
const targetNode = escapeDoubleQuotes(importPath);
const label = importItems
? `[label="${escapeDoubleQuotes(importItems)}"]`
: "";
const edge: Edge = { sourceNode, targetNode, label };
return edge;
}
if (import.meta.main) {
const rootDir = Deno.args[0] || ".";
toDot(rootDir);
}