Skip to content

Commit

Permalink
Basic Check update for platforms without auto updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanseidel committed Mar 27, 2017
1 parent 88880e6 commit 49962da
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 19 deletions.
61 changes: 42 additions & 19 deletions controllers/AutoUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,79 @@ const { autoUpdater, BrowserWindow } = require('electron');
const os = require('os');

const UPDATE_SERVER_HOST = 'tournamenter.herokuapp.com';
const UPDATABLE_PLATFORMS = ['darwin', 'win32'];
const DELAY_BEFORE_CHECKING = 10000;

const VERSION = eApp.getVersion()
const FEED_URL = `http://${UPDATE_SERVER_HOST}/update/${os.platform()}_${os.arch()}/${VERSION}`

exports.init = function init(window) {

if(app.helpers.isDev()) {
console.log(TAG, chalk.gray('Dev Mode. Skip autoupdate'));
return
console.log(TAG, chalk.dim('Dev Mode. Skip autoDownload'));
console.log(TAG, chalk.white(feedURL));
return;
}

if(os.platform() !== 'win32') {
console.log(TAG, chalk.gray('Skip AutoUpdater. running on ' + os.platform()));
var supportUpdates = (UPDATABLE_PLATFORMS.indexOf(os.platform()) > -1)
if(!supportUpdates) {
console.log(TAG, chalk.dim('Platform does AutoUpdater. running on ' + os.platform()));

// Manual Check if update is available
setTimeout(exports.manualCheckUpdate, DELAY_BEFORE_CHECKING)
return;
}

const version = eApp.getVersion()
autoUpdater.addListener('update-available', (event) => {
console.log(TAG, chalk.green('A new update is available'));
app.controllers.MainWindow.notify('', 'New Update available. Downloading...', 8000);
app.controllers.MainWindow.notify('Updater', 'New Update available. Downloading...', 8000);
})

autoUpdater.addListener('update-downloaded', (event, releaseNotes, releaseName, releaseDate, updateURL) => {
app.controllers.MainWindow.notify(
'A new update is ready to install',
'Updater Ready',
`Version ${releaseName} is downloaded and will be automatically installed on Quit`,
'OK'
);
})

autoUpdater.addListener('error', (error) => {
console.error(TAG, error)
app.controllers.MainWindow.notify('Update Error', 'Failed to download Update: ' + error, 'OK');
})

autoUpdater.addListener('checking-for-update', (event) => {
console.log(TAG, 'checking-for-update')
app.controllers.MainWindow.notify('', 'Checking for updates...');
app.controllers.MainWindow.notify('Updater', 'Checking for updates...');
})

autoUpdater.addListener('update-not-available', () => {
console.log(TAG, 'update-not-available')
app.controllers.MainWindow.notify('Updater', 'Tournamenter is up to date! Swweeeeeeet');
})

autoUpdater.setFeedURL(`http://${UPDATE_SERVER_HOST}/update/${os.platform()}_${os.arch()}/${version}`)
autoUpdater.setFeedURL(FEED_URL)

var win = app.controllers.MainWindow.getWindow();
setTimeout(() => {
autoUpdater.checkForUpdates();
}, DELAY_BEFORE_CHECKING);
}

if(win){
win.webContents.once('did-frame-finish-load', (event) => {
autoUpdater.checkForUpdates()
})
}else{
setTimeout(() => {
autoUpdater.checkForUpdates();
}, 6000);
}
exports.manualCheckUpdate = function () {
app.helpers.CheckUpdate(FEED_URL, function (err, update) {
if (err) {
console.error(TAG, err)
app.controllers.MainWindow.notify('Update Error', 'Failed to check for updates');
return
}

// Update is available. Notify window
app.controllers.MainWindow.notify(
'Updater Available',
`Version ${update.version} is available for download.`,
'OK'
);

app.controllers.MainWindow.send('newUpdate', update);
})
}
8 changes: 8 additions & 0 deletions controllers/MainWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ exports.notify = function notify(title, message, stick) {

mainWindow.webContents.send('notify', title, message, stick);
}

exports.send = function send() {
if (!mainWindow) {
return;
}

mainWindow.webContents.send.apply(mainWindow.webContents, arguments);
}
30 changes: 30 additions & 0 deletions helpers/CheckUpdate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const request = require('request')

/*
* Checks for update given the url.
* Returns the Update object with ''
*/
module.exports = function CheckUpdate(url, next) {
request({
url: url,
json: true,
}, function (error, response, body) {
if (error) {
return next && next('Failed to check for updates')
}

if (response.statusCode == 200) {
// New update available. Parse version
body.version = body.url.match(/version\/([\d\.]*)/g)[1] || body.name
module.exports.newUpdate = body
return next && next(null, body)
}

return next && next(null, null)
})
}

/*
* Stores if there is an update
*/
module.exports.newUpdate = false

0 comments on commit 49962da

Please sign in to comment.