2016-03-24 20:15:04 +00:00
|
|
|
'use strict'
|
2016-01-15 22:12:57 +00:00
|
|
|
|
2016-07-26 01:40:55 +00:00
|
|
|
const {app} = require('electron')
|
|
|
|
const {EventEmitter} = require('events')
|
2016-03-24 20:15:04 +00:00
|
|
|
const squirrelUpdate = require('./squirrel-update-win')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-07-26 01:40:55 +00:00
|
|
|
class AutoUpdater extends EventEmitter {
|
|
|
|
quitAndInstall () {
|
|
|
|
if (!this.updateAvailable) {
|
|
|
|
return this.emitError('No update available, can\'t quit and install')
|
|
|
|
}
|
|
|
|
squirrelUpdate.processStart()
|
2016-08-08 20:06:14 +00:00
|
|
|
app.quit()
|
2016-04-25 15:35:34 +00:00
|
|
|
}
|
2016-07-14 12:04:48 +00:00
|
|
|
|
2016-07-26 01:40:55 +00:00
|
|
|
getFeedURL () {
|
|
|
|
return this.updateURL
|
2016-02-03 17:52:04 +00:00
|
|
|
}
|
2016-07-26 01:40:55 +00:00
|
|
|
|
2018-02-15 02:58:59 +00:00
|
|
|
setFeedURL (options) {
|
|
|
|
let updateURL
|
|
|
|
if (typeof options === 'string') {
|
|
|
|
updateURL = options
|
|
|
|
} else if (typeof options === 'object' && typeof options.url === 'string') {
|
|
|
|
updateURL = options.url
|
|
|
|
} else {
|
|
|
|
throw new Error('Expected options.url to be a string but none was provided')
|
|
|
|
}
|
2016-07-26 01:40:55 +00:00
|
|
|
this.updateURL = updateURL
|
2016-02-03 17:52:04 +00:00
|
|
|
}
|
2016-07-26 01:40:55 +00:00
|
|
|
|
|
|
|
checkForUpdates () {
|
|
|
|
if (!this.updateURL) {
|
|
|
|
return this.emitError('Update URL is not set')
|
2016-03-10 19:54:17 +00:00
|
|
|
}
|
2016-07-26 01:40:55 +00:00
|
|
|
if (!squirrelUpdate.supported()) {
|
|
|
|
return this.emitError('Can not find Squirrel')
|
2016-03-10 19:54:17 +00:00
|
|
|
}
|
2016-07-26 01:40:55 +00:00
|
|
|
this.emit('checking-for-update')
|
2017-09-10 22:54:08 +00:00
|
|
|
squirrelUpdate.checkForUpdate(this.updateURL, (error, update) => {
|
2016-02-03 17:52:04 +00:00
|
|
|
if (error != null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
return this.emitError(error)
|
2016-02-03 17:52:04 +00:00
|
|
|
}
|
2016-07-26 01:40:55 +00:00
|
|
|
if (update == null) {
|
|
|
|
return this.emit('update-not-available')
|
|
|
|
}
|
|
|
|
this.updateAvailable = true
|
|
|
|
this.emit('update-available')
|
|
|
|
squirrelUpdate.update(this.updateURL, (error) => {
|
|
|
|
if (error != null) {
|
|
|
|
return this.emitError(error)
|
|
|
|
}
|
2016-08-08 20:06:14 +00:00
|
|
|
const {releaseNotes, version} = update
|
|
|
|
// Date is not available on Windows, so fake it.
|
|
|
|
const date = new Date()
|
2016-07-26 01:40:55 +00:00
|
|
|
this.emit('update-downloaded', {}, releaseNotes, version, date, this.updateURL, () => {
|
|
|
|
this.quitAndInstall()
|
|
|
|
})
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
|
|
|
})
|
2016-07-26 01:40:55 +00:00
|
|
|
}
|
2016-01-15 22:12:57 +00:00
|
|
|
|
2016-07-26 01:40:55 +00:00
|
|
|
// Private: Emit both error object and message, this is to keep compatibility
|
|
|
|
// with Old APIs.
|
|
|
|
emitError (message) {
|
2016-08-08 20:06:14 +00:00
|
|
|
this.emit('error', new Error(message), message)
|
2016-07-26 01:40:55 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
module.exports = new AutoUpdater()
|