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:
commit
38e8208639
3 changed files with 86 additions and 46 deletions
|
@ -3,60 +3,61 @@
|
||||||
const app = require('electron').app;
|
const app = require('electron').app;
|
||||||
const EventEmitter = require('events').EventEmitter;
|
const EventEmitter = require('events').EventEmitter;
|
||||||
const squirrelUpdate = require('./squirrel-update-win');
|
const squirrelUpdate = require('./squirrel-update-win');
|
||||||
|
const util = require('util');
|
||||||
|
|
||||||
class AutoUpdater extends EventEmitter {
|
function AutoUpdater() {
|
||||||
constructor() {
|
EventEmitter.call(this);
|
||||||
super();
|
}
|
||||||
|
|
||||||
|
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');
|
||||||
}
|
}
|
||||||
|
if (!squirrelUpdate.supported()) {
|
||||||
quitAndInstall() {
|
return this.emitError('Can not find Squirrel');
|
||||||
squirrelUpdate.processStart();
|
|
||||||
return app.quit();
|
|
||||||
}
|
}
|
||||||
|
this.emit('checking-for-update');
|
||||||
setFeedURL(updateURL) {
|
return squirrelUpdate.download(this.updateURL, (function(_this) {
|
||||||
return this.updateURL = updateURL;
|
return function(error, update) {
|
||||||
}
|
if (error != null) {
|
||||||
|
return _this.emitError(error);
|
||||||
checkForUpdates() {
|
}
|
||||||
if (!this.updateURL) {
|
if (update == null) {
|
||||||
return this.emitError('Update URL is not set');
|
return _this.emit('update-not-available');
|
||||||
}
|
}
|
||||||
if (!squirrelUpdate.supported()) {
|
_this.emit('update-available');
|
||||||
return this.emitError('Can not find Squirrel');
|
return squirrelUpdate.update(_this.updateURL, function(error) {
|
||||||
}
|
var date, releaseNotes, version;
|
||||||
this.emit('checking-for-update');
|
|
||||||
return squirrelUpdate.download(this.updateURL, (function(_this) {
|
|
||||||
return function(error, update) {
|
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
return _this.emitError(error);
|
return _this.emitError(error);
|
||||||
}
|
}
|
||||||
if (update == null) {
|
releaseNotes = update.releaseNotes, version = update.version;
|
||||||
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;
|
|
||||||
|
|
||||||
// Following information is not available on Windows, so fake them.
|
// Following information is not available on Windows, so fake them.
|
||||||
date = new Date;
|
date = new Date;
|
||||||
return _this.emit('update-downloaded', {}, releaseNotes, version, date, _this.updateURL, function() {
|
return _this.emit('update-downloaded', {}, releaseNotes, version, date, _this.updateURL, function() {
|
||||||
return _this.quitAndInstall();
|
return _this.quitAndInstall();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
});
|
||||||
})(this));
|
};
|
||||||
}
|
})(this));
|
||||||
|
};
|
||||||
|
|
||||||
// Private: Emit both error object and message, this is to keep compatibility
|
// Private: Emit both error object and message, this is to keep compatibility
|
||||||
// with Old APIs.
|
// with Old APIs.
|
||||||
emitError(message) {
|
AutoUpdater.prototype.emitError = function(message) {
|
||||||
return this.emit('error', new Error(message), message);
|
return this.emit('error', new Error(message), message);
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = new AutoUpdater;
|
module.exports = new AutoUpdater;
|
||||||
|
|
34
spec/api-auto-updater-spec.js
Normal file
34
spec/api-auto-updater-spec.js
Normal 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('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -72,6 +72,11 @@ app.on('ready', function() {
|
||||||
// Test if using protocol module would crash.
|
// Test if using protocol module would crash.
|
||||||
electron.protocol.registerStringProtocol('test-if-crashes', function() {});
|
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({
|
window = new BrowserWindow({
|
||||||
title: 'Electron Tests',
|
title: 'Electron Tests',
|
||||||
show: false,
|
show: false,
|
||||||
|
|
Loading…
Reference in a new issue