Repository navigation
Expand file tree
/
Copy pathquantum-launch.js
More file actions
345 lines (305 loc) · 11.4 KB
/
Copy pathquantum-launch.js
File metadata and controls
345 lines (305 loc) · 11.4 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env node
/**
* ============================================================
* QuantumIDE Unified Single-Command Full-Stack Launcher
* Starts, monitors, and keeps both Backend & Frontend services alive.
* ============================================================
*/
const { spawn, execSync } = require('child_process');
const path = require('path');
const fs = require('fs');
const net = require('net');
// Terminal color helpers
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
dim: '\x1b[2m',
cyan: '\x1b[36m',
blue: '\x1b[34m',
green: '\x1b[32m',
yellow: '\x1b[33m',
red: '\x1b[31m',
magenta: '\x1b[35m'
};
const ROOT_DIR = __dirname;
const BACKEND_DIR = path.join(ROOT_DIR, 'backend');
const FRONTEND_DIR = path.join(ROOT_DIR, 'frontend');
const BACKEND_PORT = 5000;
const FRONTEND_PORT = 3000;
console.log(`\n${colors.bright}${colors.cyan}============================================================${colors.reset}`);
console.log(`${colors.bright}${colors.cyan} ⚡ QuantumIDE Single-Command Full-Stack Launcher ⚡ ${colors.reset}`);
console.log(`${colors.cyan}============================================================${colors.reset}\n`);
// 1. Verify directories
if (!fs.existsSync(BACKEND_DIR)) {
console.error(`${colors.red}❌ Error: Backend directory not found at: ${BACKEND_DIR}${colors.reset}`);
process.exit(1);
}
if (!fs.existsSync(FRONTEND_DIR)) {
console.error(`${colors.red}❌ Error: Frontend directory not found at: ${FRONTEND_DIR}${colors.reset}`);
process.exit(1);
}
// 2. Kill any stale/orphaned processes on target ports to ensure a clean start
function killProcessesOnPort(port) {
try {
if (process.platform === 'win32') {
const out = execSync(`netstat -ano | findstr ":${port}"`, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
const lines = out.split(/\r?\n/);
const pids = new Set();
for (const line of lines) {
const parts = line.trim().split(/\s+/);
if (parts.length >= 5 && parts[1].includes(`:${port}`) && (parts[3] === 'LISTENING' || parts[3] === 'ESTABLISHED')) {
const pid = parseInt(parts[4], 10);
if (pid && pid !== process.pid) {
pids.add(pid);
}
}
}
for (const pid of pids) {
try {
execSync(`taskkill /pid ${pid} /T /F 2>nul`, { stdio: 'ignore' });
} catch (_) {}
}
} else {
execSync(`lsof -ti:${port} | xargs kill -9 2>/dev/null`, { stdio: 'ignore' });
}
} catch (_) {}
}
// Clean up stale ports before launching so there's never an EADDRINUSE conflict
killProcessesOnPort(BACKEND_PORT);
killProcessesOnPort(FRONTEND_PORT);
// 3. Port check utility
function checkPortInUse(port) {
return new Promise((resolve) => {
const socket = new net.Socket();
socket.setTimeout(400);
socket.once('connect', () => {
socket.destroy();
resolve(true);
});
const tryIpv6 = () => {
const socket6 = new net.Socket();
socket6.setTimeout(400);
socket6.once('connect', () => {
socket6.destroy();
resolve(true);
});
socket6.once('error', () => {
socket6.destroy();
resolve(false);
});
socket6.once('timeout', () => {
socket6.destroy();
resolve(false);
});
socket6.connect(port, '::1');
};
socket.once('error', () => {
socket.destroy();
tryIpv6();
});
socket.once('timeout', () => {
socket.destroy();
tryIpv6();
});
socket.connect(port, '127.0.0.1');
});
}
// 4. Port readiness check utility
function waitForPort(port, timeoutMs = 15000) {
const startTime = Date.now();
return new Promise((resolve) => {
const check = () => {
const socket = new net.Socket();
socket.setTimeout(500);
socket.once('connect', () => {
socket.destroy();
resolve(true);
});
socket.once('error', () => {
socket.destroy();
if (Date.now() - startTime >= timeoutMs) {
resolve(false);
} else {
setTimeout(check, 300);
}
});
socket.once('timeout', () => {
socket.destroy();
if (Date.now() - startTime >= timeoutMs) {
resolve(false);
} else {
setTimeout(check, 300);
}
});
socket.connect(port, '127.0.0.1');
};
check();
});
}
// Tracking child processes
let backendProcess = null;
let frontendProcess = null;
let electronProcess = null;
let isShuttingDown = false;
const isDesktopMode = process.argv.includes('--desktop') || process.argv.includes('-d');
function killProcessTree(proc) {
if (!proc || !proc.pid) return;
try {
if (process.platform === 'win32') {
execSync(`taskkill /pid ${proc.pid} /T /F 2>nul`, { stdio: 'ignore' });
} else {
process.kill(-proc.pid, 'SIGTERM');
}
} catch (_) {}
}
function cleanExit() {
if (isShuttingDown) return;
isShuttingDown = true;
console.log(`\n${colors.yellow}🛑 Shutting down QuantumIDE services...${colors.reset}`);
if (electronProcess) killProcessTree(electronProcess);
if (backendProcess) killProcessTree(backendProcess);
if (frontendProcess) killProcessTree(frontendProcess);
killProcessesOnPort(BACKEND_PORT);
killProcessesOnPort(FRONTEND_PORT);
console.log(`${colors.green}✔ All services stopped.${colors.reset}\n`);
process.exit(0);
}
process.on('SIGINT', cleanExit);
process.on('SIGTERM', cleanExit);
// Function to start Backend
function spawnBackend() {
killProcessesOnPort(BACKEND_PORT);
console.log(`${colors.blue}▶ Starting QuantumIDE Backend (Express on port ${BACKEND_PORT})...${colors.reset}`);
backendProcess = spawn('node', ['server.js'], {
cwd: BACKEND_DIR,
env: { ...process.env, PORT: BACKEND_PORT.toString() },
shell: false,
stdio: ['ignore', 'pipe', 'pipe']
});
backendProcess.stdout.on('data', (data) => {
const text = data.toString();
text.split(/\r?\n/).forEach((line) => {
if (line.trim()) {
console.log(`${colors.cyan}[Backend]${colors.reset} ${line}`);
}
});
});
backendProcess.stderr.on('data', (data) => {
const text = data.toString();
text.split(/\r?\n/).forEach((line) => {
if (line.trim()) {
console.error(`${colors.yellow}[Backend Warning]${colors.reset} ${line}`);
}
});
});
backendProcess.on('exit', (code) => {
if (!isShuttingDown) {
console.error(`\n${colors.red}❌ QuantumIDE Backend stopped unexpectedly with exit code: ${code}${colors.reset}`);
console.log(`${colors.yellow}🔄 Restarting Backend in 2 seconds...${colors.reset}`);
setTimeout(() => {
if (!isShuttingDown) spawnBackend();
}, 2000);
}
});
}
// Function to start Frontend
function spawnFrontend() {
killProcessesOnPort(FRONTEND_PORT);
console.log(`${colors.magenta}▶ Starting QuantumIDE Frontend (Vite on port ${FRONTEND_PORT})...${colors.reset}`);
// Use npm start for frontend which runs vite --host 0.0.0.0 --port 3000
frontendProcess = spawn('npm.cmd', ['start'], {
cwd: FRONTEND_DIR,
env: { ...process.env },
shell: true,
stdio: ['ignore', 'pipe', 'pipe']
});
frontendProcess.stdout.on('data', (data) => {
const text = data.toString();
text.split(/\r?\n/).forEach((line) => {
if (line.trim()) {
console.log(`${colors.magenta}[Frontend]${colors.reset} ${line}`);
}
});
});
frontendProcess.stderr.on('data', (data) => {
const text = data.toString();
text.split(/\r?\n/).forEach((line) => {
if (line.trim()) {
console.error(`${colors.yellow}[Frontend Notice]${colors.reset} ${line}`);
}
});
});
frontendProcess.on('exit', (code) => {
if (!isShuttingDown) {
console.error(`\n${colors.red}❌ QuantumIDE Frontend stopped unexpectedly with exit code: ${code}${colors.reset}`);
console.log(`${colors.yellow}🔄 Restarting Frontend in 2 seconds...${colors.reset}`);
setTimeout(() => {
if (!isShuttingDown) spawnFrontend();
}, 2000);
}
});
}
// Main startup routine
async function startQuantum() {
// 1. Start Backend
spawnBackend();
process.stdout.write(`${colors.dim} Waiting for backend port ${BACKEND_PORT}...${colors.reset} `);
const backendReady = await waitForPort(BACKEND_PORT, 12000);
if (backendReady) {
process.stdout.write(`\r${colors.green}✔ QuantumIDE Backend → Running${colors.reset} (http://localhost:${BACKEND_PORT})\n`);
} else {
process.stdout.write(`\r${colors.yellow}⚠ QuantumIDE Backend initialized.${colors.reset}\n`);
}
// 2. Start Frontend
spawnFrontend();
process.stdout.write(`${colors.dim} Waiting for frontend port ${FRONTEND_PORT}...${colors.reset} `);
const frontendReady = await waitForPort(FRONTEND_PORT, 15000);
if (frontendReady) {
process.stdout.write(`\r${colors.green}✔ QuantumIDE Frontend → Running${colors.reset} (http://localhost:${FRONTEND_PORT})\n`);
} else {
process.stdout.write(`\r${colors.yellow}⚠ QuantumIDE Frontend initialized.${colors.reset}\n`);
}
// 3. Final Summary Banner
console.log(`\n${colors.bright}${colors.green}============================================================${colors.reset}`);
console.log(`${colors.bright}${colors.green} 🚀 QuantumIDE is fully active and ready to use! ${colors.reset}`);
console.log(`${colors.green}============================================================${colors.reset}`);
console.log(` ${colors.bright}• Workspace URL:${colors.reset} ${colors.cyan}http://localhost:${FRONTEND_PORT}${colors.reset}`);
console.log(` ${colors.bright}• Auth & History:${colors.reset} ${colors.cyan}http://localhost:${FRONTEND_PORT}/#auth${colors.reset}`);
console.log(` ${colors.bright}• Backend API:${colors.reset} ${colors.blue}http://localhost:${BACKEND_PORT}${colors.reset}`);
console.log(` ${colors.bright}• Status:${colors.reset} ${colors.green}RUNNING${colors.reset} (Monitoring services actively)`);
if (isDesktopMode) {
console.log(` ${colors.bright}• Shell:${colors.reset} ${colors.cyan}Desktop (Electron Window)${colors.reset}`);
}
console.log(` ${colors.dim}• Press Ctrl+C at any time to stop both servers.${colors.reset}\n`);
// Launch Electron if in desktop dev mode
if (isDesktopMode) {
console.log(`${colors.cyan}▶ Opening QuantumIDE Desktop Window...${colors.reset}`);
const electronBin = process.platform === 'win32' ? 'npx.cmd' : 'npx';
electronProcess = spawn(electronBin, ['electron', '.'], {
cwd: ROOT_DIR,
env: { ...process.env, QUANTUM_DEV: '1' },
shell: true,
stdio: 'inherit'
});
electronProcess.on('exit', () => {
console.log(`${colors.yellow}🛑 Desktop window closed. Shutting down development services...${colors.reset}`);
cleanExit();
});
}
// 4. CRITICAL KEEP-ALIVE: Keep the launcher process active indefinitely!
// This ensures the terminal window never exits or closes automatically.
if (process.stdin.isTTY) {
process.stdin.setRawMode(false);
}
process.stdin.resume();
// Periodic heartbeat monitor keeping event loop permanently alive
const monitorTimer = setInterval(() => {
// Keeps process alive and event loop engaged
}, 1000 * 60 * 60);
// Await infinite promise so startQuantum never completes on its own
await new Promise(() => {});
}
startQuantum().catch((err) => {
console.error(`${colors.red}Fatal Launcher Error: ${err.message}${colors.reset}`);
cleanExit();
});