Skip to content

Commit

Permalink
Merge pull request #2875 from balena-io/fix-auto-updater
Browse files Browse the repository at this point in the history
Fix auto updater
  • Loading branch information
thundron authored Aug 19, 2019
2 parents f4ac4de + 8c2c4e2 commit 5cfb95e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
15 changes: 0 additions & 15 deletions docs/USER-DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,6 @@ run Etcher on a GNU/Linux system.

- 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
------------------------

Expand Down
32 changes: 23 additions & 9 deletions lib/gui/etcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,25 @@ const path = require('path')
const _ = require('lodash')
const { autoUpdater } = require('electron-updater')
const Bluebird = require('bluebird')
const semver = require('semver')
const EXIT_CODES = require('../shared/exit-codes')
const buildWindowMenu = require('./menu')
const settings = require('./app/models/settings')
const analytics = require('./app/modules/analytics')
const { getConfig } = require('../shared/utils')
const { version, packageType } = require('../../package.json')
/* eslint-disable lodash/prefer-lodash-method */
/* eslint-disable no-magic-numbers */

const config = settings.getDefaults()

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

/**
*
Expand All @@ -40,14 +49,19 @@ const configUrl = settings.get('configUrl') || 'https://balena.io/etcher/static/
const checkForUpdates = async (interval) => {
// We use a while loop instead of a setInterval to preserve
// async execution time between each function call
while (true) {
try {
const release = await autoUpdater.checkForUpdates()
if (release.updateInfo.stagingPercentage) {
await autoUpdater.downloadUpdate()
while (!packageUpdated) {
if (settings.get('updatesEnabled')) {
try {
const release = await autoUpdater.checkForUpdates()
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)
}
Expand Down Expand Up @@ -110,7 +124,7 @@ const createMainWindow = () => {
autoUpdater.on('error', (err) => {
analytics.logException(err)
})
if (settings.get('updatesEnabled')) {
if (packageUpdatable) {
try {
const onlineConfig = await getConfig(configUrl)
const autoUpdaterConfig = _.get(onlineConfig, [ 'autoUpdates', 'autoUpdaterConfig' ], {
Expand Down
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5cfb95e

Please sign in to comment.