electron/lib/browser/api/auto-updater/auto-updater-win.js

75 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-03-20 13:28:31 -07:00
'use strict';
2016-01-15 14:12:57 -08:00
2020-03-20 13:28:31 -07:00
const { app } = require('electron');
const { EventEmitter } = require('events');
const squirrelUpdate = require('@electron/internal/browser/api/auto-updater/squirrel-update-win');
2016-01-11 18:40:23 -08:00
2016-07-26 11:40:55 +10:00
class AutoUpdater extends EventEmitter {
quitAndInstall () {
if (!this.updateAvailable) {
2020-03-20 13:28:31 -07:00
return this.emitError('No update available, can\'t quit and install');
2016-07-26 11:40:55 +10:00
}
2020-03-20 13:28:31 -07:00
squirrelUpdate.processStart();
app.quit();
}
2016-07-26 11:40:55 +10:00
getFeedURL () {
2020-03-20 13:28:31 -07:00
return this.updateURL;
}
2016-07-26 11:40:55 +10:00
setFeedURL (options) {
2020-03-20 13:28:31 -07:00
let updateURL;
2018-02-16 16:02:10 +11:00
if (typeof options === 'object') {
if (typeof options.url === 'string') {
2020-03-20 13:28:31 -07:00
updateURL = options.url;
2018-02-16 16:02:10 +11:00
} else {
2020-03-20 13:28:31 -07:00
throw new Error('Expected options object to contain a \'url\' string property in setFeedUrl call');
2018-02-16 16:02:10 +11:00
}
} else if (typeof options === 'string') {
2020-03-20 13:28:31 -07:00
updateURL = options;
} else {
2020-03-20 13:28:31 -07:00
throw new Error('Expected an options object with a \'url\' property to be provided');
}
2020-03-20 13:28:31 -07:00
this.updateURL = updateURL;
}
2016-07-26 11:40:55 +10:00
checkForUpdates () {
if (!this.updateURL) {
2020-03-20 13:28:31 -07:00
return this.emitError('Update URL is not set');
}
2016-07-26 11:40:55 +10:00
if (!squirrelUpdate.supported()) {
2020-03-20 13:28:31 -07:00
return this.emitError('Can not find Squirrel');
}
2020-03-20 13:28:31 -07:00
this.emit('checking-for-update');
squirrelUpdate.checkForUpdate(this.updateURL, (error, update) => {
if (error != null) {
2020-03-20 13:28:31 -07:00
return this.emitError(error);
}
2016-07-26 11:40:55 +10:00
if (update == null) {
2020-03-20 13:28:31 -07:00
return this.emit('update-not-available');
2016-07-26 11:40:55 +10:00
}
2020-03-20 13:28:31 -07:00
this.updateAvailable = true;
this.emit('update-available');
2016-07-26 11:40:55 +10:00
squirrelUpdate.update(this.updateURL, (error) => {
if (error != null) {
2020-03-20 13:28:31 -07:00
return this.emitError(error);
2016-07-26 11:40:55 +10:00
}
2020-03-20 13:28:31 -07:00
const { releaseNotes, version } = update;
2016-08-08 13:06:14 -07:00
// Date is not available on Windows, so fake it.
2020-03-20 13:28:31 -07:00
const date = new Date();
2016-07-26 11:40:55 +10:00
this.emit('update-downloaded', {}, releaseNotes, version, date, this.updateURL, () => {
2020-03-20 13:28:31 -07:00
this.quitAndInstall();
});
});
});
2016-07-26 11:40:55 +10:00
}
2016-01-15 14:12:57 -08:00
2016-07-26 11:40:55 +10:00
// Private: Emit both error object and message, this is to keep compatibility
// with Old APIs.
emitError (message) {
2020-03-20 13:28:31 -07:00
this.emit('error', new Error(message), message);
2016-07-26 11:40:55 +10:00
}
}
2016-01-11 18:40:23 -08:00
2020-03-20 13:28:31 -07:00
module.exports = new AutoUpdater();