asarfuckery/electronasar/canary/browser/api/auto-updater/squirrel-update-win.js

107 lines
4 KiB
JavaScript
Raw Normal View History

2019-05-07 17:59:15 +00:00
'use strict';
const fs = require('fs');
const path = require('path');
const spawn = require('child_process').spawn;
2019-02-06 20:27:58 +00:00
// i.e. my-app/app-0.1.13/
2019-05-07 17:59:15 +00:00
const appFolder = path.dirname(process.execPath);
2019-02-06 20:27:58 +00:00
// i.e. my-app/Update.exe
2019-05-07 17:59:15 +00:00
const updateExe = path.resolve(appFolder, '..', 'Update.exe');
const exeName = path.basename(process.execPath);
let spawnedArgs = [];
let spawnedProcess;
const isSameArgs = (args) => args.length === spawnedArgs.length && args.every((e, i) => e === spawnedArgs[i]);
2019-02-06 20:27:58 +00:00
// Spawn a command and invoke the callback when it completes with an error
// and the output from standard out.
const spawnUpdate = function (args, detached, callback) {
2019-05-07 17:59:15 +00:00
let error, errorEmitted, stderr, stdout;
try {
// Ensure we don't spawn multiple squirrel processes
// Process spawned, same args: Attach events to alread running process
// Process spawned, different args: Return with error
// No process spawned: Spawn new process
if (spawnedProcess && !isSameArgs(args)) {
// Disabled for backwards compatibility:
// eslint-disable-next-line standard/no-callback-literal
return callback(`AutoUpdater process with arguments ${args} is already running`);
}
else if (!spawnedProcess) {
spawnedProcess = spawn(updateExe, args, {
detached: detached,
windowsHide: true
});
spawnedArgs = args || [];
}
2019-02-06 20:27:58 +00:00
}
2019-05-07 17:59:15 +00:00
catch (error1) {
error = error1;
// Shouldn't happen, but still guard it.
process.nextTick(function () {
return callback(error);
});
return;
2019-02-06 20:27:58 +00:00
}
2019-05-07 17:59:15 +00:00
stdout = '';
stderr = '';
spawnedProcess.stdout.on('data', (data) => { stdout += data; });
spawnedProcess.stderr.on('data', (data) => { stderr += data; });
errorEmitted = false;
spawnedProcess.on('error', (error) => {
errorEmitted = true;
callback(error);
});
return spawnedProcess.on('exit', function (code, signal) {
spawnedProcess = undefined;
spawnedArgs = [];
// We may have already emitted an error.
if (errorEmitted) {
return;
}
// Process terminated with error.
if (code !== 0) {
// Disabled for backwards compatibility:
// eslint-disable-next-line standard/no-callback-literal
return callback(`Command failed: ${signal != null ? signal : code}\n${stderr}`);
}
// Success.
callback(null, stdout);
});
};
2019-02-06 20:27:58 +00:00
// Start an instance of the installed app.
exports.processStart = function () {
2019-05-07 17:59:15 +00:00
return spawnUpdate(['--processStartAndWait', exeName], true, function () { });
};
2019-02-06 20:27:58 +00:00
// Download the releases specified by the URL and write new results to stdout.
exports.checkForUpdate = function (updateURL, callback) {
2019-05-07 17:59:15 +00:00
return spawnUpdate(['--checkForUpdate', updateURL], false, function (error, stdout) {
let ref, ref1, update;
if (error != null) {
return callback(error);
}
try {
// Last line of output is the JSON details about the releases
const 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;
}
catch (_a) {
// Disabled for backwards compatibility:
// eslint-disable-next-line standard/no-callback-literal
return callback(`Invalid result:\n${stdout}`);
}
return callback(null, update);
});
};
2019-02-06 20:27:58 +00:00
// Update the application to the latest remote version specified by URL.
exports.update = function (updateURL, callback) {
2019-05-07 17:59:15 +00:00
return spawnUpdate(['--update', updateURL], false, callback);
};
2019-02-06 20:27:58 +00:00
// Is the Update.exe installed with the current application?
exports.supported = function () {
2019-05-07 17:59:15 +00:00
try {
fs.accessSync(updateExe, fs.R_OK);
return true;
}
catch (_a) {
return false;
}
};
//# sourceMappingURL=squirrel-update-win.js.map