-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·72 lines (57 loc) · 1.36 KB
/
index.js
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
#!/usr/bin/env node
import readline from "node:readline";
import { exit, argv } from "node:process";
const fileToTrace = argv[2];
const files = new Map();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
});
let currentFile = null;
rl.on("line", (line) => {
if (line.startsWith(" ")) {
const match = line.match(/from file '(.+)'/);
const fromFile = match?.[1];
if (currentFile && fromFile) {
files.get(currentFile).push(fromFile);
}
} else {
currentFile = line.trim();
if (currentFile) {
files.set(currentFile, []);
}
}
});
rl.once("close", () => {
if (fileToTrace) {
if (!files.has(fileToTrace)) {
console.error(
`No such file as '${fileToTrace}' on the loaded files list!`
);
exit(1);
}
traceFile(fileToTrace);
} else {
for (const file of files.keys()) {
traceFile(file);
}
}
exit(0);
});
function traceFile(file, trace = [file]) {
const loadedBy = files.get(file) ?? [];
if (!loadedBy.length) {
printFileTrace(trace);
}
for (const nextFile of loadedBy) {
if (trace.includes(nextFile)) {
printFileTrace([...trace, `!! circular dep to ${nextFile}`]);
} else {
traceFile(nextFile, [...trace, nextFile]);
}
}
}
function printFileTrace(trace) {
console.log(trace.join(" < "));
}