1
0
Fork 0
mirror of https://gitlab.com/futo-org/fcast.git synced 2025-06-24 21:25:23 +00:00

Added check for updates on startup

This commit is contained in:
Michael Hollister 2024-11-17 23:36:16 -06:00
parent e5232c1358
commit 3a9aedae31
2 changed files with 62 additions and 51 deletions

View file

@ -41,6 +41,44 @@ export default class Main {
}
}
private static async checkForUpdates(silent: boolean) {
if (Updater.updateDownloaded) {
Main.mainWindow.webContents.send("download-complete");
return;
}
try {
const updateAvailable = await Updater.checkForUpdates();
if (updateAvailable) {
Main.mainWindow.webContents.send("update-available");
}
else {
if (!silent) {
await dialog.showMessageBox({
type: 'info',
title: 'Already up-to-date',
message: 'The application is already on the latest version.',
buttons: ['OK'],
defaultId: 0
});
}
}
} catch (err) {
if (!silent) {
await dialog.showMessageBox({
type: 'error',
title: 'Failed to check for updates',
message: err,
buttons: ['OK'],
defaultId: 0
});
}
Main.logger.error('Failed to check for updates:', err);
}
}
private static createTray() {
const icon = (process.platform === 'win32') ? path.join(__dirname, 'icon.ico') : path.join(__dirname, 'icon.png');
const trayicon = nativeImage.createFromPath(icon)
@ -52,39 +90,7 @@ export default class Main {
},
{
label: 'Check for updates',
click: async () => {
if (Updater.updateDownloaded) {
Main.mainWindow.webContents.send("download-complete");
return;
}
try {
const updateAvailable = await Updater.checkForUpdates();
if (updateAvailable) {
Main.mainWindow.webContents.send("update-available");
}
else {
await dialog.showMessageBox({
type: 'info',
title: 'Already up-to-date',
message: 'The application is already on the latest version.',
buttons: ['OK'],
defaultId: 0
});
}
} catch (err) {
await dialog.showMessageBox({
type: 'error',
title: 'Failed to check for updates',
message: err,
buttons: ['OK'],
defaultId: 0
});
Main.logger.error('Failed to check for updates:', err);
}
},
click: async () => { await Main.checkForUpdates(false); },
},
{
type: 'separator',
@ -245,6 +251,10 @@ export default class Main {
defaultId: 0
});
}
if (Updater.checkForUpdatesOnStart) {
Main.checkForUpdates(true);
}
}

View file

@ -61,6 +61,24 @@ export class Updater {
public static updateError: boolean = false;
public static updateDownloaded: boolean = false;
public static updateProgress: number = 0;
public static checkForUpdatesOnStart: boolean = true;
static {
Updater.localPackageJson = JSON.parse(fs.readFileSync(path.join(Updater.appPath, './package.json'), 'utf8'));
let updaterSettings = Store.get('updater');
if (updaterSettings !== null) {
Updater.localPackageJson.channel = updaterSettings.channel === undefined ? 'stable' : updaterSettings.channel;
Updater.checkForUpdatesOnStart = updaterSettings.checkForUpdatesOnStart === undefined ? true : updaterSettings.checkForUpdatesOnStart;
}
updaterSettings = {
'channel': Updater.localPackageJson.channel,
'checkForUpdatesOnStart': Updater.checkForUpdatesOnStart,
}
Store.set('updater', updaterSettings);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private static async fetchJSON(url: string): Promise<any> {
@ -210,11 +228,7 @@ export class Updater {
}
public static getChannelVersion(): string {
if (Updater.localPackageJson === null) {
Updater.localPackageJson = JSON.parse(fs.readFileSync(path.join(Updater.appPath, './package.json'), 'utf8'));
Updater.localPackageJson.channelVersion = Updater.localPackageJson.channelVersion ? Updater.localPackageJson.channelVersion : 0
}
Updater.localPackageJson.channelVersion = Updater.localPackageJson.channelVersion ? Updater.localPackageJson.channelVersion : 0
return Updater.localPackageJson.channelVersion;
}
@ -290,23 +304,10 @@ export class Updater {
public static async checkForUpdates(): Promise<boolean> {
logger.info('Checking for updates...');
Updater.localPackageJson = JSON.parse(fs.readFileSync(path.join(Updater.appPath, './package.json'), 'utf8'));
try {
Updater.releasesJson = await Updater.fetchJSON(`${Updater.baseUrl}/releases_v${Updater.supportedReleasesJsonVersion}.json`.toString()) as ReleaseInfo;
let updaterSettings = Store.get('updater');
if (updaterSettings === null) {
updaterSettings = {
'channel': Updater.localPackageJson.channel,
}
Store.set('updater', updaterSettings);
}
else {
Updater.localPackageJson.channel = updaterSettings.channel;
}
const localChannelVersion: number = Updater.localPackageJson.channelVersion ? Updater.localPackageJson.channelVersion : 0;
const currentChannelVersion: number = Updater.releasesJson.channelCurrentVersions[Updater.localPackageJson.channel] ? Updater.releasesJson.channelCurrentVersions[Updater.localPackageJson.channel] : 0;
logger.info('Update check', { channel: Updater.localPackageJson.channel, channel_version: localChannelVersion, localVersion: Updater.localPackageJson.version,