aboutsummaryrefslogtreecommitdiffstats
path: root/src/electron/ipc-api/autoUpdate.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/electron/ipc-api/autoUpdate.ts')
-rw-r--r--src/electron/ipc-api/autoUpdate.ts82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/electron/ipc-api/autoUpdate.ts b/src/electron/ipc-api/autoUpdate.ts
new file mode 100644
index 000000000..e6b805edc
--- /dev/null
+++ b/src/electron/ipc-api/autoUpdate.ts
@@ -0,0 +1,82 @@
1import { app, ipcMain, BrowserWindow } from 'electron';
2import { autoUpdater } from 'electron-updater';
3import { isMac, isWindows } from '../../environment';
4
5const debug = require('debug')('Ferdi:ipcApi:autoUpdate');
6
7export default (params: { mainWindow: BrowserWindow; settings: any }) => {
8 const enableUpdate = Boolean(params.settings.app.get('automaticUpdates'));
9
10 if (!enableUpdate) {
11 autoUpdater.autoInstallOnAppQuit = false;
12 autoUpdater.autoDownload = false;
13 } else if (isMac || isWindows || process.env.APPIMAGE) {
14 ipcMain.on('autoUpdate', (event, args) => {
15 if (enableUpdate) {
16 try {
17 autoUpdater.autoInstallOnAppQuit = false;
18 autoUpdater.allowPrerelease = Boolean(
19 params.settings.app.get('beta'),
20 );
21 autoUpdater.channel = autoUpdater.allowPrerelease ? 'beta' : 'latest';
22
23 if (params.settings.app.get('nightly')) {
24 autoUpdater.allowPrerelease = Boolean(
25 params.settings.app.get('nightly'),
26 );
27 autoUpdater.channel = 'alpha';
28 }
29
30 if (args.action === 'check') {
31 debug('checking for update');
32 autoUpdater.checkForUpdates();
33 } else if (args.action === 'install') {
34 debug('installing update');
35 autoUpdater.quitAndInstall();
36 // we need to send a quit event
37 setTimeout(() => {
38 app.quit();
39 }, 20);
40 }
41 } catch (error) {
42 console.error(error);
43 event.sender.send('autoUpdate', { error: true });
44 }
45 }
46 });
47
48 autoUpdater.on('update-not-available', () => {
49 debug('update-not-available');
50 params.mainWindow.webContents.send('autoUpdate', { available: false });
51 });
52
53 autoUpdater.on('update-available', event => {
54 debug('update-available');
55
56 if (enableUpdate) {
57 params.mainWindow.webContents.send('autoUpdate', {
58 version: event.version,
59 available: true,
60 });
61 }
62 });
63
64 autoUpdater.on('download-progress', progressObj => {
65 let logMessage = `Download speed: ${progressObj.bytesPerSecond}`;
66 logMessage = `${logMessage} - Downloaded ${progressObj.percent}%`;
67 logMessage = `${logMessage} (${progressObj.transferred}/${progressObj.total})`;
68
69 debug(logMessage);
70 });
71
72 autoUpdater.on('update-downloaded', () => {
73 debug('update-downloaded');
74 params.mainWindow.webContents.send('autoUpdate', { downloaded: true });
75 });
76
77 autoUpdater.on('error', () => {
78 debug('update-error');
79 params.mainWindow.webContents.send('autoUpdate', { error: true });
80 });
81 }
82};