Skip to content

Commit a943527

Browse files
Kimpyphilia
Kim
authored andcommitted
feat: ask user before updating app
1 parent f188f27 commit a943527

12 files changed

+16688
-86
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,6 @@ build/*
6464

6565
# excluse test outputs
6666
test/tmp
67+
68+
# excluse app update file
69+
dev-app-update.yml

package.json

+7
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@
166166
"appId": "com.graasp",
167167
"compression": "normal",
168168
"productName": "Graasp",
169+
"publish": [
170+
{
171+
"provider": "github",
172+
"owner": "graasp",
173+
"repo": "graasp-desktop"
174+
}
175+
],
169176
"directories": {
170177
"buildResources": "build",
171178
"output": "dist"

public/app/config/channels.js

+2
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,6 @@ module.exports = {
7474
POST_FILE_CHANNEL: 'file:post',
7575
DELETE_FILE_CHANNEL: 'file:delete',
7676
COMPLETE_TOUR_CHANNEL: 'tour:complete',
77+
GET_APP_UPGRADE_CHANNEL: 'app:upgrade:get',
78+
INSTALL_APP_UPGRADE_CHANNEL: 'app:upgrade:install',
7779
};

public/app/listeners/getAppUpgrade.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { autoUpdater } = require('electron-updater');
2+
const logger = require('../logger');
3+
const { GET_APP_UPGRADE_CHANNEL } = require('../config/channels');
4+
5+
const getAppUpgrade = mainWindow => async () => {
6+
// app update
7+
autoUpdater.logger = logger;
8+
autoUpdater.autoDownload = false;
9+
10+
autoUpdater.once('update-available', () => {
11+
logger.debug('update is available');
12+
mainWindow.webContents.send(GET_APP_UPGRADE_CHANNEL, true);
13+
});
14+
15+
autoUpdater.once('update-not-available', () => {
16+
logger.debug('update is not available');
17+
mainWindow.webContents.send(GET_APP_UPGRADE_CHANNEL, false);
18+
});
19+
20+
// noinspection ES6MissingAwait
21+
autoUpdater
22+
.checkForUpdates()
23+
.then()
24+
.catch(err => logger.error(err));
25+
};
26+
27+
module.exports = getAppUpgrade;

public/app/listeners/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ const windowAllClosed = require('./windowAllClosed');
5454
const completeTour = require('./completeTour');
5555
const postFile = require('./postFile');
5656
const deleteFile = require('./deleteFile');
57+
const installAppUpgrade = require('./installAppUpgrade');
58+
const getAppUpgrade = require('./getAppUpgrade');
5759

5860
module.exports = {
5961
loadSpace,
@@ -111,4 +113,6 @@ module.exports = {
111113
completeTour,
112114
postFile,
113115
deleteFile,
116+
installAppUpgrade,
117+
getAppUpgrade,
114118
};
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { autoUpdater } = require('electron-updater');
2+
const logger = require('../logger');
3+
const { INSTALL_APP_UPGRADE_CHANNEL } = require('../config/channels');
4+
5+
const installAppUpgrade = mainwindow => async (event, shouldUpgrade) => {
6+
try {
7+
if (shouldUpgrade) {
8+
autoUpdater.once('update-downloaded', () => {
9+
const msg = 'Update downloaded';
10+
logger.debug('message', { msg, hide: false, replaceAll: true });
11+
autoUpdater.quitAndInstall();
12+
});
13+
14+
mainwindow.webContents.send(INSTALL_APP_UPGRADE_CHANNEL, true);
15+
16+
autoUpdater.downloadUpdate().catch(err => {
17+
logger.error(err);
18+
mainwindow.webContents.send(INSTALL_APP_UPGRADE_CHANNEL, false);
19+
});
20+
}
21+
} catch (err) {
22+
logger.error(err);
23+
mainwindow.webContents.send(INSTALL_APP_UPGRADE_CHANNEL, false);
24+
}
25+
};
26+
27+
module.exports = installAppUpgrade;

public/electron.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const {
1010
} = require('electron');
1111
const path = require('path');
1212
const isDev = require('electron-is-dev');
13-
const { autoUpdater } = require('electron-updater');
1413
const Sentry = require('@sentry/electron');
1514
const ua = require('universal-analytics');
1615
const { machineIdSync } = require('node-machine-id');
@@ -79,6 +78,8 @@ const {
7978
COMPLETE_TOUR_CHANNEL,
8079
POST_FILE_CHANNEL,
8180
DELETE_FILE_CHANNEL,
81+
GET_APP_UPGRADE_CHANNEL,
82+
INSTALL_APP_UPGRADE_CHANNEL,
8283
} = require('./app/config/channels');
8384
const env = require('./env.json');
8485
const {
@@ -136,6 +137,8 @@ const {
136137
completeTour,
137138
postFile,
138139
deleteFile,
140+
installAppUpgrade,
141+
getAppUpgrade,
139142
} = require('./app/listeners');
140143
const isMac = require('./app/utils/isMac');
141144

@@ -369,15 +372,6 @@ const generateMenu = () => {
369372
};
370373

371374
app.on('ready', async () => {
372-
// updater
373-
autoUpdater.logger = logger;
374-
375-
// noinspection ES6MissingAwait
376-
autoUpdater
377-
.checkForUpdatesAndNotify()
378-
.then()
379-
.catch((err) => logger.error(err));
380-
381375
await ensureDatabaseExists(DATABASE_PATH);
382376
const db = bootstrapDatabase(DATABASE_PATH);
383377

@@ -388,6 +382,12 @@ app.on('ready', async () => {
388382
const visitor = ua(GOOGLE_ANALYTICS_ID, machineId);
389383
visitor.pageview('/').send();
390384

385+
// called when getting upgrade availability
386+
ipcMain.on(GET_APP_UPGRADE_CHANNEL, getAppUpgrade(mainWindow));
387+
388+
// called when getting upgrade availability
389+
ipcMain.on(INSTALL_APP_UPGRADE_CHANNEL, installAppUpgrade(mainWindow));
390+
391391
// called when saving a space
392392
ipcMain.on(SAVE_SPACE_CHANNEL, saveSpace(mainWindow, db));
393393

0 commit comments

Comments
 (0)