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