Fix auto-updater check for updates

Change-type: patch
Changelog-entry: Fix auto-updater check for updates
Signed-off-by: Lorenzo Alberto Maria Ambrosi <lorenzoa@balena.io>
This commit is contained in:
Lorenzo Alberto Maria Ambrosi 2019-07-30 17:37:37 +02:00
parent f4ac4dee60
commit 8c2c4e233a
3 changed files with 24 additions and 25 deletions

View File

@ -130,21 +130,6 @@ run Etcher on a GNU/Linux system.
- liblzma (for xz decompression) - liblzma (for xz decompression)
Simulate an update alert
------------------------
You can set the `ETCHER_FAKE_S3_LATEST_VERSION` environment variable to a valid
semver version (greater than the current version) to trick the application into
thinking that what you put there is the latest available version, therefore
causing the update notification dialog to be presented at startup.
Note that the value of the variable will be ignored if it doesn't match the
release type of the current application version. For example, setting the
variable to a production version (e.g. `ETCHER_FAKE_S3_LATEST_VERSION=2.0.0`)
will be ignored if you're running a snapshot build, and vice-versa.
See [`PUBLISHING.md`][publishing] for more details about release types.
Recovering broken drives Recovering broken drives
------------------------ ------------------------

View File

@ -21,16 +21,25 @@ const path = require('path')
const _ = require('lodash') const _ = require('lodash')
const { autoUpdater } = require('electron-updater') const { autoUpdater } = require('electron-updater')
const Bluebird = require('bluebird') const Bluebird = require('bluebird')
const semver = require('semver')
const EXIT_CODES = require('../shared/exit-codes') const EXIT_CODES = require('../shared/exit-codes')
const buildWindowMenu = require('./menu') const buildWindowMenu = require('./menu')
const settings = require('./app/models/settings') const settings = require('./app/models/settings')
const analytics = require('./app/modules/analytics') const analytics = require('./app/modules/analytics')
const { getConfig } = require('../shared/utils') const { getConfig } = require('../shared/utils')
const { version, packageType } = require('../../package.json')
/* eslint-disable lodash/prefer-lodash-method */ /* eslint-disable lodash/prefer-lodash-method */
/* eslint-disable no-magic-numbers */
const config = settings.getDefaults() const config = settings.getDefaults()
const configUrl = settings.get('configUrl') || 'https://balena.io/etcher/static/config.json' const configUrl = settings.get('configUrl') || 'https://balena.io/etcher/static/config.json'
const updatablePackageTypes = [
'appimage',
'nsis',
'dmg'
]
const packageUpdatable = _.includes(updatablePackageTypes, packageType)
let packageUpdated = false
/** /**
* *
@ -40,14 +49,19 @@ const configUrl = settings.get('configUrl') || 'https://balena.io/etcher/static/
const checkForUpdates = async (interval) => { const checkForUpdates = async (interval) => {
// We use a while loop instead of a setInterval to preserve // We use a while loop instead of a setInterval to preserve
// async execution time between each function call // async execution time between each function call
while (true) { while (!packageUpdated) {
try { if (settings.get('updatesEnabled')) {
const release = await autoUpdater.checkForUpdates() try {
if (release.updateInfo.stagingPercentage) { const release = await autoUpdater.checkForUpdates()
await autoUpdater.downloadUpdate() const isOutdated = semver.compare(release.updateInfo.version, version) > 0
const shouldUpdate = parseInt(release.updateInfo.stagingPercentage, 10) > 0
if (shouldUpdate && isOutdated) {
await autoUpdater.downloadUpdate()
packageUpdated = true
}
} catch (err) {
analytics.logException(err)
} }
} catch (err) {
analytics.logException(err)
} }
await Bluebird.delay(interval) await Bluebird.delay(interval)
} }
@ -110,7 +124,7 @@ const createMainWindow = () => {
autoUpdater.on('error', (err) => { autoUpdater.on('error', (err) => {
analytics.logException(err) analytics.logException(err)
}) })
if (settings.get('updatesEnabled')) { if (packageUpdatable) {
try { try {
const onlineConfig = await getConfig(configUrl) const onlineConfig = await getConfig(configUrl)
const autoUpdaterConfig = _.get(onlineConfig, [ 'autoUpdates', 'autoUpdaterConfig' ], { const autoUpdaterConfig = _.get(onlineConfig, [ 'autoUpdates', 'autoUpdaterConfig' ], {

2
npm-shrinkwrap.json generated
View File

@ -14377,4 +14377,4 @@
} }
} }
} }
} }