add auto update for android
This commit is contained in:
55
client/src/utils/autoUpdate.js
Normal file
55
client/src/utils/autoUpdate.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import { Capacitor } from '@capacitor/core';
|
||||
import { App } from '@capacitor/app';
|
||||
|
||||
export async function checkForUpdates() {
|
||||
if (!Capacitor.isNativePlatform()) {
|
||||
console.log('Auto-update skipped (running on web)');
|
||||
return;
|
||||
}
|
||||
|
||||
function isNewer(current, target) {
|
||||
const c = current.replace(/^v/, '').split('.').map(Number);
|
||||
const t = target.replace(/^v/, '').split('.').map(Number);
|
||||
|
||||
for (let i = 0; i < 3; i++) {
|
||||
if (t[i] > c[i]) return true;
|
||||
if (t[i] < c[i]) return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const { CapacitorUpdater } = await import('@capgo/capacitor-updater');
|
||||
|
||||
await CapacitorUpdater.notifyAppReady();
|
||||
|
||||
const GITEA_API_URL = 'https://git.crylia.de/Crylia/zen-kanji/releases/latest';
|
||||
|
||||
const appInfo = await App.getInfo();
|
||||
const currentVersion = appInfo.version;
|
||||
|
||||
const response = await fetch(GITEA_API_URL);
|
||||
if (!response.ok) return;
|
||||
|
||||
const release = await response.json();
|
||||
const latestTag = release.tag_name;
|
||||
|
||||
if (!isNewer(currentVersion, latestTag)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const asset = release.assets.find((a) => a.name === 'dist.zip');
|
||||
if (!asset) return;
|
||||
|
||||
console.log(`Downloading update: ${latestTag}`);
|
||||
|
||||
const version = await CapacitorUpdater.download({
|
||||
url: asset.browser_download_url,
|
||||
version: latestTag,
|
||||
});
|
||||
|
||||
await CapacitorUpdater.set(version);
|
||||
} catch (error) {
|
||||
console.error('Auto-update failed:', error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user