Skip to content

Instantly share code, notes, and snippets.

@shakogegia
Created May 1, 2020 15:18
Show Gist options
  • Save shakogegia/95e955d4faffb2bb6ef139f016d832bd to your computer and use it in GitHub Desktop.
Save shakogegia/95e955d4faffb2bb6ef139f016d832bd to your computer and use it in GitHub Desktop.
import { app } from 'electron'
import { autoUpdater } from 'electron-updater'
import log from 'electron-log'
import axios from 'axios'
import semverGT from 'semver/functions/gt'
const quitAndInstall = () => {
autoUpdater.quitAndInstall()
}
const downloadUpdate = e => {
autoUpdater.downloadUpdate(e.cancellationToken)
}
const checkForPrivateUpdates = async e => {
const response = await axios.get(`http://example.com/api/release`, {
params: {
platform: process.platform,
currentVersion: app.getVersion(),
nextVersion: e.updateInfo.version,
},
})
if (response.data.canUpdate) {
downloadUpdate(e)
return e
}
return { isUpdateAvailable: false }
}
const checkForUpdates = async () => {
const result = await autoUpdater.checkForUpdates()
const isUpdateAvailable = semverGT(result.updateInfo.version, app.getVersion())
if (!isUpdateAvailable) {
return result
}
const data = await checkForPrivateUpdates(result)
return data
}
const startCheckUpdates = () => {
if (process.env.NODE_ENV === 'production') {
checkForUpdates()
setInterval(() => {
checkForUpdates()
}, 1000 * 30 * 60) // every 30 min
}
}
const setup = () => {
autoUpdater.autoDownload = false
autoUpdater.logger = log
setTimeout(() => {
startCheckUpdates()
}, 1000 * 30)
}
export { quitAndInstall, checkForUpdates, setup }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment