From 18eaec374c2d43f0eaa858d53f9cc196386a27e9 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 13 Nov 2014 15:42:51 -0800 Subject: [PATCH] Spawn Update.exe through cmd.exe /s /c --- src/browser/auto-updater-win32.coffee | 33 ++++++--------------------- src/browser/squirrel-events.coffee | 8 ++----- src/browser/squirrel-update.coffee | 31 +++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 32 deletions(-) create mode 100644 src/browser/squirrel-update.coffee diff --git a/src/browser/auto-updater-win32.coffee b/src/browser/auto-updater-win32.coffee index 6f5727886..337e8b596 100644 --- a/src/browser/auto-updater-win32.coffee +++ b/src/browser/auto-updater-win32.coffee @@ -1,48 +1,29 @@ -ChildProcess = require 'child_process' {EventEmitter} = require 'events' -fs = require 'fs' -path = require 'path' - _ = require 'underscore-plus' shellAutoUpdater = require 'auto-updater' +SquirrelUpdate = require './squirrel-update' class AutoUpdater _.extend @prototype, EventEmitter.prototype - constructor: -> - @updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe') - setFeedUrl: (@updateUrl) -> if @updateUrl # Schedule an update when the feed URL is set process.nextTick => @checkForUpdates() - spawnUpdate: (args, callback) -> - stdout = '' - error = null - updateProcess = ChildProcess.spawn(@updateDotExe, args) - 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 - quitAndInstall: -> - unless fs.existsSync(@updateDotExe) + unless SquirrelUpdate.existsSync() shellAutoUpdater.quitAndInstall() return - @spawnUpdate ['--update', @updateUrl], (error) => + SquirrelUpdate.spawn ['--update', @updateUrl], (error) => return if error? - @spawnUpdate ['--processStart', 'atom.exe'], -> + SquirrelUpdate.spawn ['--processStart', 'atom.exe'], -> shellAutoUpdater.quitAndInstall() downloadUpdate: (callback) -> - @spawnUpdate ['--download', @updateUrl], (error, stdout) -> + SquirrelUpdate.spawn ['--download', @updateUrl], (error, stdout) -> return callback(error) if error? try @@ -56,14 +37,14 @@ class AutoUpdater callback(null, update) installUpdate: (callback) -> - @spawnUpdate(['--update', @updateUrl], callback) + SquirrelUpdate.spawn(['--update', @updateUrl], callback) checkForUpdates: -> throw new Error('Update URL is not set') unless @updateUrl @emit 'checking-for-update' - unless fs.existsSync(@updateDotExe) + unless SquirrelUpdate.existsSync() @emit 'update-not-available' return diff --git a/src/browser/squirrel-events.coffee b/src/browser/squirrel-events.coffee index 4725cf7f5..e3d75ab57 100644 --- a/src/browser/squirrel-events.coffee +++ b/src/browser/squirrel-events.coffee @@ -1,14 +1,10 @@ app = require 'app' -ChildProcess = require 'child_process' path = require 'path' +SquirrelUpdate = require './squirrel-update' spawnUpdateAndQuit = (option) -> - updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe') exeName = path.basename(process.execPath) - updateProcess = ChildProcess.spawn(updateDotExe, ["--#{option}", exeName]) - updateProcess.on 'error', -> # Ignore errors - updateProcess.on 'close', -> app.quit() - undefined + SquirrelUpdate.spawn ["--#{option}", exeName], -> app.quit() module.exports = -> switch process.argv[1] diff --git a/src/browser/squirrel-update.coffee b/src/browser/squirrel-update.coffee new file mode 100644 index 000000000..57fa6a7ff --- /dev/null +++ b/src/browser/squirrel-update.coffee @@ -0,0 +1,31 @@ +ChildProcess = require 'child_process' +fs = require 'fs' + +updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe') + +module.exports = + spawn: (args, callback) -> + stdout = '' + error = null + + 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 + + existsSync: -> + fs.existsSync(updateDotExe)