forked from klaudiosinani/tusk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate.js
103 lines (91 loc) · 2.96 KB
/
update.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use strict';
const electron = require('electron');
const get = require('simple-get');
let result;
let latestVersion;
const app = electron.app;
const shell = electron.shell;
const dialog = electron.dialog;
const currentVersion = app.getVersion();
const updateURL = 'https://champloohq.github.io/tusk/update.json';
const releaseURL = 'https://github.com/champloohq/tusk/releases/latest';
function checkUpdate(err, res, data) {
if (err) {
console.log('Update error.');
} else if (res.statusCode === 200) {
// Updating URL resolved properly
try {
data = JSON.parse(data); // Parse JSON safely
} catch (err) {
console.log('Invalid JSON object');
}
// Intialize the update process
latestVersion = data.version; // Get the latest version
console.log('Latest version of Tusk is: ' + latestVersion);
console.log('You are running Tusk on version: ' + currentVersion);
// Decide if the app should be updated
decideUpdate(latestVersion, currentVersion);
// Get user's response on update notification
response(result);
} else {
// Updating URL did not resolve properly
console.log('Unexpected status code error');
}
}
function decideUpdate(latestVerion, currentVersion) {
if (latestVersion === currentVersion) {
// User is already on the latest version
console.log('You are on the latest version');
} else {
// An updated version has been released
console.log('An update is available');
result = dialog.showMessageBox({
type: 'info',
buttons: ['Download', 'Dismiss'],
defaultId: 0, // Make `Download` the default action button
title: 'Update Tusk',
message: 'Version ' + latestVersion + ' is now available',
detail: 'Click Download to get it now'
});
return result;
}
}
function response(result) {
// If the `Download` button was pressed
// send the user to the latest Github release
if (result === 0) {
shell.openExternal(releaseURL);
}
}
module.exports.init = () => {
if (process.platform !== 'win32') {
return;
}
electron.autoUpdater.on('checking-for-update', () => {
console.log('checking-for-update');
});
electron.autoUpdater.on('update-available', () => {
console.log('update-available');
});
electron.autoUpdater.on('update-not-available', () => {
console.log('update-not-available');
});
electron.autoUpdater.on('update-downloaded', () => {
console.log('update-downloaded');
});
electron.autoUpdater.on('error', err => {
console.log('Error fetching updates', err);
});
const version = electron.app.getVersion();
const feedURL = `https://hazel-dqdcpouiom.now.sh/update/${process.platform}/${version}`;
electron.autoUpdater.setFeedURL(feedURL);
};
module.exports.checkUpdate = () => {
if (process.platform === 'win32') {
// Auto-update on Windows
electron.autoUpdater.checkForUpdates();
} else {
// Check for updates manually on Linux/Macos
get.concat(updateURL, checkUpdate);
}
};