Spawn Update.exe through cmd.exe /s /c

This commit is contained in:
Kevin Sawicki
2014-11-13 15:42:51 -08:00
parent 430a36ac42
commit 18eaec374c
3 changed files with 40 additions and 32 deletions

View File

@@ -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

View File

@@ -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]

View File

@@ -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)