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-01-12 02:40:23 +00:00
|
|
|
|
2016-01-15 22:12:57 +00:00
|
|
|
class AutoUpdater extends EventEmitter {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-15 22:12:57 +00:00
|
|
|
quitAndInstall() {
|
|
|
|
squirrelUpdate.processStart();
|
|
|
|
return app.quit();
|
2016-01-15 17:57:36 +00:00
|
|
|
}
|
2016-01-15 22:12:57 +00:00
|
|
|
|
|
|
|
setFeedURL(updateURL) {
|
|
|
|
return this.updateURL = updateURL;
|
2016-01-15 17:57:36 +00:00
|
|
|
}
|
2016-01-15 22:12:57 +00:00
|
|
|
|
|
|
|
checkForUpdates() {
|
|
|
|
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');
|
|
|
|
return squirrelUpdate.download(this.updateURL, (function(_this) {
|
|
|
|
return function(error, update) {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (error != null) {
|
|
|
|
return _this.emitError(error);
|
|
|
|
}
|
2016-01-15 22:12:57 +00:00
|
|
|
if (update == null) {
|
|
|
|
return _this.emit('update-not-available');
|
|
|
|
}
|
|
|
|
_this.emit('update-available');
|
|
|
|
return squirrelUpdate.update(_this.updateURL, function(error) {
|
|
|
|
var date, releaseNotes, version;
|
|
|
|
if (error != null) {
|
|
|
|
return _this.emitError(error);
|
|
|
|
}
|
|
|
|
releaseNotes = update.releaseNotes, version = update.version;
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-15 22:12:57 +00:00
|
|
|
// Following information is not available on Windows, so fake them.
|
|
|
|
date = new Date;
|
2016-01-26 14:44:46 +00:00
|
|
|
return _this.emit('update-downloaded', {}, releaseNotes, version, date, _this.updateURL, function() {
|
2016-01-15 22:12:57 +00:00
|
|
|
return _this.quitAndInstall();
|
|
|
|
});
|
2016-01-12 02:40:23 +00:00
|
|
|
});
|
2016-01-15 22:12:57 +00:00
|
|
|
};
|
|
|
|
})(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private: Emit both error object and message, this is to keep compatibility
|
|
|
|
// with Old APIs.
|
|
|
|
emitError(message) {
|
|
|
|
return this.emit('error', new Error(message), message);
|
|
|
|
}
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
|
|
|
module.exports = new AutoUpdater;
|