fixed an error where app wouldnt launch
This commit is contained in:
@@ -1,12 +1,64 @@
|
||||
const { app, BrowserWindow, ipcMain } = require('electron');
|
||||
const { fork } = require('child_process');
|
||||
const path = require('path');
|
||||
const log = require('electron-log');
|
||||
|
||||
log.transports.file.level = 'info';
|
||||
log.format = '[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}] {text}';
|
||||
|
||||
let win;
|
||||
let backend;
|
||||
const net = require('net');
|
||||
|
||||
function waitForServer(port, host = '127.0.0.1', timeout = 10000) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const start = Date.now();
|
||||
|
||||
const check = () => {
|
||||
const socket = new net.Socket();
|
||||
|
||||
socket
|
||||
.once('connect', () => {
|
||||
socket.destroy();
|
||||
resolve();
|
||||
})
|
||||
.once('error', () => {
|
||||
socket.destroy();
|
||||
if (Date.now() - start > timeout) {
|
||||
reject(new Error('Backend timeout'));
|
||||
} else {
|
||||
setTimeout(check, 200);
|
||||
}
|
||||
})
|
||||
.connect(port, host);
|
||||
};
|
||||
|
||||
check();
|
||||
});
|
||||
}
|
||||
|
||||
function startBackend() {
|
||||
backend = fork(path.join(__dirname, 'server.js'));
|
||||
backend = fork(path.join(__dirname, 'server.js'), [], {
|
||||
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
|
||||
env: {
|
||||
...process.env,
|
||||
IS_PACKAGED: app.isPackaged ? 'true' : 'false'
|
||||
}
|
||||
});
|
||||
|
||||
log.info('Starting backend process...');
|
||||
|
||||
backend.stdout.on('data', (data) => {
|
||||
log.info(`[Backend]: ${data.toString().trim()}`);
|
||||
});
|
||||
|
||||
backend.stderr.on('data', (data) => {
|
||||
log.error(`[Backend ERROR]: ${data.toString().trim()}`);
|
||||
});
|
||||
|
||||
backend.on('exit', (code) => {
|
||||
log.warn(`Backend process exited with code: ${code}`);
|
||||
});
|
||||
}
|
||||
|
||||
function createWindow() {
|
||||
@@ -23,19 +75,47 @@ function createWindow() {
|
||||
});
|
||||
|
||||
win.setMenu(null);
|
||||
|
||||
win.loadURL('http://localhost:54322');
|
||||
|
||||
win.on('closed', () => {
|
||||
win = null;
|
||||
});
|
||||
}
|
||||
|
||||
ipcMain.on("win:minimize", () => win.minimize());
|
||||
ipcMain.on("win:maximize", () => win.maximize());
|
||||
ipcMain.on("win:maximize", () => {
|
||||
if (win.isMaximized()) {
|
||||
win.unmaximize();
|
||||
} else {
|
||||
win.maximize();
|
||||
}
|
||||
});
|
||||
ipcMain.on("win:close", () => win.close());
|
||||
|
||||
app.whenReady().then(() => {
|
||||
process.on('uncaughtException', (err) => {
|
||||
log.error('Critical unhandled error in Main:', err);
|
||||
});
|
||||
|
||||
app.whenReady().then(async () => {
|
||||
log.info('--- Application Started ---');
|
||||
console.log("Logs location:", log.transports.file.getFile().path);
|
||||
startBackend();
|
||||
await waitForServer(54322);
|
||||
createWindow();
|
||||
|
||||
app.on('activate', () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) createWindow();
|
||||
});
|
||||
});
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (backend) backend.kill();
|
||||
app.quit();
|
||||
});
|
||||
log.info('Closing all windows...');
|
||||
if (backend) {
|
||||
backend.kill();
|
||||
log.info('Backend process terminated.');
|
||||
}
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit();
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user