{EventEmitter} = require 'events'
SquirrelUpdate = require './auto-updater/squirrel-update-win'
+app = require('app')
class AutoUpdater extends EventEmitter
quitAndInstall: ->
- # TODO
+ SquirrelUpdate.processStart ->
+ app.quit()
setFeedUrl: (updateUrl) ->
# set feed URL only when it hasn't been set before
@emit 'update-not-available'
return
+ # info about the newly installed version and a function any of the event listeners can call to restart the application
@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)
+ SquirrelUpdate.download(callback)
installUpdate: (callback) ->
- SquirrelUpdate.spawn(['--update', @updateUrl], callback)
+ SquirrelUpdate.update(@updateUrl, callback)
supportsUpdates: ->
- SquirrelUpdate.existsSync()
-
+ SquirrelUpdate.supported()
module.exports = new AutoUpdater()
# Spawn a command and invoke the callback when it completes with an error
# and the output from standard out.
-spawn = (command, args, callback) ->
+spawnUpdate = (args, callback) ->
stdout = ''
try
- spawnedProcess = ChildProcess.spawn(command, args)
+ spawnedProcess = ChildProcess.spawn(updateDotExe, args)
catch error
# Spawn can throw an error
process.nextTick -> callback?(error, stdout)
error?.stdout ?= stdout
callback?(error, stdout)
-# Spawn the Update.exe with the given arguments and invoke the callback when
-# the command completes.
-spawnUpdate = (args, callback) ->
- spawn(updateDotExe, args, callback)
+processStart = (callback) ->
+ spawnUpdate(['--processStart', exeName], callback)
+
+download = (callback) ->
+ spawnUpdate ['--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)
-# Exports
-exports.spawn = spawnUpdate
+update = (updateUrl, callback) ->
+ spawnUpdate ['--update', updateUrl], callback
-# Is the Update.exe installed with Atom?
-exports.existsSync = ->
+# Is the Update.exe installed with the current application?
+exports.supported = ->
fs.accessSync(updateDotExe, fs.R_OK)
+exports.processStart = processStart
+exports.download = download
+exports.update = update