-
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathmain.cjs
More file actions
130 lines (111 loc) · 2.89 KB
/
Copy pathmain.cjs
File metadata and controls
130 lines (111 loc) · 2.89 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
const { app, BrowserWindow } = require('electron');
const path = require('path');
const { spawn } = require('child_process');
const http = require('http');
const fs = require('fs');
let mainWindow;
let serverProcess;
let serverHost = '0.0.0.0';
let serverPort = 3000;
try {
const configPath = './src/server-config.json';
if (fs.existsSync(configPath)) {
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
if (config.host) serverHost = config.host;
if (config.frontendPort) serverPort = config.frontendPort;
}
} catch (e) {
console.warn('Failed to load server config:', e);
}
// Prevent multiple instances
const gotLock = app.requestSingleInstanceLock();
if (!gotLock) {
app.quit();
process.exit(0);
}
// Wait until server is ready
function waitForServer(url, timeout = 15000) {
return new Promise((resolve, reject) => {
const start = Date.now();
const check = () => {
const req = http.get(url, (res) => {
res.resume();
resolve();
});
req.setTimeout(2000, () => {
req.destroy();
});
req.on('error', () => {
if (Date.now() - start > timeout) {
reject(new Error('Server did not start within timeout'));
} else {
setTimeout(check, 500);
}
});
};
check();
});
}
// Start Nitro server (production)
function startServer() {
return new Promise((resolve, reject) => {
const serverPath = path.join(
process.resourcesPath,
'app.asar.unpacked',
'.output',
'server',
'index.mjs'
);
console.log("Starting server from:", serverPath);
serverProcess = spawn('node', [serverPath], {
stdio: 'ignore', // no terminal
windowsHide: true, // hide CMD
env: {
...process.env,
HOST: serverHost,
PORT: serverPort.toString(),
},
});
waitForServer(`http://localhost:${serverPort}`)
.then(resolve)
.catch((err) => {
console.error('Server failed to start:', err);
if (serverProcess) serverProcess.kill();
reject(err)
});
});
}
// Create window
function createWindow() {
if (mainWindow) return;
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
show: false,
});
mainWindow.loadURL(`http://localhost:${serverPort}`);
// Show when ready
mainWindow.once('ready-to-show', () => {
mainWindow.show();
});
// Debug only if needed
mainWindow.webContents.on('did-fail-load', (e, code, desc) => {
console.log("LOAD FAILED:", code, desc);
});
}
// App start
app.whenReady().then(async () => {
try {
await startServer();
createWindow();
} catch (e) {
console.error('Startup failed:', e);
if (serverProcess) serverProcess.kill();
app.quit();
}
});
// Cleanup
app.on('window-all-closed', () => {
if (serverProcess) serverProcess.kill();
if (process.platform !== 'darwin') app.quit();
});