@@ -3,17 +3,15 @@ import { existsSync } from 'node:fs';
3
3
import { resolve , basename } from 'path' ;
4
4
import puppeteer from 'puppeteer' ;
5
5
import { templates } from '../src/templates.mjs' ;
6
+ import express from 'express' ;
6
7
7
8
const args = process . argv . slice ( 2 ) ;
9
+ const port = process . env . PORT ?? 3030 ;
8
10
const dir = process . cwd ( ) ;
9
11
const langs = ( args [ 0 ] || 'en,ru' ) . split ( ',' ) ;
10
12
const target = args [ 1 ] ;
11
13
12
- async function buildGraphs ( langs , target , srcDir , dstDir , tmpDir ) {
13
- if ( ! existsSync ( tmpDir ) ) {
14
- await mkdir ( tmpDir ) ;
15
- }
16
-
14
+ async function buildGraphs ( langs , target , srcDir , dstDir , urlBuilder ) {
17
15
for ( const lang of langs ) {
18
16
const graphDir = resolve ( srcDir , lang , 'graphs' ) ;
19
17
const targets = target
@@ -25,7 +23,7 @@ async function buildGraphs(langs, target, srcDir, dstDir, tmpDir) {
25
23
) ;
26
24
27
25
for ( const t of targets ) {
28
- await buildGraph ( lang , t , dstDir , tmpDir ) ;
26
+ await buildGraph ( lang , t , dstDir , urlBuilder ( t ) ) ;
29
27
}
30
28
}
31
29
}
@@ -41,15 +39,9 @@ async function getGraphList(srcDir) {
41
39
return result ;
42
40
}
43
41
44
- async function buildGraph ( lang , target , dstDir , tmpDir ) {
42
+ async function buildGraph ( lang , target , dstDir , url ) {
45
43
const targetName = basename ( target ) ;
46
- console . log (
47
- `Processing ${ target } , basename: ${ targetName } dst: ${ dstDir } , tmp: ${ tmpDir } `
48
- ) ;
49
- const tmpFileName = resolve ( tmpDir , `${ targetName } .${ lang } .html` ) ;
50
- const graph = await readFile ( target , 'utf-8' ) ;
51
- await writeFile ( tmpFileName , templates . graphHtmlTemplate ( graph ) ) ;
52
- console . log ( `Tmp file ${ tmpFileName } written` ) ;
44
+ console . log ( `Processing ${ target } , basename: ${ targetName } dst: ${ dstDir } ` ) ;
53
45
const browser = await puppeteer . launch ( {
54
46
headless : 'new' ,
55
47
product : 'chrome' ,
@@ -64,28 +56,65 @@ async function buildGraph(lang, target, dstDir, tmpDir) {
64
56
`${ targetName . replace ( '.mermaid' , '' ) } .${ lang } .png`
65
57
) ;
66
58
const page = await browser . newPage ( ) ;
67
- await page . goto ( tmpFileName , {
59
+ await page . goto ( url , {
68
60
waitUntil : 'networkidle0'
69
61
} ) ;
62
+ console . log ( `URL ${ url } loaded` ) ;
70
63
const $canvas = await page . $ ( 'svg' ) ;
71
64
const bounds = await $canvas . boundingBox ( ) ;
72
- const body = await page . $ ( 'body' ) ;
73
- await body . screenshot ( {
65
+ // const body = await page.$('body');
66
+ await $canvas . screenshot ( {
74
67
path : outFile ,
75
68
type : 'png' ,
76
69
captureBeyondViewport : true ,
77
70
clip : bounds
78
71
} ) ;
79
72
await browser . close ( ) ;
73
+ console . log ( `File ${ outFile } saved` ) ;
74
+ }
75
+
76
+ async function main ( ) {
77
+ const app = express ( ) ;
78
+ app . use ( '/src' , express . static ( 'src' ) )
79
+ . use ( '/docs' , express . static ( 'docs' ) )
80
+ . get ( '/graph' , async ( req , res , next ) => {
81
+ try {
82
+ const file = req . query . file ;
83
+ console . log ( `Reading file "${ file } "` ) ;
84
+ const html = templates . graphHtmlTemplate (
85
+ ( await readFile ( file ) ) . toString ( 'utf-8' )
86
+ ) ;
87
+ res . status ( 200 ) ;
88
+ res . end ( html ) ;
89
+ } catch ( e ) {
90
+ next ( e ) ;
91
+ }
92
+ } )
93
+ . use ( ( req , res , error , next ) => {
94
+ res . status ( 500 ) ;
95
+ res . end ( error . toString ( ) ) ;
96
+ throw error ;
97
+ } ) ;
98
+
99
+ app . listen ( port , ( ) => {
100
+ console . log ( `Graph server started at localhost:${ port } ` ) ;
101
+ } ) ;
102
+
103
+ await buildGraphs (
104
+ langs ,
105
+ target ,
106
+ resolve ( dir , 'src' ) ,
107
+ resolve ( dir , 'src' , 'img' , 'graphs' ) ,
108
+ ( file ) =>
109
+ `http://localhost:${ port } /graph?file=${ encodeURIComponent ( file ) } `
110
+ ) ;
111
+
112
+ console . log ( 'All graphs built successfully' ) ;
113
+
114
+ await new Promise ( ( r ) => setTimeout ( ( ) => r ( ) , 60 * 60 * 1000 ) ) ;
80
115
}
81
116
82
- buildGraphs (
83
- langs ,
84
- target ,
85
- resolve ( dir , 'src' ) ,
86
- resolve ( dir , 'src' , 'img' , 'graphs' ) ,
87
- resolve ( dir , '.tmp' )
88
- )
117
+ main ( )
89
118
. catch ( ( e ) => {
90
119
console . error ( e ) ;
91
120
} )
0 commit comments