Created
August 25, 2021 07:14
-
-
Save ospfranco/d599a68f1a2fe1f39a457162238fec78 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const graph = {}; | |
const file = fs.readFileSync("output2").toString(); | |
file | |
.split("\n") | |
.map(l => l.trim()) | |
.map(l => l.split(" -> ")) | |
.forEach(([start, end]) => { | |
if (start.includes("node_modules") || end.includes("node_modules")) { | |
return; | |
} | |
if (graph[start]) { | |
if (!graph[start].includes(end)) { | |
graph[start].push(end); | |
} | |
} else { | |
graph[start] = [end]; | |
} | |
}); | |
// console.warn("DIRECTED GRAPH", Object.keys(graph).length); | |
const visited = {}; | |
const toExpand = ['"src/Sagas/AppSagas.js"']; | |
const finalGraph = []; | |
while (toExpand.length) { | |
const head = toExpand.pop(); | |
if (visited[head]) { | |
// console.warn("cycle detected", head); | |
} else { | |
visited[head] = true; | |
const leafs = graph[head]; | |
leafs?.forEach(leaf => { | |
// console.warn("checking head/leaf", head, leaf); | |
finalGraph.push(`${head} -> ${leaf}`); | |
toExpand.push(leaf); | |
}); | |
} | |
} | |
console.warn("visited length", Object.entries(visited).length); | |
let str = `digraph {\n`; | |
finalGraph.forEach(line => { | |
str += ` ${line}\n`; | |
}); | |
str += `}`; | |
fs.writeFileSync("./customOutput", str); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment