diff --git a/src/browser/main.coffee b/src/browser/main.coffee index d308c1e7f..b5f95bd34 100644 --- a/src/browser/main.coffee +++ b/src/browser/main.coffee @@ -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() diff --git a/src/browser/squirrel-events.coffee b/src/browser/squirrel-events.coffee deleted file mode 100644 index e3d75ab57..000000000 --- a/src/browser/squirrel-events.coffee +++ /dev/null @@ -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 diff --git a/src/browser/squirrel-update.coffee b/src/browser/squirrel-update.coffee index 723ee2d49..0959c282d 100644 --- a/src/browser/squirrel-update.coffee +++ b/src/browser/squirrel-update.coffee @@ -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