Skip to content

Commit 6806fde

Browse files
committed
✨ feat: Auto Update
Finally got autoUpdater to work :) Not it will auto update to latest version
1 parent e7177c7 commit 6806fde

5 files changed

Lines changed: 74 additions & 87 deletions

File tree

Utils/autoUpdater.js

Lines changed: 0 additions & 70 deletions
This file was deleted.

expressServer.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const express = require("express");
22
const path = require("path");
33
const cors = require("cors");
4+
5+
const log = require("electron-log");
46
const {
57
getGroups,
68
addGroup,
@@ -20,6 +22,7 @@ const {
2022
} = require("./Controllers/system-controller");
2123
const { handleGroupItem } = require("./Controllers/group-item-controller");
2224
const expressApp = express();
25+
let server;
2326
const port = 9153;
2427
function startExpressServer() {
2528
expressApp.use(cors());
@@ -101,9 +104,14 @@ function startExpressServer() {
101104
res.status(200);
102105
});
103106

104-
expressApp.listen(port, () => {
107+
server = expressApp.listen(port, () => {
108+
log.info("Express server started on port " + port);
105109
console.log(`Example app listening on port ${port}`);
106110
});
107111
}
108112

109-
module.exports = { startExpressServer };
113+
function stopExpressServer() {
114+
server.close();
115+
}
116+
117+
module.exports = { startExpressServer, stopExpressServer };

main.js

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
const { app, Tray, Menu, nativeImage } = require("electron");
1+
const {
2+
app,
3+
Tray,
4+
Menu,
5+
nativeImage,
6+
autoUpdater,
7+
dialog
8+
} = require("electron");
29
const sqlite3 = require("sqlite3");
310
const fs = require("fs");
4-
const { startExpressServer } = require("./expressServer");
5-
const { startWebsocketServer } = require("./websocketServer");
11+
const { startExpressServer, stopExpressServer } = require("./expressServer");
12+
const {
13+
startWebsocketServer,
14+
stopWebsocketServer
15+
} = require("./websocketServer");
616
const path = require("path");
717
const { openUrl } = require("./Controllers/system-controller");
818
const log = require("electron-log");
9-
const {
10-
checkForUpdate,
11-
downloadLatestRelease
12-
} = require("./Utils/autoUpdater");
1319

1420
if (require("electron-squirrel-startup")) app.quit();
1521

@@ -41,13 +47,48 @@ const handleDatabase = () => {
4147
};
4248

4349
app.whenReady().then(async () => {
50+
log.info(process.argv);
4451
// Check for update
45-
const updateInfo = await checkForUpdate(
46-
"https://api.github.com/repos/hysasuke/project-hub/releases"
47-
);
48-
console.log(updateInfo);
49-
if (updateInfo.updateAvailable) {
50-
downloadLatestRelease(updateInfo.downloadUrls);
52+
autoUpdater.setFeedURL({
53+
url: "https://github.com/hysasuke/Project-Hub/releases/latest/download/"
54+
});
55+
56+
autoUpdater.on("checking-for-update", () => {
57+
log.info("Checking for update...");
58+
});
59+
autoUpdater.on("update-available", (info) => {
60+
log.info("Update available.");
61+
});
62+
63+
autoUpdater.on("update-downloaded", (info) => {
64+
log.info("Update downloaded");
65+
autoUpdater.quitAndInstall();
66+
});
67+
68+
autoUpdater.on("update-not-available", (info) => {
69+
log.info("Update not available.");
70+
});
71+
72+
autoUpdater.on("error", (err) => {
73+
log.info("Error in auto-updater. " + err);
74+
});
75+
76+
autoUpdater.on("before-quit-for-update", () => {
77+
log.info("Update downloaded; will install on quit");
78+
stopExpressServer();
79+
stopWebsocketServer();
80+
});
81+
82+
// Listen to before-quit event
83+
app.on("before-quit", () => {
84+
log.info("App is quitting...");
85+
stopExpressServer();
86+
stopWebsocketServer();
87+
});
88+
89+
// set auto update if in production
90+
if (app.isPackaged) {
91+
autoUpdater.checkForUpdates();
5192
}
5293
// Make appData directory
5394
const appDataPath = path.join(app.getPath("appData"), "ProjectHub");
@@ -67,6 +108,7 @@ app.whenReady().then(async () => {
67108
const exeName = path.basename(process.execPath);
68109
// note: your contextMenu, Tooltip and Title code will go here!
69110
const contextMenu = Menu.buildFromTemplate([
111+
{ label: "Current Version: " + app.getVersion(), enabled: false },
70112
{
71113
label: "Auto Start",
72114
type: "checkbox",
@@ -99,6 +141,8 @@ app.whenReady().then(async () => {
99141
tray.setToolTip("Project Hub");
100142
tray.setTitle("Project Hub");
101143

144+
log.info("Starting express server...");
102145
startExpressServer();
146+
log.info("Starting websocket server...");
103147
startWebsocketServer();
104148
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ProjectHub",
3-
"version": "0.0.5",
3+
"version": "0.0.3",
44
"description": "Project Hub",
55
"main": "main.js",
66
"author": "Wenhao Li",

websocketServer.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ const messageHandler = (message) => {
6565
}
6666
};
6767

68+
function stopWebsocketServer() {
69+
server.close();
70+
}
71+
6872
module.exports = {
69-
startWebsocketServer
73+
startWebsocketServer,
74+
stopWebsocketServer
7075
};

0 commit comments

Comments
 (0)