Skip to content

Instantly share code, notes, and snippets.

@kitze
Created March 23, 2019 11:03
Show Gist options
  • Save kitze/3b8ac5b9fd9b42876f7ab9fa32390600 to your computer and use it in GitHub Desktop.
Save kitze/3b8ac5b9fd9b42876f7ab9fa32390600 to your computer and use it in GitHub Desktop.
electron auto update
// Packages
const { app, dialog, autoUpdater } = require('electron');
const ms = require('ms');
const isDev = require('electron-is-dev');
const logger = require('electron-timber');
const notify = require('./notify');
const got = require('got');
// Utilities
const { version: packageJsonVersion } = require('../package');
const getUpdateUrl = () => {
try {
const { platform } = process;
const server = isDev ? 'http://localhost:4500' : 'https://your-app-updates-server.now.sh';
let version = isDev ? packageJsonVersion : app.getVersion();
let feedUrl = `${server}/update/${platform}/${version}?type=update`;
logger.log('feedUrl', feedUrl);
return feedUrl;
} catch (err) {
logger.error(err);
}
};
const setUpdateURL = async () => {
autoUpdater.setFeedURL(getUpdateUrl());
};
const checkForUpdates = async () => {
logger.log('Checking for updates...');
if (process.env.CONNECTION === 'offline') {
setTimeout(checkForUpdates, ms('30m'));
return;
}
try {
await setUpdateURL();
} catch (err) {
return;
}
autoUpdater.checkForUpdates();
};
const showWindowForRestart = () => {
notify({
title: 'Update downloaded!',
onClick: () => {
app.isAutoUpdating = true;
autoUpdater.quitAndInstall();
},
body: `A new update has been downloaded. Click here to restart the app.`
});
};
const startAppUpdates = async () => {
logger.log('Starting updates');
autoUpdater.on('error', error => {
logger.error('Updates error', error);
setTimeout(checkForUpdates, ms('15m'));
});
setTimeout(checkForUpdates, ms('10s'));
autoUpdater.on('update-downloaded', showWindowForRestart);
autoUpdater.on('checking-for-update', () => {
logger.log('Checking for app updates...');
});
autoUpdater.on('before-quit-for-update', () => {
logger.log('before quit for update...');
app.isAutoUpdating = true;
});
autoUpdater.on('update-available', () => {
logger.log('Found update for the app! Downloading...');
});
autoUpdater.on('update-not-available', () => {
logger.log('No updates found. Checking again in 5 minutes...');
setTimeout(checkForUpdates, ms('5m'));
});
};
module.exports = {
automatic: () => {
{
if (process.platform === 'linux') {
return;
}
if (!isDev) {
startAppUpdates();
}
}
},
manual: async () => {
try {
const { body } = await got(getUpdateUrl());
if (body) {
const { name, url } = JSON.parse(body);
if (name && url) {
let buttons = ['Download', 'Cancel'];
dialog.showMessageBox(
{
type: 'info',
message: `Twizzy ${name} is available!`,
title: `Twizzy ${name} is available!`,
detail: 'Do you want to download the new update?',
buttons
},
index => {
const button = buttons[index];
if (button === 'Download') {
checkForUpdates();
dialog.showMessageBox({
type: 'info',
message: 'Downloading update...',
title: 'Downloading update...',
detail: `The update is downloading in the background. You'll be notified when it's done.`,
buttons: ['OK']
});
}
if (button === 'Update now') {
showWindowForRestart();
}
}
);
}
} else {
dialog.showMessageBox({
type: 'none',
message: 'App is up to date!',
title: 'App is up to date',
detail: 'There are no updates available.',
buttons: ['OK']
});
}
} catch (err) {
dialog.showMessageBox({
type: 'error',
message: 'An error ocurred',
title: 'An error ocurred',
detail: 'Cannot check for updates, please try again later.',
buttons: ['OK']
});
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment