Files
DndGamePlayer/app/main/update/installAutoUpdater.ts
T
2026-05-11 22:20:14 +08:00

72 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { app, dialog } from 'electron';
import { autoUpdater } from 'electron-updater';
import { addLicenseChangeListener } from '../license/licenseService';
import type { LicenseService } from '../license/licenseService';
const STARTUP_CHECK_DELAY_MS = 12_000;
/** Не дёргать сервер чаще (смена лицензии / повторные emit). */
const RE_CHECK_COOLDOWN_MS = 30_000;
let lastCheckAt = 0;
function isLicensedForUpdates(licenseService: LicenseService): boolean {
const snap = licenseService.getStatusSync();
return snap.active;
}
function maybeCheckForUpdates(licenseService: LicenseService, ignoreCooldown: boolean): void {
if (!app.isPackaged) return;
if (!isLicensedForUpdates(licenseService)) return;
const now = Date.now();
if (!ignoreCooldown && now - lastCheckAt < RE_CHECK_COOLDOWN_MS) return;
lastCheckAt = now;
void autoUpdater.checkForUpdates().catch(() => undefined);
}
/**
* Проверка обновлений: только упакованное приложение, только при активной лицензии.
* Канал и URL задаются при сборке (`publish` → `app-update.yml` внутри установки).
*/
export function installAutoUpdater(licenseService: LicenseService): void {
if (!app.isPackaged) return;
const feedOverride = process.env.DND_UPDATE_FEED_URL?.trim();
if (feedOverride) {
const url = feedOverride.endsWith('/') ? feedOverride : `${feedOverride}/`;
autoUpdater.setFeedURL({ provider: 'generic', url });
}
autoUpdater.autoDownload = true;
autoUpdater.autoInstallOnAppQuit = true;
autoUpdater.on('update-downloaded', (info) => {
void dialog
.showMessageBox({
type: 'info',
title: 'DNDGamePlayer',
message: `Доступна новая версия ${info.version}. Установить и перезапустить?`,
buttons: ['Перезапустить сейчас', 'Позже'],
defaultId: 0,
cancelId: 1,
})
.then((r) => {
if (r.response === 0) {
autoUpdater.quitAndInstall(false, true);
}
});
});
autoUpdater.on('error', () => {
/* без console: в production main минифицируется с drop console */
});
addLicenseChangeListener(() => {
maybeCheckForUpdates(licenseService, false);
});
setTimeout(() => {
maybeCheckForUpdates(licenseService, true);
}, STARTUP_CHECK_DELAY_MS);
}