Skip to content

Commit 4a7f6a5

Browse files
committed
launcher
1 parent 0a28882 commit 4a7f6a5

12 files changed

+513
-67
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
build
33
.DS_Store
4+
lib

lib/index.html

+326-13
Large diffs are not rendered by default.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"author": "USER",
99
"scripts": {
1010
"start": "electron ./src",
11-
"build": "rimraf build && electron-builder --config electron-builder.yml"
11+
"build": "rimraf build && electron-builder --config electron-builder.yml",
12+
"postinstall": "sh ./scripts/postinstall.sh"
1213
},
1314
"devDependencies": {
1415
"electron": "31.4.0",

scripts/postinstall.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
cd src
3+
npm install

src/main.js

+50-28
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,70 @@
1-
// Modules to control application life and create native browser window
2-
const { app, BrowserWindow } = require('electron')
1+
const { app, dialog, BrowserWindow } = require('electron')
32
const path = require('node:path')
43

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+
745
const mainWindow = new BrowserWindow({
846
width: 800,
947
height: 600,
1048
webPreferences: {
11-
preload: path.join(__dirname, 'preload.js'),
1249
webSecurity: false,
50+
sandbox: false,
1351
}
14-
})
15-
16-
const uri = path.join(path.dirname(app.getPath('exe')), 'resources', 'index.html');
17-
18-
console.log({ uri })
52+
});
1953

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}`);
2555
}
2656

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.
3057
app.whenReady().then(() => {
3158
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', () => {
3660
if (BrowserWindow.getAllWindows().length === 0) createWindow()
3761
})
3862
})
3963

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', () => {
4465
if (process.platform !== 'darwin') app.quit()
4566
})
4667

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+
})

src/package-lock.json

+114
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
"version": "1.0.0",
1111
"devDependencies": {
1212
"electron": "31.4.0"
13+
},
14+
"dependencies": {
15+
"serve-handler": "6.1.5"
1316
}
1417
}

src/preload.js

-18
This file was deleted.

src/renderer.js

-7
This file was deleted.

template/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# The static assets template
2+
3+
Rename that dir from `template` to `lib`

template/index.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<link href="./styles.css" rel="stylesheet">
6+
<title>Hello World!</title>
7+
</head>
8+
<body>
9+
<h1>Hello World!</h1>
10+
</body>
11+
</html>

lib/styles.css template/styles.css

File renamed without changes.

0 commit comments

Comments
 (0)