Skip to content

Commit

Permalink
Added update-notifying modal on Linux & Macos
Browse files Browse the repository at this point in the history
  • Loading branch information
klaudiosinani committed Sep 8, 2017
1 parent ae9ca39 commit e79e51d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
},
"dependencies": {
"ms": "0.7.2",
"simple-get": "^2.0.0",
"devtron": "^1.4.0",
"electron-dl": "^1.9.0",
"electron-is-dev": "^0.1.2",
Expand Down
60 changes: 56 additions & 4 deletions update.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
'use strict';
const electron = require('electron');
const get = require('simple-get');

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';

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');
}
const latestVersion = data.version; // Get the latest version
console.log('Latest version of Tusk is: ' + latestVersion);
console.log('You are running Tusk on version: ' + 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');
const 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'
});
response(result); // Check which button was pressed based on index
}
} else {
// Updating URL did not resolve properly
console.log('Unexpected status code error');
}
}

function response(result) {
// If the `Download` button was pressed
// send the user to the latest Github release
if (result === 0) {
shell.openExternal('https://github.com/champloohq/tusk/releases/latest');
}
}

module.exports.init = () => {
if (process.platform !== 'win32') {
Expand Down Expand Up @@ -32,9 +82,11 @@ module.exports.init = () => {
};

module.exports.checkUpdate = () => {
if (process.platform !== 'win32') {
return;
if (process.platform === 'win32') {
// Auto-update on Windows
electron.autoUpdater.checkForUpdates();
} else {
// Check for updates manually on Linux/Macos
get.concat(updateURL, checkUpdate);
}

electron.autoUpdater.checkForUpdates();
};

0 comments on commit e79e51d

Please sign in to comment.