3
3
const { spawn } = require ( "child_process" ) ;
4
4
const fs = require ( 'fs' ) ;
5
5
const path = require ( "path" ) ;
6
+ const express = require ( 'express' ) ;
7
+ const puppeteer = require ( 'puppeteer' ) ;
8
+ const { PDFDocument } = require ( 'pdf-lib' ) ;
6
9
7
10
// Define variables
8
11
let nodeProcess = null ;
@@ -21,17 +24,52 @@ if (process.argv.length === 3) {
21
24
22
25
// Initialization function
23
26
function init ( ) {
24
- //check if /model is present or not
25
- modelDirPath = path . join ( process . cwd ( ) , '/models' )
26
- if ( ! fs . existsSync ( modelDirPath ) ) {
27
- console . log ( "Error : models folder not found, create a models folder to visualize schemas" )
28
- process . exit ( )
27
+ // Check if /model is present or not
28
+ modelDirPath = path . join ( process . cwd ( ) , '/models' ) ;
29
+ if ( ! fs . existsSync ( modelDirPath ) ) {
30
+ console . log ( "Error: models folder not found, create a models folder to visualize schemas" ) ;
31
+ process . exit ( ) ;
29
32
}
30
33
34
+ const app = express ( ) ;
35
+ const port = 3000 ;
36
+
37
+ // Serve static files (your visualizations)
38
+ app . use ( express . static ( path . join ( __dirname , 'public' ) ) ) ;
39
+
40
+ // Add routes for export functionality
41
+ app . get ( '/export/pdf' , async ( req , res ) => {
42
+ try {
43
+ const pdf = await generatePDF ( ) ;
44
+ res . setHeader ( 'Content-Type' , 'application/pdf' ) ;
45
+ res . setHeader ( 'Content-Disposition' , 'attachment; filename=schema.pdf' ) ;
46
+ res . send ( pdf ) ;
47
+ } catch ( err ) {
48
+ console . error ( err ) ;
49
+ res . status ( 500 ) . send ( 'Error generating PDF' ) ;
50
+ }
51
+ } ) ;
52
+
53
+ app . get ( '/export/png' , async ( req , res ) => {
54
+ try {
55
+ const png = await generatePNG ( ) ;
56
+ res . setHeader ( 'Content-Type' , 'image/png' ) ;
57
+ res . setHeader ( 'Content-Disposition' , 'attachment; filename=schema.png' ) ;
58
+ res . send ( png ) ;
59
+ } catch ( err ) {
60
+ console . error ( err ) ;
61
+ res . status ( 500 ) . send ( 'Error generating PNG' ) ;
62
+ }
63
+ } ) ;
64
+
65
+ app . listen ( port , ( ) => {
66
+ console . log ( `Server is running at http://localhost:${ port } ` ) ;
67
+ } ) ;
68
+
31
69
nodeProcess = startProcess ( ) ;
32
70
watchFiles ( ) ;
33
- process . on ( 'SIGINT' , async ( ) => { await exitHandler ( ) } ) ;
34
- process . on ( 'SIGTERM' , async ( ) => { await exitHandler ( ) } ) ;
71
+ process . on ( 'SIGINT' , async ( ) => { await exitHandler ( ) ; } ) ;
72
+ process . on ( 'SIGTERM' , async ( ) => { await exitHandler ( ) ; } ) ;
35
73
}
36
74
37
75
// Start the child process
@@ -61,7 +99,7 @@ function startProcess() {
61
99
processExited = false ;
62
100
childProcess . on ( 'close' , ( ) => {
63
101
processExited = true ;
64
- console . log ( 'server restarting' ) ;
102
+ console . log ( 'Server restarting' ) ;
65
103
} ) ;
66
104
childProcess . on ( 'error' , ( ) => {
67
105
processExited = true ;
@@ -78,7 +116,7 @@ function watchFiles() {
78
116
fs . watchFile ( dir , { interval : 1000 } , ( curr , prev ) => {
79
117
// Check if file content has changed
80
118
if ( curr . mtime !== prev . mtime ) {
81
- console . log ( "files modified..." ) ;
119
+ console . log ( "Files modified..." ) ;
82
120
clearTimeout ( previousReloadTimer ) ;
83
121
previousReloadTimer = setTimeout ( async ( ) => {
84
122
await reload ( ) ;
@@ -112,3 +150,23 @@ async function exitHandler() {
112
150
await stopProcess ( ) ;
113
151
process . exit ( ) ;
114
152
}
153
+
154
+ // Generate PDF function
155
+ async function generatePDF ( ) {
156
+ const browser = await puppeteer . launch ( ) ;
157
+ const page = await browser . newPage ( ) ;
158
+ await page . goto ( `http://localhost:${ 3000 } ` ) ; // URL of your visualization
159
+ const pdf = await page . pdf ( { format : 'A4' } ) ;
160
+ await browser . close ( ) ;
161
+ return pdf ;
162
+ }
163
+
164
+ // Generate PNG function
165
+ async function generatePNG ( ) {
166
+ const browser = await puppeteer . launch ( ) ;
167
+ const page = await browser . newPage ( ) ;
168
+ await page . goto ( `http://localhost:${ 3000 } ` ) ; // URL of your visualization
169
+ const screenshot = await page . screenshot ( { fullPage : true } ) ;
170
+ await browser . close ( ) ;
171
+ return screenshot ;
172
+ }
0 commit comments