1+ "use strict" ;
2+
13import { exec } from "node:child_process" ;
24import * as path from "node:path" ;
35
@@ -73,10 +75,9 @@ const makeResolver = (filenameToNodeId) => {
7375 } ;
7476} ;
7577
76- const main = async ( ) => {
77- const markdownFilenames = await findMarkdownFiles ( ) ;
78- const links = await scanForLinks ( markdownFilenames ) ;
79- const trimmed = localMarkdownToMarkdownLinks ( links ) ;
78+ const buildGraph = ( markdownFilenames , trimmed ) => {
79+ const nodeLines = [ ] ;
80+ const edgeLines = [ ] ;
8081
8182 const filenameToNodeId = new Map (
8283 markdownFilenames . map ( ( filename , index ) => [ filename , `id${ index } ` ] ) ,
@@ -86,12 +87,8 @@ const main = async () => {
8687 const targetResolver = makeResolver ( filenameToNodeId ) ;
8788 const seenEdges = new Set ( ) ;
8889
89- console . log ( "```mermaid" ) ;
90- // console.log("graph TD;"); // top-down
91- console . log ( "graph LR;" ) ; // left-right
92-
9390 for ( const [ filename , nodeId ] of filenameToNodeId . entries ( ) ) {
94- console . log ( ` ${ nodeId } [${ JSON . stringify ( filename ) } ]` ) ;
91+ nodeLines . push ( ` ${ nodeId } [${ JSON . stringify ( filename ) } ]` ) ;
9592 }
9693
9794 for ( const links of trimmed ) {
@@ -102,20 +99,32 @@ const main = async () => {
10299
103100 if ( r . isNew ) {
104101 const label = `?? ${ r . resolved } ??` ;
105- console . log ( ` ${ r . id } [${ JSON . stringify ( label ) } ]` ) ;
102+ nodeLines . push ( ` ${ r . id } [${ JSON . stringify ( label ) } ]` ) ;
106103 }
107104
108105 const edgeKey = `${ fromNodeId } to ${ r . id } ` ;
109106 if ( ! seenEdges . has ( edgeKey ) ) {
110- console . log ( ` ${ fromNodeId } -->${ r . id } ` ) ;
107+ edgeLines . push ( ` ${ fromNodeId } -->${ r . id } ` ) ;
111108 seenEdges . add ( edgeKey ) ;
112109 }
113110 }
114111 }
115112
116- console . log ( "```" ) ;
113+ // Stable output
114+ return [ ...nodeLines . toSorted ( ) , ...edgeLines . toSorted ( ) ] ;
115+ } ;
116+
117+ const main = async ( ) => {
118+ const markdownFilenames = await findMarkdownFiles ( ) ;
119+ const links = await scanForLinks ( markdownFilenames ) ;
120+ const trimmed = localMarkdownToMarkdownLinks ( links ) ;
121+
122+ const graphLines = buildGraph ( markdownFilenames , trimmed ) ;
123+ const graphContent =
124+ [ "```mermaid" , "graph LR;" , ...graphLines , "```" ] . join ( "\n" ) + "\n" ;
117125
118- // console.dir({ trimmed, e: [...m.entries()] }, { depth: 5 });
126+ await writeFile ( "diagram.md" , graphContent , "utf-8" ) ;
127+ console . log ( "Wrote diagram.md" ) ;
119128} ;
120129
121130main ( ) . catch ( ( error ) => {
0 commit comments