Created
November 14, 2018 05:00
-
-
Save kitze/9fe3203db86897147a747dc42516ed15 to your computer and use it in GitHub Desktop.
electron auto-update
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); | |
const { version: packageJsonVersion } = require('../package'); | |
const getUpdateUrl = () => { | |
try { | |
const { platform } = process; | |
const server = isDev ? 'http://localhost:4500' : 'https://my-app-updates.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 startAppUpdates = async () => { | |
logger.log('Starting updates'); | |
autoUpdater.on('error', error => { | |
console.error(error); | |
setTimeout(checkForUpdates, ms('15m')); | |
}); | |
setTimeout(checkForUpdates, ms('10s')); | |
autoUpdater.on('update-downloaded', async () => { | |
notify({ | |
title: 'Update downloaded!', | |
onClick: () => { | |
autoUpdater.quitAndInstall(); | |
app.quit(); | |
}, | |
body: `A new update has been downloaded. Click here to restart the app.` | |
}); | |
}); | |
autoUpdater.on('checking-for-update', () => { | |
logger.log('Checking for app updates...'); | |
}); | |
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: () => { | |
{ | |
logger.log(process.platform); | |
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: `App ${name} is available!`, | |
title: `App ${name} is available!`, | |
detail: 'Do you want to download the new update?', | |
buttons | |
}, | |
index => { | |
const button = buttons[index]; | |
if (button === 'Download') { | |
autoUpdater.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'] | |
}); | |
} | |
} | |
); | |
} | |
} 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