Add auto updater

This commit is contained in:
2026-03-10 23:38:57 +01:00
parent e8e5c24600
commit c3fbd7d4fe
20 changed files with 2272 additions and 14 deletions

View File

@@ -2,8 +2,13 @@ import { app } from 'electron';
import * as fs from 'fs';
import * as path from 'path';
export type AutoUpdateMode = 'auto' | 'off' | 'version';
export interface DesktopSettings {
autoUpdateMode: AutoUpdateMode;
hardwareAcceleration: boolean;
manifestUrls: string[];
preferredVersion: string | null;
vaapiVideoEncode: boolean;
}
@@ -13,10 +18,45 @@ export interface DesktopSettingsSnapshot extends DesktopSettings {
}
const DEFAULT_DESKTOP_SETTINGS: DesktopSettings = {
autoUpdateMode: 'auto',
hardwareAcceleration: true,
manifestUrls: [],
preferredVersion: null,
vaapiVideoEncode: false
};
function normalizeAutoUpdateMode(value: unknown): AutoUpdateMode {
return value === 'off' || value === 'version' ? value : 'auto';
}
function normalizePreferredVersion(value: unknown): string | null {
return typeof value === 'string' && value.trim().length > 0 ? value.trim() : null;
}
function normalizeManifestUrls(value: unknown): string[] {
if (!Array.isArray(value)) {
return [];
}
const manifestUrls: string[] = [];
for (const entry of value) {
if (typeof entry !== 'string') {
continue;
}
const nextUrl = entry.trim();
if (!nextUrl || manifestUrls.includes(nextUrl)) {
continue;
}
manifestUrls.push(nextUrl);
}
return manifestUrls;
}
export function getDesktopSettingsSnapshot(): DesktopSettingsSnapshot {
const storedSettings = readDesktopSettings();
const runtimeHardwareAcceleration = app.isHardwareAccelerationEnabled();
@@ -40,12 +80,15 @@ export function readDesktopSettings(): DesktopSettings {
const parsed = JSON.parse(raw) as Partial<DesktopSettings>;
return {
autoUpdateMode: normalizeAutoUpdateMode(parsed.autoUpdateMode),
vaapiVideoEncode: typeof parsed.vaapiVideoEncode === 'boolean'
? parsed.vaapiVideoEncode
: DEFAULT_DESKTOP_SETTINGS.vaapiVideoEncode,
hardwareAcceleration: typeof parsed.hardwareAcceleration === 'boolean'
? parsed.hardwareAcceleration
: DEFAULT_DESKTOP_SETTINGS.hardwareAcceleration
: DEFAULT_DESKTOP_SETTINGS.hardwareAcceleration,
manifestUrls: normalizeManifestUrls(parsed.manifestUrls),
preferredVersion: normalizePreferredVersion(parsed.preferredVersion)
};
} catch {
return { ...DEFAULT_DESKTOP_SETTINGS };
@@ -53,10 +96,21 @@ export function readDesktopSettings(): DesktopSettings {
}
export function updateDesktopSettings(patch: Partial<DesktopSettings>): DesktopSettingsSnapshot {
const nextSettings: DesktopSettings = {
const mergedSettings = {
...readDesktopSettings(),
...patch
};
const nextSettings: DesktopSettings = {
autoUpdateMode: normalizeAutoUpdateMode(mergedSettings.autoUpdateMode),
hardwareAcceleration: typeof mergedSettings.hardwareAcceleration === 'boolean'
? mergedSettings.hardwareAcceleration
: DEFAULT_DESKTOP_SETTINGS.hardwareAcceleration,
manifestUrls: normalizeManifestUrls(mergedSettings.manifestUrls),
preferredVersion: normalizePreferredVersion(mergedSettings.preferredVersion),
vaapiVideoEncode: typeof mergedSettings.vaapiVideoEncode === 'boolean'
? mergedSettings.vaapiVideoEncode
: DEFAULT_DESKTOP_SETTINGS.vaapiVideoEncode
};
const filePath = getDesktopSettingsPath();
fs.mkdirSync(path.dirname(filePath), { recursive: true });