Skip to content

Instantly share code, notes, and snippets.

@akashnimare
Last active March 26, 2018 14:38
Show Gist options
  • Save akashnimare/6374ab79d5c8cf235971079e6959fe88 to your computer and use it in GitHub Desktop.
Save akashnimare/6374ab79d5c8cf235971079e6959fe88 to your computer and use it in GitHub Desktop.
Add auto-updates to your Electron app
'use strict';
const os = require('os');
const {app, autoUpdater, dialog} = require('electron');
const version = app.getVersion();
const platform = os.platform() + '_' + os.arch(); // usually returns darwin_64
const updaterFeedURL = 'http://zulipdesktop.herokuapp.com/update/' + platform + '/' + version;
// replace updaterFeedURL with http://yourappname.herokuapp.com
function appUpdater() {
autoUpdater.setFeedURL(updaterFeedURL);
/* Log whats happening
TODO send autoUpdater events to renderer so that we could console log it in developer tools
You could alsoe use nslog or other logging to see what's happening */
autoUpdater.on('error', err => console.log(err));
autoUpdater.on('checking-for-update', () => console.log('checking-for-update'));
autoUpdater.on('update-available', () => console.log('update-available'));
autoUpdater.on('update-not-available', () => console.log('update-not-available'));
// Ask the user if update is available
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
let message = app.getName() + ' ' + releaseName + ' is now available. It will be installed the next time you restart the application.';
if (releaseNotes) {
const splitNotes = releaseNotes.split(/[^\r]\n/);
message += '\n\nRelease notes:\n';
splitNotes.forEach(notes => {
message += notes + '\n\n';
});
}
// Ask user to update the app
dialog.showMessageBox({
type: 'question',
buttons: ['Install and Relaunch', 'Later'],
defaultId: 0,
message: 'A new version of ' + app.getName() + ' has been downloaded',
detail: message
}, response => {
if (response === 0) {
setTimeout(() => autoUpdater.quitAndInstall(), 1);
}
});
});
// init for updates
autoUpdater.checkForUpdates();
}
exports = module.exports = {
appUpdater
};
@mattpilott
Copy link

Hey this is great, i have it all working even with heroku it seems. I am yet to see what happens when i add a new release to github though

I do have a question. In my app id like it to check for updates on start, which works fine. Then I also want the user to be able to check for updates themselves. I have this setup but i'm having an issue whereby the updater function runs exponentially. For example if I boot the app in the console it runs the check (no. 1) now if i click 'check for updates' it will run twice (instead of once) if i click it again it will run 3 times and so on.

How can I stop this from happening?

I really appreciate your help on this and my nuts woes.

@akashnimare
Copy link
Author

@matt3224 somehow I missed this. Have you figured out your problem? Let me know if you need any help in setting up auto-updates.

@JeremySwiat
Copy link

Hi.

About the auto updating, any idea why this :

"const updaterFeedURL = 'http://myapp.herokuapp.com/update/' + platform + '/' + version;"

send me to a 404 error when i'm launching my app ? I think it doesn't know what is the "/update" part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment