89 lines
2.0 KiB
JavaScript
89 lines
2.0 KiB
JavaScript
const { spawn } = require('child_process');
|
|
const { app, BrowserWindow } = require('electron');
|
|
const path = require('path');
|
|
const url = require('url');
|
|
|
|
let expressProcess;
|
|
let win;
|
|
|
|
|
|
|
|
|
|
// app.on('ready', createWindow);
|
|
app.on('ready', () => {
|
|
// ! Start the Express server loop
|
|
startExpressServer();
|
|
createMediaFolders();
|
|
|
|
// Start the Electron app
|
|
createWindow();
|
|
});
|
|
|
|
app.on('window-all-closed', () => {
|
|
if (process.platform !== 'darwin') {
|
|
app.quit();
|
|
}
|
|
});
|
|
|
|
app.on('activate', () => {
|
|
if (win === null) {
|
|
createWindow();
|
|
}
|
|
});
|
|
|
|
const { dialog, ipcMain } = require('electron');
|
|
const { createMediaFolders } = require('./backend/services/mediaService');
|
|
|
|
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
|
|
expressProcess = spawn('node', ['backend/server.js'], { env: { PARENT_PROCESS: JSON.stringify(parrentProcess) } });
|
|
expressProcess.stdout.on('data', (data) => {
|
|
console.log(`Express stdout: ${data}`);
|
|
});
|
|
expressProcess.stderr.on('data', (data) => {
|
|
console.error(`Express stderr: ${data}`);
|
|
});
|
|
|
|
// Restart the server if it crashes
|
|
expressProcess.on('exit', (code, signal) => {
|
|
console.log(`Express process exited with code ${code} and signal ${signal}. Restarting...`);
|
|
startExpressServer();
|
|
});
|
|
}
|
|
|
|
function createWindow() {
|
|
win = new BrowserWindow({
|
|
width: 1200,
|
|
height: 720,
|
|
webPreferences: {
|
|
preload: path.join(__dirname, 'preload.js'),
|
|
nodeIntegration: true,
|
|
enableRemoteModule: false,
|
|
contextIsolation: true,
|
|
sandbox: true
|
|
}
|
|
});
|
|
|
|
|
|
win.loadURL(
|
|
url.format({
|
|
pathname: path.join(__dirname, 'dist', 'showy', 'browser', 'index.html'),
|
|
protocol: 'file:',
|
|
slashes: true
|
|
})
|
|
);
|
|
|
|
win.on('closed', () => {
|
|
win = null;
|
|
});
|
|
} |