Consolidate squirrel update code

This commit is contained in:
Kevin Sawicki 2014-11-13 15:48:59 -08:00
parent 327e8476dd
commit 7ba47840d8
3 changed files with 48 additions and 48 deletions

View File

@ -15,8 +15,8 @@ process.on 'uncaughtException', (error={}) ->
start = ->
if process.platform is 'win32'
handleSquirrelEvents = require './squirrel-events'
return if handleSquirrelEvents()
SquirrelUpdate = require './squirrel-update'
return if SquirrelUpdate.handleStartupEvent()
args = parseCommandLine()

View File

@ -1,21 +0,0 @@
app = require 'app'
path = require 'path'
SquirrelUpdate = require './squirrel-update'
spawnUpdateAndQuit = (option) ->
exeName = path.basename(process.execPath)
SquirrelUpdate.spawn ["--#{option}", exeName], -> app.quit()
module.exports = ->
switch process.argv[1]
when '--squirrel-install', '--squirrel-updated'
spawnUpdateAndQuit('createShortcut')
true
when '--squirrel-uninstall'
spawnUpdateAndQuit('removeShortcut')
true
when '--squirrel-obsolete'
app.quit()
true
else
false

View File

@ -1,32 +1,53 @@
app = require 'app'
ChildProcess = require 'child_process'
fs = require 'fs'
path = require 'path'
updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe')
exeName = path.basename(process.execPath)
module.exports =
spawn: (args, callback) ->
stdout = ''
error = null
# Spawn the Update.exe with the given arguments and invoke the callback when
# the command completes.
exports.spawn = (args, callback) ->
stdout = ''
error = null
args = args.map (arg) -> "\"#{arg.toString().replace(/"/g, '\\"')}\""
if /\s/.test(updateDotExe)
args.unshift("\"#{updateDotExe}\"")
args = args.map (arg) -> "\"#{arg.toString().replace(/"/g, '\\"')}\""
if /\s/.test(updateDotExe)
args.unshift("\"#{updateDotExe}\"")
else
args.unshift(updateDotExe)
args = ['/s', '/c', "\"#{cmdArgs.join(' ')}\""]
command = process.env.comspec or 'cmd.exe'
updateProcess = ChildProcess.spawn(command, args, windowsVerbatimArguments: true)
updateProcess.stdout.on 'data', (data) -> stdout += data
updateProcess.on 'error', (processError) -> error ?= processError
updateProcess.on 'close', (code, signal) ->
error ?= new Error("Command failed: #{signal}") if code isnt 0
error?.code ?= code
error?.stdout ?= stdout
callback(error, stdout)
undefined
# Is the Update.exe installed with Atom?
exports.existsSync = ->
fs.existsSync(updateDotExe)
# Handle squirrel events denoted by --squirrel-* command line arguments.
exports.handleStartupEvent = ->
switch process.argv[1]
when '--squirrel-install', '--squirrel-updated'
exports.spawn ['--createShortcut', exeName], -> app.quit()
spawnUpdateAndQuit('')
true
when '--squirrel-uninstall'
exports.spawn ['--removeShortcut', exeName], -> app.quit()
true
when '--squirrel-obsolete'
app.quit()
true
else
args.unshift(updateDotExe)
args = ['/s', '/c', "\"#{cmdArgs.join(' ')}\""]
command = process.env.comspec or 'cmd.exe'
updateProcess = ChildProcess.spawn(command, args, windowsVerbatimArguments: true)
updateProcess.stdout.on 'data', (data) -> stdout += data
updateProcess.on 'error', (processError) -> error ?= processError
updateProcess.on 'close', (code, signal) ->
error ?= new Error("Command failed: #{signal}") if code isnt 0
error?.code ?= code
error?.stdout ?= stdout
callback(error, stdout)
undefined
existsSync: ->
fs.existsSync(updateDotExe)
false