-
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathmain.cjs
More file actions
124 lines (107 loc) · 3.13 KB
/
Copy pathmain.cjs
File metadata and controls
124 lines (107 loc) · 3.13 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
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) {
return new Promise((resolve) => {
const check = () => {
http
.get(url, () => resolve())
.on('error', () => setTimeout(check, 500));
};
check();
});
}
// Start Nitro server (development or production)
function startServer() {
return new Promise((resolve) => {
// Try development path first (after npm run build)
const devPath = path.join(process.cwd(), '.output', 'server', 'index.mjs');
// Fallback to production path (after npm run dist)
const prodPath = path.join(
process.resourcesPath,
'app.asar.unpacked',
'.output',
'server',
'index.mjs'
);
// Determine which path to use
let serverPath;
let isDevelopment = false;
if (fs.existsSync(devPath)) {
serverPath = devPath;
isDevelopment = true;
console.log("Starting server in DEVELOPMENT mode from:", serverPath);
} else if (fs.existsSync(prodPath)) {
serverPath = prodPath;
console.log("Starting server in PRODUCTION mode from:", serverPath);
} else {
console.error("ERROR: Server build not found!");
console.error("Tried development path:", devPath);
console.error("Tried production path:", prodPath);
console.error("Please run 'npm run build' first.");
app.quit();
return;
}
serverProcess = spawn('node', [serverPath], {
stdio: isDevelopment ? 'inherit' : 'ignore', // show logs in dev
windowsHide: !isDevelopment, // show terminal in dev
env: {
...process.env,
HOST: serverHost,
PORT: serverPort.toString(),
},
});
waitForServer(`http://localhost:${serverPort}`).then(resolve);
});
}
// 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 () => {
await startServer();
createWindow();
});
// Cleanup
app.on('window-all-closed', () => {
if (serverProcess) serverProcess.kill();
if (process.platform !== 'darwin') app.quit();
});