Merge pull request #4350 from atom/dont-use-es6-class-for-auto-updater

Don't use ES6 class for auto updater
This commit is contained in:
Kevin Sawicki 2016-02-11 11:27:10 -08:00
commit 38e8208639
3 changed files with 86 additions and 46 deletions

View file

@ -3,60 +3,61 @@
const app = require('electron').app;
const EventEmitter = require('events').EventEmitter;
const squirrelUpdate = require('./squirrel-update-win');
const util = require('util');
class AutoUpdater extends EventEmitter {
constructor() {
super();
function AutoUpdater() {
EventEmitter.call(this);
}
util.inherits(AutoUpdater, EventEmitter);
AutoUpdater.prototype.quitAndInstall = function() {
squirrelUpdate.processStart();
return app.quit();
};
AutoUpdater.prototype.setFeedURL = function(updateURL) {
return this.updateURL = updateURL;
};
AutoUpdater.prototype.checkForUpdates = function() {
if (!this.updateURL) {
return this.emitError('Update URL is not set');
}
quitAndInstall() {
squirrelUpdate.processStart();
return app.quit();
if (!squirrelUpdate.supported()) {
return this.emitError('Can not find Squirrel');
}
setFeedURL(updateURL) {
return this.updateURL = updateURL;
}
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) {
this.emit('checking-for-update');
return squirrelUpdate.download(this.updateURL, (function(_this) {
return function(error, update) {
if (error != null) {
return _this.emitError(error);
}
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);
}
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;
releaseNotes = update.releaseNotes, version = update.version;
// Following information is not available on Windows, so fake them.
date = new Date;
return _this.emit('update-downloaded', {}, releaseNotes, version, date, _this.updateURL, function() {
return _this.quitAndInstall();
});
// Following information is not available on Windows, so fake them.
date = new Date;
return _this.emit('update-downloaded', {}, releaseNotes, version, date, _this.updateURL, function() {
return _this.quitAndInstall();
});
};
})(this));
}
});
};
})(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);
}
}
// Private: Emit both error object and message, this is to keep compatibility
// with Old APIs.
AutoUpdater.prototype.emitError = function(message) {
return this.emit('error', new Error(message), message);
};
module.exports = new AutoUpdater;

View file

@ -0,0 +1,34 @@
const assert = require('assert');
const autoUpdater = require('electron').remote.autoUpdater;
const ipcRenderer = require('electron').ipcRenderer;
describe('autoUpdater module', function() {
describe('checkForUpdates', function() {
it('emits an error on Windows when called the feed URL is not set', function (done) {
if (process.platform !== 'win32') {
return done();
}
ipcRenderer.once('auto-updater-error', function(event, message) {
assert.equal(message, 'Update URL is not set');
done();
});
autoUpdater.setFeedURL('');
autoUpdater.checkForUpdates();
});
});
describe('setFeedURL', function() {
it('emits an error on Mac OS X when the application is unsigned', function (done) {
if (process.platform !== 'darwin') {
return done();
}
ipcRenderer.once('auto-updater-error', function(event, message) {
assert.equal(message, 'Could not get code signature for running application');
done();
});
autoUpdater.setFeedURL('');
});
});
});

View file

@ -72,6 +72,11 @@ app.on('ready', function() {
// Test if using protocol module would crash.
electron.protocol.registerStringProtocol('test-if-crashes', function() {});
// Send auto updater errors to window to be verified in specs
electron.autoUpdater.on('error', function (error) {
window.send('auto-updater-error', error.message)
});
window = new BrowserWindow({
title: 'Electron Tests',
show: false,