forked from alxlchnr/electron-desktop-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
76 lines (67 loc) · 2.51 KB
/
main.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// This is free and unencumbered software released into the public domain.
// See LICENSE for details
const {app, BrowserWindow} = require('electron');
const log = require('electron-log');
const {autoUpdater} = require("electron-updater");
//-------------------------------------------------------------------
// Logging
//-------------------------------------------------------------------
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
log.info('App starting...');
//-------------------------------------------------------------------
// configure auto updater event listeners
//-------------------------------------------------------------------
function sendStatusToWindow(text) {
log.info(text);
win.webContents.send('message', text);
}
autoUpdater.on('checking-for-update', function () {
sendStatusToWindow('Checking for update...');
});
autoUpdater.on('update-available', function () {
sendStatusToWindow('Update available.');
});
autoUpdater.on('update-not-available', function () {
sendStatusToWindow('Update not available.');
});
autoUpdater.on('error', function (error) {
sendStatusToWindow(JSON.stringify(error));
});
autoUpdater.on('download-progress', function (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);
});
autoUpdater.on('update-downloaded', function () {
sendStatusToWindow('Update downloaded; will install in 5 seconds');
});
autoUpdater.on('update-downloaded', function () {
// Wait 5 seconds, then quit and install
setTimeout(function () {
autoUpdater.quitAndInstall();
}, 5000
);
});
//-------------------------------------------------------------------
// Open a window that displays the version
//-------------------------------------------------------------------
let win;
function createDefaultWindow() {
win = new BrowserWindow();
win.setMenu(null);
win.on('closed', function () {
win = null;
});
win.loadURL(`file://${__dirname}/filebrowser/dist/index.html`);
return win;
}
app.on('ready', function () {
createDefaultWindow();
autoUpdater.checkForUpdates();
// setTimeout(() => sendStatusToWindow('I\'m just a message to demonstrate, how to show notifications in Electron.'), 3000);
});
app.on('window-all-closed', function () {
app.quit();
});