2016-01-15 22:12:57 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-01-14 21:16:42 +00:00
|
|
|
const app = require('electron').app;
|
|
|
|
const EventEmitter = require('events').EventEmitter;
|
|
|
|
const squirrelUpdate = require('./squirrel-update-win');
|
2016-02-03 19:00:34 +00:00
|
|
|
const util = require('util');
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-02-03 17:52:04 +00:00
|
|
|
function AutoUpdater() {
|
2016-02-03 18:01:01 +00:00
|
|
|
EventEmitter.call(this);
|
2016-02-03 17:52:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-02-03 19:00:34 +00:00
|
|
|
util.inherits(AutoUpdater, EventEmitter);
|
2016-01-15 22:12:57 +00:00
|
|
|
|
2016-02-03 17:52:04 +00:00
|
|
|
AutoUpdater.prototype.quitAndInstall = function() {
|
|
|
|
squirrelUpdate.processStart();
|
|
|
|
return app.quit();
|
2016-02-03 18:01:01 +00:00
|
|
|
};
|
2016-02-03 17:52:04 +00:00
|
|
|
|
|
|
|
AutoUpdater.prototype.setFeedURL = function(updateURL) {
|
|
|
|
return this.updateURL = updateURL;
|
2016-02-03 18:01:01 +00:00
|
|
|
};
|
2016-01-15 22:12:57 +00:00
|
|
|
|
2016-02-03 17:52:04 +00:00
|
|
|
AutoUpdater.prototype.checkForUpdates = function() {
|
|
|
|
if (!this.updateURL) {
|
|
|
|
return this.emitError('Update URL is not set');
|
|
|
|
}
|
|
|
|
if (!squirrelUpdate.supported()) {
|
|
|
|
return this.emitError('Can not find Squirrel');
|
|
|
|
}
|
|
|
|
this.emit('checking-for-update');
|
2016-03-10 19:54:17 +00:00
|
|
|
squirrelUpdate.download(this.updateURL, (error, update) => {
|
|
|
|
if (error != null) {
|
|
|
|
return this.emitError(error);
|
|
|
|
}
|
|
|
|
if (update == null) {
|
|
|
|
return this.emit('update-not-available');
|
|
|
|
}
|
|
|
|
this.emit('update-available');
|
|
|
|
squirrelUpdate.update(this.updateURL, (error) => {
|
|
|
|
var date, releaseNotes, version;
|
2016-02-03 17:52:04 +00:00
|
|
|
if (error != null) {
|
2016-03-10 19:54:17 +00:00
|
|
|
return this.emitError(error);
|
2016-02-03 17:52:04 +00:00
|
|
|
}
|
2016-03-10 19:54:17 +00:00
|
|
|
releaseNotes = update.releaseNotes, version = update.version;
|
|
|
|
|
|
|
|
// Following information is not available on Windows, so fake them.
|
|
|
|
date = new Date;
|
|
|
|
this.emit('update-downloaded', {}, releaseNotes, version, date, this.updateURL, () => {
|
|
|
|
this.quitAndInstall();
|
2016-02-03 17:52:04 +00:00
|
|
|
});
|
2016-03-10 19:54:17 +00:00
|
|
|
});
|
|
|
|
});
|
2016-02-03 18:01:01 +00:00
|
|
|
};
|
2016-01-15 22:12:57 +00:00
|
|
|
|
2016-02-03 17:52:04 +00:00
|
|
|
// Private: Emit both error object and message, this is to keep compatibility
|
|
|
|
// with Old APIs.
|
2016-02-03 18:01:01 +00:00
|
|
|
AutoUpdater.prototype.emitError = function(message) {
|
2016-02-03 17:52:04 +00:00
|
|
|
return this.emit('error', new Error(message), message);
|
2016-02-03 18:01:01 +00:00
|
|
|
};
|
2016-01-12 02:40:23 +00:00
|
|
|
|
|
|
|
module.exports = new AutoUpdater;
|