v1.6.0 #221
@@ -1,68 +1,26 @@
|
||||
const { app, BrowserWindow, Menu, shell, ipcMain } = require('electron');
|
||||
const { app, BrowserWindow, shell, ipcMain } = require('electron');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
// Global variables
|
||||
let mainWindow = null;
|
||||
let backendPort = null;
|
||||
|
||||
// Development environment detection
|
||||
const isDev = process.env.NODE_ENV === 'development' || !app.isPackaged;
|
||||
|
||||
const BACKEND_CONFIG = {
|
||||
port: 18080
|
||||
};
|
||||
|
||||
// Prevent multiple instances
|
||||
const gotTheLock = app.requestSingleInstanceLock();
|
||||
if (!gotTheLock) {
|
||||
console.log('Another instance is already running, quitting...');
|
||||
app.quit();
|
||||
process.exit(0);
|
||||
} else {
|
||||
app.on('second-instance', () => {
|
||||
app.on('second-instance', (event, commandLine, workingDirectory) => {
|
||||
console.log('Second instance detected, focusing existing window...');
|
||||
if (mainWindow) {
|
||||
if (mainWindow.isMinimized()) mainWindow.restore();
|
||||
mainWindow.focus();
|
||||
mainWindow.show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Simple backend management
|
||||
async function startBackend() {
|
||||
try {
|
||||
console.log('Starting backend...');
|
||||
|
||||
// Set up environment
|
||||
process.env.NODE_ENV = 'production';
|
||||
process.env.PORT = BACKEND_CONFIG.port.toString();
|
||||
process.env.DATA_PATH = app.getPath('userData');
|
||||
process.env.DB_PATH = path.join(app.getPath('userData'), 'database.db');
|
||||
process.env.VERSION = app.getVersion();
|
||||
|
||||
// Get backend path
|
||||
const backendPath = isDev
|
||||
? path.join(__dirname, '..', 'dist', 'backend', 'starter.js')
|
||||
: path.join(process.resourcesPath, 'dist', 'backend', 'starter.js');
|
||||
|
||||
console.log('Loading backend from:', backendPath);
|
||||
|
||||
if (!fs.existsSync(backendPath)) {
|
||||
console.error('Backend file not found at:', backendPath);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Load and start the backend
|
||||
const { startServer } = await import(backendPath);
|
||||
backendPort = await startServer();
|
||||
console.log('Backend started successfully on port:', backendPort);
|
||||
return backendPort;
|
||||
|
||||
} catch (error) {
|
||||
console.error('Failed to start backend:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Create main window
|
||||
function createWindow() {
|
||||
mainWindow = new BrowserWindow({
|
||||
width: 1200,
|
||||
@@ -81,29 +39,24 @@ function createWindow() {
|
||||
show: false
|
||||
});
|
||||
|
||||
// Remove menu bar on Windows/Linux
|
||||
if (process.platform !== 'darwin') {
|
||||
mainWindow.setMenuBarVisibility(false);
|
||||
}
|
||||
|
||||
// Load application
|
||||
if (isDev) {
|
||||
mainWindow.loadURL('http://localhost:5173');
|
||||
mainWindow.webContents.openDevTools();
|
||||
} else {
|
||||
// Load from dist folder
|
||||
const indexPath = path.join(__dirname, '..', 'dist', 'index.html');
|
||||
console.log('Loading frontend from:', indexPath);
|
||||
mainWindow.loadFile(indexPath);
|
||||
}
|
||||
|
||||
// Show window when ready
|
||||
mainWindow.once('ready-to-show', () => {
|
||||
console.log('Window ready to show');
|
||||
mainWindow.show();
|
||||
});
|
||||
|
||||
// Handle load errors
|
||||
mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription, validatedURL) => {
|
||||
console.error('Failed to load:', errorCode, errorDescription, validatedURL);
|
||||
});
|
||||
@@ -112,7 +65,6 @@ function createWindow() {
|
||||
console.log('Frontend loaded successfully');
|
||||
});
|
||||
|
||||
// Handle window close
|
||||
mainWindow.on('close', (event) => {
|
||||
if (process.platform === 'darwin') {
|
||||
event.preventDefault();
|
||||
@@ -124,18 +76,12 @@ function createWindow() {
|
||||
mainWindow = null;
|
||||
});
|
||||
|
||||
// Handle external links
|
||||
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
|
||||
shell.openExternal(url);
|
||||
return { action: 'deny' };
|
||||
});
|
||||
}
|
||||
|
||||
// IPC handlers
|
||||
ipcMain.handle('get-backend-port', () => {
|
||||
return backendPort;
|
||||
});
|
||||
|
||||
ipcMain.handle('get-app-version', () => {
|
||||
return app.getVersion();
|
||||
});
|
||||
@@ -144,30 +90,8 @@ ipcMain.handle('get-platform', () => {
|
||||
return process.platform;
|
||||
});
|
||||
|
||||
ipcMain.handle('restart-backend', async () => {
|
||||
try {
|
||||
backendPort = await startBackend();
|
||||
return { success: true, port: backendPort };
|
||||
} catch (error) {
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
});
|
||||
|
||||
// App event handlers
|
||||
app.whenReady().then(async () => {
|
||||
// Create window immediately for fast startup
|
||||
app.whenReady().then(() => {
|
||||
createWindow();
|
||||
|
||||
// Start backend in background (non-blocking)
|
||||
try {
|
||||
backendPort = await startBackend();
|
||||
if (backendPort) {
|
||||
console.log('Backend started successfully on port:', backendPort);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Backend failed to start:', error);
|
||||
}
|
||||
|
||||
console.log('Termix started successfully');
|
||||
});
|
||||
|
||||
@@ -189,7 +113,10 @@ app.on('before-quit', () => {
|
||||
console.log('App is quitting...');
|
||||
});
|
||||
|
||||
// Error handling
|
||||
app.on('will-quit', () => {
|
||||
console.log('App will quit...');
|
||||
});
|
||||
|
||||
process.on('uncaughtException', (error) => {
|
||||
console.error('Uncaught Exception:', error);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user