fork of hey-api/openapi-ts because I need some additional things
at main 60 lines 1.7 kB view raw
1import fs from 'node:fs'; 2import path from 'node:path'; 3import { fileURLToPath } from 'node:url'; 4 5const __filename = fileURLToPath(import.meta.url); 6const __dirname = path.dirname(__filename); 7 8const nodes = JSON.parse( 9 fs.readFileSync(path.resolve(__dirname, 'graph.json'), 'utf-8'), 10); 11 12// --- Filter nodes for readability --- 13const threshold = 10; // high-fanout threshold 14const filteredSet = new Set(); 15 16// Include nodes with children > threshold and their immediate children 17for (const n of nodes) { 18 const childCount = n.childrenPointers?.length ?? 0; 19 if (childCount > threshold) { 20 filteredSet.add(n.pointer); 21 for (const child of n.childrenPointers || []) { 22 filteredSet.add(child); 23 } 24 } 25} 26 27// Filtered nodes list 28const filteredNodes = nodes.filter((n) => filteredSet.has(n.pointer)); 29 30// Start the .dot file 31let dot = 'digraph OpenAPIGraph {\nrankdir=LR;\nnode [style=filled];\n'; 32 33// Add nodes with color based on fanout 34for (const n of filteredNodes) { 35 const childCount = n.childrenPointers?.length ?? 0; 36 const color = 37 childCount > 50 ? 'red' : childCount > 20 ? 'orange' : 'lightgray'; 38 dot += `"${n.pointer}" [label="${n.pointer}\\n${childCount} children", fillcolor=${color}];\n`; 39} 40 41// Add edges: node -> its children 42for (const n of filteredNodes) { 43 for (const child of n.childrenPointers || []) { 44 if (filteredSet.has(child)) { 45 dot += `"${n.pointer}" -> "${child}";\n`; 46 } 47 } 48} 49 50dot += '}\n'; 51 52// Write to a file 53fs.writeFileSync(path.resolve(__dirname, 'graph.dot'), dot); 54console.log('graph.dot created!'); 55 56// Instructions: 57// Render with Graphviz: 58// dot -Tpng graph.dot -o graph.png 59// or 60// dot -Tsvg graph.dot -o graph.svg