electron/atom/browser/api/lib/auto-updater/auto-updater-win.coffee
Patrick Detlefsen 621178f558 initial idea
2015-06-15 20:28:47 +02:00

60 lines
1.5 KiB
CoffeeScript

{EventEmitter} = require 'events'
SquirrelUpdate = require './auto-updater/squirrel-update-win'
class AutoUpdater
@__proto__ = EventEmitter.prototype
quitAndInstall: ->
# TODO
setFeedUrl: (@updateUrl) ->
checkForUpdates: ->
throw new Error('Update URL is not set') unless @updateUrl
@emit 'checking-for-update'
unless SquirrelUpdate.existsSync()
@emit 'update-not-available'
return
@downloadUpdate (error, update) =>
if error?
@emit 'update-not-available'
return
unless update?
@emit 'update-not-available'
return
@emit 'update-available'
@installUpdate (error) =>
if error?
@emit 'update-not-available'
return
@emit 'update-downloaded', {}, update.releaseNotes, update.version, new Date(), @updateUrl, => @quitAndInstall()
downloadUpdate: (callback) ->
SquirrelUpdate.spawn ['--download', @updateUrl], (error, stdout) ->
return callback(error) if error?
try
# Last line of output is the JSON details about the releases
json = stdout.trim().split('\n').pop()
update = JSON.parse(json)?.releasesToApply?.pop?()
catch error
error.stdout = stdout
return callback(error)
callback(null, update)
installUpdate: (callback) ->
SquirrelUpdate.spawn(['--update', @updateUrl], callback)
supportsUpdates: ->
SquirrelUpdate.existsSync()
module.exports = new AutoUpdater()