2015-06-15 18:28:47 +00:00
|
|
|
{EventEmitter} = require 'events'
|
|
|
|
SquirrelUpdate = require './auto-updater/squirrel-update-win'
|
2015-06-22 13:16:50 +00:00
|
|
|
app = require('app')
|
2015-06-15 18:28:47 +00:00
|
|
|
|
2015-06-16 10:31:55 +00:00
|
|
|
class AutoUpdater extends EventEmitter
|
2015-06-15 18:28:47 +00:00
|
|
|
|
|
|
|
quitAndInstall: ->
|
2015-06-22 13:16:50 +00:00
|
|
|
SquirrelUpdate.processStart ->
|
|
|
|
app.quit()
|
2015-06-15 18:28:47 +00:00
|
|
|
|
2015-06-16 09:04:37 +00:00
|
|
|
setFeedUrl: (updateUrl) ->
|
|
|
|
# set feed URL only when it hasn't been set before
|
|
|
|
unless @updateUrl
|
|
|
|
@updateUrl = updateUrl
|
2015-06-15 18:28:47 +00:00
|
|
|
|
|
|
|
checkForUpdates: ->
|
|
|
|
throw new Error('Update URL is not set') unless @updateUrl
|
|
|
|
|
|
|
|
@emit 'checking-for-update'
|
|
|
|
|
2015-06-22 13:33:08 +00:00
|
|
|
unless SquirrelUpdate.supported()
|
2015-06-15 18:28:47 +00:00
|
|
|
@emit 'update-not-available'
|
|
|
|
return
|
|
|
|
|
2015-06-22 13:33:08 +00:00
|
|
|
SquirrelUpdate.download (error, update) =>
|
2015-06-15 18:28:47 +00:00
|
|
|
if error?
|
|
|
|
@emit 'update-not-available'
|
|
|
|
return
|
|
|
|
|
|
|
|
unless update?
|
|
|
|
@emit 'update-not-available'
|
|
|
|
return
|
|
|
|
|
|
|
|
@emit 'update-available'
|
|
|
|
|
2015-06-22 13:33:08 +00:00
|
|
|
SquirrelUpdate.update @updateUrl, (error) =>
|
2015-06-15 18:28:47 +00:00
|
|
|
if error?
|
|
|
|
@emit 'update-not-available'
|
|
|
|
return
|
|
|
|
|
2015-06-22 13:16:50 +00:00
|
|
|
# info about the newly installed version and a function any of the event listeners can call to restart the application
|
2015-06-15 18:28:47 +00:00
|
|
|
@emit 'update-downloaded', {}, update.releaseNotes, update.version, new Date(), @updateUrl, => @quitAndInstall()
|
|
|
|
|
|
|
|
module.exports = new AutoUpdater()
|