|
1 |
| -// Modules to control application life and create native browser window |
2 |
| -const { app, BrowserWindow } = require('electron') |
| 1 | +const { app, dialog, BrowserWindow } = require('electron') |
3 | 2 | const path = require('node:path')
|
4 | 3 |
|
5 |
| -function createWindow () { |
6 |
| - // Create the browser window. |
| 4 | +const handler = require('serve-handler'); |
| 5 | +const http = require('http'); |
| 6 | + |
| 7 | +const APP_PORT = 1338; |
| 8 | + |
| 9 | +app.commandLine.appendSwitch('js-flags', '--max-old-space-size=16384'); |
| 10 | + |
| 11 | +const uri = path.join(path.dirname(app.getPath('exe')), 'resources'); |
| 12 | + |
| 13 | +const server = http.createServer((request, response) => { |
| 14 | + return handler(request, response, { |
| 15 | + public: uri, |
| 16 | + }); |
| 17 | +}); |
| 18 | + |
| 19 | +const startServer = () => new Promise((resolve, reject) => { |
| 20 | + let startFinished = false; |
| 21 | + server.listen(APP_PORT, () => { |
| 22 | + if (!startFinished) { |
| 23 | + startFinished = true; |
| 24 | + resolve(); |
| 25 | + } |
| 26 | + }); |
| 27 | + server.once('error', (err) => { |
| 28 | + if (!startFinished) { |
| 29 | + startFinished = true; |
| 30 | + reject(err); |
| 31 | + } |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | +async function createWindow () { |
| 36 | + |
| 37 | + try { |
| 38 | + await startServer(); |
| 39 | + } catch { |
| 40 | + dialog.showErrorBox('Application Error', `An unexpected error occurred. Looks like port ${APP_PORT} is busy`); |
| 41 | + app.quit(); |
| 42 | + process.exit(1); |
| 43 | + } |
| 44 | + |
7 | 45 | const mainWindow = new BrowserWindow({
|
8 | 46 | width: 800,
|
9 | 47 | height: 600,
|
10 | 48 | webPreferences: {
|
11 |
| - preload: path.join(__dirname, 'preload.js'), |
12 | 49 | webSecurity: false,
|
| 50 | + sandbox: false, |
13 | 51 | }
|
14 |
| - }) |
15 |
| - |
16 |
| - const uri = path.join(path.dirname(app.getPath('exe')), 'resources', 'index.html'); |
17 |
| - |
18 |
| - console.log({ uri }) |
| 52 | + }); |
19 | 53 |
|
20 |
| - // and load the index.html of the app. |
21 |
| - mainWindow.loadURL(`file://${uri}`); |
22 |
| - |
23 |
| - // Open the DevTools. |
24 |
| - // mainWindow.webContents.openDevTools() |
| 54 | + mainWindow.loadURL(`http://localhost:${APP_PORT}`); |
25 | 55 | }
|
26 | 56 |
|
27 |
| -// This method will be called when Electron has finished |
28 |
| -// initialization and is ready to create browser windows. |
29 |
| -// Some APIs can only be used after this event occurs. |
30 | 57 | app.whenReady().then(() => {
|
31 | 58 | createWindow()
|
32 |
| - |
33 |
| - app.on('activate', function () { |
34 |
| - // On macOS it's common to re-create a window in the app when the |
35 |
| - // dock icon is clicked and there are no other windows open. |
| 59 | + app.on('activate', () => { |
36 | 60 | if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
37 | 61 | })
|
38 | 62 | })
|
39 | 63 |
|
40 |
| -// Quit when all windows are closed, except on macOS. There, it's common |
41 |
| -// for applications and their menu bar to stay active until the user quits |
42 |
| -// explicitly with Cmd + Q. |
43 |
| -app.on('window-all-closed', function () { |
| 64 | +app.on('window-all-closed', () => { |
44 | 65 | if (process.platform !== 'darwin') app.quit()
|
45 | 66 | })
|
46 | 67 |
|
47 |
| -// In this file you can include the rest of your app's specific main process |
48 |
| -// code. You can also put them in separate files and require them here. |
| 68 | +app.on('quit', () => { |
| 69 | + server.close(); |
| 70 | +}) |
0 commit comments