112 lines
2.9 KiB
JavaScript
112 lines
2.9 KiB
JavaScript
const { spawn } = require('child_process');
|
|
const { app, BrowserWindow } = require('electron');
|
|
const path = require('path');
|
|
const url = require('url');
|
|
const configService = require('./backend/services/configService');
|
|
|
|
let backendExpressProcess;
|
|
let mainWindow;
|
|
|
|
const _configService = configService;
|
|
|
|
|
|
app.on('ready', () => {
|
|
// ! Start the Express server loop
|
|
startExpressServer();
|
|
_configService.createMediaFolders();
|
|
|
|
// Start the Electron app
|
|
createWindow();
|
|
});
|
|
|
|
app.on('before-quit', () => {
|
|
if (backendExpressProcess) {
|
|
backendExpressProcess.kill();
|
|
console.log('Killing backend process.');
|
|
}
|
|
});
|
|
|
|
app.on('window-all-closed', () => {
|
|
if (process.platform !== 'darwin') {
|
|
app.quit();
|
|
}
|
|
});
|
|
|
|
app.on('activate', () => {
|
|
if (mainWindow === null) {
|
|
createWindow();
|
|
}
|
|
});
|
|
|
|
// const { dialog, ipcMain } = require('electron');
|
|
|
|
// ipcMain.on('select-dirs', async (event, arg) => {
|
|
// const result = await dialog.showOpenDialog(mainWindow, {
|
|
// properties: ['openDirectory']
|
|
// })
|
|
// console.log('directories selected', result.filePaths)
|
|
// })
|
|
|
|
function startExpressServer() {
|
|
let parrentProcess = {
|
|
"electron_path": app.getPath('userData')
|
|
}
|
|
// Start the Express server
|
|
backendExpressProcess = spawn('node', ['backend/server.js'], { env: { PARENT_PROCESS: JSON.stringify(parrentProcess) } });
|
|
|
|
backendExpressProcess.stdout.on('data', (data) => {
|
|
console.log(`Express stdout: ${data}`);
|
|
if (data.toString().includes('Server started')) { // Adjust to match backend's startup message
|
|
mainWindow.webContents.send('backend-ready'); // Send event to frontend
|
|
}
|
|
});
|
|
|
|
backendExpressProcess.stderr.on('data', (data) => {
|
|
console.error(`Express stderr: ${data}`);
|
|
});
|
|
|
|
// Restart the server if it crashes
|
|
backendExpressProcess.on('exit', (code, signal) => {
|
|
console.log(`Express process exited with code ${code} and signal ${signal}. Restarting...`);
|
|
startExpressServer();
|
|
});
|
|
}
|
|
|
|
function createWindow() {
|
|
mainWindow = new BrowserWindow({
|
|
width: 1200,
|
|
height: 720,
|
|
webPreferences: {
|
|
preload: path.join(__dirname, 'preload.js'),
|
|
nodeIntegration: true,
|
|
enableRemoteModule: false,
|
|
contextIsolation: true,
|
|
sandbox: true
|
|
}
|
|
});
|
|
|
|
|
|
mainWindow.loadURL(
|
|
url.format({
|
|
pathname: path.join(__dirname, 'dist', 'showy', 'browser', 'index.html'),
|
|
protocol: 'file:',
|
|
slashes: true
|
|
})
|
|
);
|
|
|
|
mainWindow.on('closed', () => {
|
|
mainWindow = null;
|
|
});
|
|
|
|
mainWindow.webContents.on('will-navigate', (event, url) => {
|
|
event.preventDefault();
|
|
console.log('hiiiiiiiii');
|
|
mainWindow.loadURL(`file://${path.join(__dirname, 'dist/showy/index.html')}`);
|
|
});
|
|
|
|
mainWindow.webContents.on('did-fail-load', (event, url) => {
|
|
// event.preventDefault();
|
|
console.log('yoooo');
|
|
mainWindow.loadURL(`file://${path.join(__dirname, 'dist/showy/browser/index.html')}`);
|
|
});
|
|
} |