2020-07-13 16:58:49 +00:00
|
|
|
import { app } from 'electron/main';
|
2020-07-01 19:27:12 +00:00
|
|
|
import { EventEmitter } from 'events';
|
2020-07-13 16:58:49 +00:00
|
|
|
import * as squirrelUpdate from '@electron/internal/browser/api/auto-updater/squirrel-update-win';
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2023-10-25 18:02:15 +00:00
|
|
|
class AutoUpdater extends EventEmitter implements Electron.AutoUpdater {
|
2020-07-01 19:27:12 +00:00
|
|
|
updateAvailable: boolean = false;
|
|
|
|
updateURL: string | null = null;
|
|
|
|
|
2016-07-26 01:40:55 +00:00
|
|
|
quitAndInstall () {
|
|
|
|
if (!this.updateAvailable) {
|
2020-07-01 19:27:12 +00:00
|
|
|
return this.emitError(new Error('No update available, can\'t quit and install'));
|
2016-07-26 01:40:55 +00:00
|
|
|
}
|
|
|
|
squirrelUpdate.processStart();
|
2016-08-08 20:06:14 +00:00
|
|
|
app.quit();
|
2016-04-25 15:35:34 +00:00
|
|
|
}
|
2016-07-14 12:04:48 +00:00
|
|
|
|
2016-07-26 01:40:55 +00:00
|
|
|
getFeedURL () {
|
2023-10-25 18:02:15 +00:00
|
|
|
return this.updateURL ?? '';
|
2016-02-03 17:52:04 +00:00
|
|
|
}
|
2016-07-26 01:40:55 +00:00
|
|
|
|
2020-07-01 19:27:12 +00:00
|
|
|
setFeedURL (options: { url: string } | string) {
|
|
|
|
let updateURL: string;
|
2018-02-16 05:02:10 +00:00
|
|
|
if (typeof options === 'object') {
|
|
|
|
if (typeof options.url === 'string') {
|
|
|
|
updateURL = options.url;
|
|
|
|
} else {
|
2023-07-25 16:08:46 +00:00
|
|
|
throw new TypeError('Expected options object to contain a \'url\' string property in setFeedUrl call');
|
2018-02-16 05:02:10 +00:00
|
|
|
}
|
|
|
|
} else if (typeof options === 'string') {
|
2018-02-15 02:58:59 +00:00
|
|
|
updateURL = options;
|
|
|
|
} else {
|
2023-07-25 16:08:46 +00:00
|
|
|
throw new TypeError('Expected an options object with a \'url\' property to be provided');
|
2018-02-15 02:58:59 +00:00
|
|
|
}
|
2016-07-26 01:40:55 +00:00
|
|
|
this.updateURL = updateURL;
|
2016-02-03 17:52:04 +00:00
|
|
|
}
|
2016-07-26 01:40:55 +00:00
|
|
|
|
2023-11-07 22:55:22 +00:00
|
|
|
async checkForUpdates () {
|
2020-07-01 19:27:12 +00:00
|
|
|
const url = this.updateURL;
|
|
|
|
if (!url) {
|
|
|
|
return this.emitError(new Error('Update URL is not set'));
|
2016-03-10 19:54:17 +00:00
|
|
|
}
|
2016-07-26 01:40:55 +00:00
|
|
|
if (!squirrelUpdate.supported()) {
|
2020-07-01 19:27:12 +00:00
|
|
|
return this.emitError(new Error('Can not find Squirrel'));
|
2016-03-10 19:54:17 +00:00
|
|
|
}
|
2016-07-26 01:40:55 +00:00
|
|
|
this.emit('checking-for-update');
|
2023-11-07 22:55:22 +00:00
|
|
|
try {
|
|
|
|
const update = await squirrelUpdate.checkForUpdate(url);
|
2016-07-26 01:40:55 +00:00
|
|
|
if (update == null) {
|
|
|
|
return this.emit('update-not-available');
|
|
|
|
}
|
|
|
|
this.updateAvailable = true;
|
|
|
|
this.emit('update-available');
|
2023-11-07 22:55:22 +00:00
|
|
|
|
|
|
|
await squirrelUpdate.update(url);
|
|
|
|
const { releaseNotes, version } = update;
|
|
|
|
// Date is not available on Windows, so fake it.
|
|
|
|
const date = new Date();
|
|
|
|
this.emit('update-downloaded', {}, releaseNotes, version, date, this.updateURL, () => {
|
|
|
|
this.quitAndInstall();
|
2016-03-24 20:15:04 +00:00
|
|
|
});
|
2023-11-07 22:55:22 +00:00
|
|
|
} catch (error) {
|
|
|
|
this.emitError(error as Error);
|
|
|
|
}
|
2016-07-26 01:40:55 +00:00
|
|
|
}
|
2016-01-15 22:12:57 +00:00
|
|
|
|
2016-07-26 01:40:55 +00:00
|
|
|
// Private: Emit both error object and message, this is to keep compatibility
|
|
|
|
// with Old APIs.
|
2020-07-01 19:27:12 +00:00
|
|
|
emitError (error: Error) {
|
|
|
|
this.emit('error', error, error.message);
|
2016-07-26 01:40:55 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-07-01 19:27:12 +00:00
|
|
|
export default new AutoUpdater();
|