-
Notifications
You must be signed in to change notification settings - Fork 0
/
appUpdater.ts
62 lines (57 loc) · 2.15 KB
/
appUpdater.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { dialog } from 'electron';
import { autoUpdater } from 'electron-updater';
import isDev from 'electron-is-dev';
import log from 'electron-log'; //open ~/Library/Logs/mediref-desktop/log.log (may have to use 'open')
export function appUpdater(mainWindow: Electron.BrowserWindow) {
function sendStatusToWindow(text: string) {
if (mainWindow) {
mainWindow.webContents.send('Updater', text);
}
}
// Don't initiate auto-updates in development and on Linux system
// since autoUpdater doesn't work on Linux
if (isDev || process.platform === 'linux') {
sendStatusToWindow('Dev or Linux environment detected. Updater function will not run');
return;
}
// Log whats happening
autoUpdater.logger = log;
autoUpdater.on('checking-for-update', () => {
sendStatusToWindow('Checking for update...');
});
autoUpdater.on('update-available', () => {
sendStatusToWindow('Update available.');
});
autoUpdater.on('update-not-available', () => {
sendStatusToWindow('Update not available.');
});
autoUpdater.on('error', (err: Error) => {
log.info(err);
sendStatusToWindow(`Error in auto-updater. ${JSON.stringify(err)}`);
});
autoUpdater.on('download-progress', (progressObj) => {
let log_message = 'Download speed: ' + progressObj.bytesPerSecond;
log_message = log_message + ' - Downloaded ' + progressObj.percent + '%';
log_message = log_message + ' (' + progressObj.transferred + '/' + progressObj.total + ')';
sendStatusToWindow(log_message);
});
// Ask the user to restart if an update is available
autoUpdater.on('update-downloaded', async (event) => {
const { response } = await dialog.showMessageBox({
type: 'question',
buttons: ['Install and Relaunch', 'Install Later'],
defaultId: 0,
message: `A new Mediref update ${event.version} has been downloaded`,
detail: 'It will be installed the next time you restart the application',
});
if (response === 0) {
autoUpdater.quitAndInstall();
// setTimeout(() => {
// // force app to quit. This is just a workaround, ideally autoUpdater.quitAndInstall() should relaunch the app.
// app.quit();
// }, 1000);
}
});
// Init for updates
autoUpdater.checkForUpdates();
}