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