2016-03-24 20:15:04 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const spawn = require('child_process').spawn
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// i.e. my-app/app-0.1.13/
|
2016-03-24 20:15:04 +00:00
|
|
|
const appFolder = path.dirname(process.execPath)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// i.e. my-app/Update.exe
|
2016-03-24 20:15:04 +00:00
|
|
|
const updateExe = path.resolve(appFolder, '..', 'Update.exe')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
const exeName = path.basename(process.execPath)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:44:21 +00:00
|
|
|
// Spawn a command and invoke the callback when it completes with an error
|
|
|
|
// and the output from standard out.
|
2016-03-24 20:15:04 +00:00
|
|
|
var spawnUpdate = function (args, detached, callback) {
|
|
|
|
var error, errorEmitted, spawnedProcess, stderr, stdout
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
|
|
|
spawnedProcess = spawn(updateExe, args, {
|
|
|
|
detached: detached
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
} catch (error1) {
|
2016-03-24 20:15:04 +00:00
|
|
|
error = error1
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Shouldn't happen, but still guard it.
|
2016-03-24 20:15:04 +00:00
|
|
|
process.nextTick(function () {
|
|
|
|
return callback(error)
|
|
|
|
})
|
|
|
|
return
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
stdout = ''
|
|
|
|
stderr = ''
|
|
|
|
spawnedProcess.stdout.on('data', function (data) {
|
|
|
|
return stdout += data
|
|
|
|
})
|
|
|
|
spawnedProcess.stderr.on('data', function (data) {
|
|
|
|
return stderr += data
|
|
|
|
})
|
|
|
|
errorEmitted = false
|
|
|
|
spawnedProcess.on('error', function (error) {
|
|
|
|
errorEmitted = true
|
|
|
|
return callback(error)
|
|
|
|
})
|
|
|
|
return spawnedProcess.on('exit', function (code, signal) {
|
2016-01-14 18:35:29 +00:00
|
|
|
// We may have already emitted an error.
|
2016-01-12 02:40:23 +00:00
|
|
|
if (errorEmitted) {
|
2016-03-24 20:15:04 +00:00
|
|
|
return
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Process terminated with error.
|
2016-01-12 02:40:23 +00:00
|
|
|
if (code !== 0) {
|
2016-03-24 20:15:04 +00:00
|
|
|
return callback('Command failed: ' + (signal != null ? signal : code) + '\n' + stderr)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Success.
|
2016-03-24 20:15:04 +00:00
|
|
|
return callback(null, stdout)
|
|
|
|
})
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Start an instance of the installed app.
|
2016-03-24 20:15:04 +00:00
|
|
|
exports.processStart = function () {
|
|
|
|
return spawnUpdate(['--processStart', exeName], true, function () {})
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Download the releases specified by the URL and write new results to stdout.
|
2016-03-24 20:15:04 +00:00
|
|
|
exports.download = function (updateURL, callback) {
|
|
|
|
return spawnUpdate(['--download', updateURL], false, function (error, stdout) {
|
|
|
|
var json, ref, ref1, update
|
2016-01-12 02:40:23 +00:00
|
|
|
if (error != null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
return callback(error)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
try {
|
2016-01-14 18:35:29 +00:00
|
|
|
// Last line of output is the JSON details about the releases
|
2016-03-24 20:15:04 +00:00
|
|
|
json = stdout.trim().split('\n').pop()
|
|
|
|
update = (ref = JSON.parse(json)) != null ? (ref1 = ref.releasesToApply) != null ? typeof ref1.pop === 'function' ? ref1.pop() : void 0 : void 0 : void 0
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (jsonError) {
|
2016-03-24 20:15:04 +00:00
|
|
|
return callback('Invalid result:\n' + stdout)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
return callback(null, update)
|
|
|
|
})
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Update the application to the latest remote version specified by URL.
|
2016-03-24 20:15:04 +00:00
|
|
|
exports.update = function (updateURL, callback) {
|
|
|
|
return spawnUpdate(['--update', updateURL], false, callback)
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Is the Update.exe installed with the current application?
|
2016-03-24 20:15:04 +00:00
|
|
|
exports.supported = function () {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2016-03-24 20:15:04 +00:00
|
|
|
fs.accessSync(updateExe, fs.R_OK)
|
|
|
|
return true
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2016-03-24 20:15:04 +00:00
|
|
|
return false
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|