-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
executable file
·172 lines (151 loc) · 5.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env node
const { spawn } = require("child_process");
const fs = require('fs');
const path = require("path");
const express = require('express');
const puppeteer = require('puppeteer');
const { PDFDocument } = require('pdf-lib');
// Define variables
let nodeProcess = null;
let processExited = true;
let pathsToWatch = [
path.join(process.cwd(), '/models/**/*.js'),
path.join(process.cwd(), '/prisma/schema.prisma')
];
let previousReloadTimer = null;
// Initialize the script
if (process.argv.length === 3) {
init();
}
// Initialization function
function init() {
// Check if /model is present or not
modelDirPath = path.join(process.cwd(), '/models');
if (!fs.existsSync(modelDirPath)) {
console.log("Error: models folder not found, create a models folder to visualize schemas");
process.exit();
}
const app = express();
const port = 3000;
// Serve static files (your visualizations)
app.use(express.static(path.join(__dirname, 'public')));
// Add routes for export functionality
app.get('/export/pdf', async (req, res) => {
try {
const pdf = await generatePDF();
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=schema.pdf');
res.send(pdf);
} catch (err) {
console.error(err);
res.status(500).send('Error generating PDF');
}
});
app.get('/export/png', async (req, res) => {
try {
const png = await generatePNG();
res.setHeader('Content-Type', 'image/png');
res.setHeader('Content-Disposition', 'attachment; filename=schema.png');
res.send(png);
} catch (err) {
console.error(err);
res.status(500).send('Error generating PNG');
}
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
nodeProcess = startProcess();
watchFiles();
process.on('SIGINT', async () => { await exitHandler(); });
process.on('SIGTERM', async () => { await exitHandler(); });
}
// Start the child process
function startProcess() {
let childProcess = null;
// Start the appropriate server based on command-line argument
if (process.argv[2] === 'm') {
childProcess = spawn('node', [path.join(__dirname, '/servers/mongo.js')], {
stdio: [process.stdin, process.stdout, process.stderr]
});
} else if (process.argv[2] === 's') {
childProcess = spawn('node', [path.join(__dirname, '/servers/sqlz.js')], {
stdio: [process.stdin, process.stdout, process.stderr]
});
} else if (process.argv[2] === 'p') {
childProcess = spawn('node', [path.join(__dirname, '/servers/prisma.js')], {
stdio: [process.stdin, process.stdout, process.stderr]
});
} else if (process.argv[2] === 't') {
childProcess = spawn('node', [path.join(__dirname, '/servers/torm.js')], {
stdio: [process.stdin, process.stdout, process.stderr]
});
}
// Setup event handlers for child process
processExited = false;
childProcess.on('close', () => {
processExited = true;
console.log('server restarting');
});
childProcess.on('error', () => {
processExited = true;
console.log('Error occurred');
});
return childProcess;
}
// Watch files for changes
function watchFiles() {
// Watch each directory in pathsToWatch
pathsToWatch.forEach(dir => {
fs.watchFile(dir, { interval: 1000 }, (curr, prev) => {
// Check if file content has changed
if (curr.mtime !== prev.mtime) {
console.log("files modified...");
clearTimeout(previousReloadTimer);
previousReloadTimer = setTimeout(async () => {
await reload();
}, 1000);
}
});
});
}
// Reload the server
async function reload() {
await stopProcess();
nodeProcess = startProcess();
}
// Stop the child process
async function stopProcess() {
return new Promise((resolve, reject) => {
nodeProcess.kill();
const key = setInterval(() => {
if (processExited) {
clearInterval(key);
resolve(true);
}
}, 500);
});
}
// Handle exit signals
async function exitHandler() {
await stopProcess();
process.exit();
}
// Generate PDF function
async function generatePDF() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(`http://localhost:${3000}`); // URL of your visualization
const pdf = await page.pdf({ format: 'A4' });
await browser.close();
return pdf;
}
// Generate PNG function
async function generatePNG() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(`http://localhost:${3000}`); // URL of your visualization
const screenshot = await page.screenshot({ fullPage: true });
await browser.close();
return screenshot;
}