feat: système de mise à jour via Gitea Releases
- Vérification auto au démarrage (après 5s) via API /releases/latest - Dialog optionnel avec snooze : 1j / 2j / 5j / 1sem / 2sem / indéfiniment - Bouton "Mises à jour" dans la topbar (ignore le snooze) - tools/release.js : script pour publier une release et uploader les binaires Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
135
main.js
135
main.js
@@ -1,4 +1,4 @@
|
||||
const { app, BrowserWindow, dialog, ipcMain } = require('electron');
|
||||
const { app, BrowserWindow, dialog, ipcMain, shell } = require('electron');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const fsp = fs.promises;
|
||||
@@ -10,6 +10,9 @@ const APP_STATE_FILE = path.join(userDataDir, 'app-state.json');
|
||||
const DROPSTATS_FILE = path.join(userDataDir, 'dropstats.json');
|
||||
const COMMUNITY_DROPSTATS_FILE = path.join(userDataDir, 'community-dropstats.json');
|
||||
const SESSION_HISTORY_FILE = path.join(userDataDir, 'session-history.json');
|
||||
const UPDATE_PREFS_FILE = path.join(userDataDir, 'update-prefs.json');
|
||||
|
||||
const RELEASES_API = 'https://git.shamiiow.com/api/v1/repos/shamiiow/farm-tracker-sao/releases/latest';
|
||||
|
||||
// ---------- Lecture / écriture disque (JSON indenté = facilement lisible) ----------
|
||||
|
||||
@@ -42,6 +45,8 @@ function loadCommunityDropStats() { return readJsonSafe(COMMUNITY_DROPSTATS_FILE
|
||||
function saveCommunityDropStats(obj) { return writeJsonAtomic(COMMUNITY_DROPSTATS_FILE, obj); }
|
||||
function loadSessionHistory() { return readJsonSafe(SESSION_HISTORY_FILE, { type: 'farm-tracker-session-history', version: 1, sessions: [] }); }
|
||||
function saveSessionHistory(obj) { return writeJsonAtomic(SESSION_HISTORY_FILE, obj); }
|
||||
function loadUpdatePrefs() { return readJsonSafe(UPDATE_PREFS_FILE, { snoozedUntil: null }); }
|
||||
function saveUpdatePrefs(obj) { return writeJsonAtomic(UPDATE_PREFS_FILE, obj); }
|
||||
|
||||
function resolveLogPath(folderPath) {
|
||||
const candidate1 = path.join(folderPath, 'logs', 'latest.log');
|
||||
@@ -51,6 +56,111 @@ function resolveLogPath(folderPath) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// ---------- Mise à jour ----------
|
||||
|
||||
function isNewerVersion(remote, current) {
|
||||
const r = remote.split('.').map(Number);
|
||||
const c = current.split('.').map(Number);
|
||||
for (let i = 0; i < 3; i++) {
|
||||
if ((r[i] || 0) > (c[i] || 0)) return true;
|
||||
if ((r[i] || 0) < (c[i] || 0)) return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const SNOOZE_OPTIONS = [
|
||||
{ label: '1 jour', days: 1 },
|
||||
{ label: '2 jours', days: 2 },
|
||||
{ label: '5 jours', days: 5 },
|
||||
{ label: '1 semaine', days: 7 },
|
||||
{ label: '2 semaines', days: 14 },
|
||||
{ label: 'Indéfiniment', days: null },
|
||||
{ label: 'Annuler', days: -1 }
|
||||
];
|
||||
|
||||
function fetchLatestRelease() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const https = require('https');
|
||||
const req = https.get(RELEASES_API, {
|
||||
timeout: 10000,
|
||||
headers: { 'User-Agent': 'farm-tracker-updater' }
|
||||
}, (res) => {
|
||||
let data = '';
|
||||
res.on('data', chunk => { data += chunk; });
|
||||
res.on('end', () => {
|
||||
try { resolve(JSON.parse(data)); }
|
||||
catch (e) { reject(new Error('Réponse invalide')); }
|
||||
});
|
||||
});
|
||||
req.on('error', reject);
|
||||
req.on('timeout', () => { req.destroy(); reject(new Error('Timeout')); });
|
||||
});
|
||||
}
|
||||
|
||||
function resolveAssetUrl(release) {
|
||||
const ext = process.platform === 'win32' ? '.exe' : '.AppImage';
|
||||
const asset = (release.assets || []).find(a => a.name.endsWith(ext));
|
||||
return asset ? asset.browser_download_url : null;
|
||||
}
|
||||
|
||||
async function showUpdateDialog(version, downloadUrl, ignoreSnooze = false) {
|
||||
const current = app.getVersion();
|
||||
if (!isNewerVersion(version, current)) return false;
|
||||
|
||||
if (!ignoreSnooze) {
|
||||
const prefs = loadUpdatePrefs();
|
||||
if (prefs.snoozedUntil === 'indefinite') return false;
|
||||
if (prefs.snoozedUntil && new Date(prefs.snoozedUntil) > new Date()) return false;
|
||||
}
|
||||
|
||||
const { response } = await dialog.showMessageBox(mainWindow, {
|
||||
type: 'info',
|
||||
title: 'Mise à jour disponible',
|
||||
message: `Version ${version} disponible`,
|
||||
detail: `Version actuelle : ${current}`,
|
||||
buttons: ['Télécharger', 'Me rappeler dans...'],
|
||||
defaultId: 0,
|
||||
cancelId: 1
|
||||
});
|
||||
|
||||
if (response === 0) {
|
||||
shell.openExternal(downloadUrl);
|
||||
return true;
|
||||
}
|
||||
|
||||
const { response: snoozeResponse } = await dialog.showMessageBox(mainWindow, {
|
||||
type: 'question',
|
||||
title: 'Me rappeler dans...',
|
||||
message: 'Ne plus afficher cette mise à jour pendant :',
|
||||
buttons: SNOOZE_OPTIONS.map(o => o.label),
|
||||
defaultId: 0,
|
||||
cancelId: SNOOZE_OPTIONS.length - 1
|
||||
});
|
||||
|
||||
const chosen = SNOOZE_OPTIONS[snoozeResponse];
|
||||
if (chosen.days === -1) return false;
|
||||
if (chosen.days === null) {
|
||||
saveUpdatePrefs({ snoozedUntil: 'indefinite' });
|
||||
} else {
|
||||
const until = new Date();
|
||||
until.setDate(until.getDate() + chosen.days);
|
||||
saveUpdatePrefs({ snoozedUntil: until.toISOString() });
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
async function checkForUpdates(ignoreSnooze = false) {
|
||||
try {
|
||||
const release = await fetchLatestRelease();
|
||||
const version = (release.tag_name || '').replace(/^v/, '');
|
||||
const downloadUrl = resolveAssetUrl(release);
|
||||
if (!version || !downloadUrl) return false;
|
||||
return await showUpdateDialog(version, downloadUrl, ignoreSnooze);
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
let mainWindow;
|
||||
|
||||
function createWindow() {
|
||||
@@ -71,7 +181,10 @@ function createWindow() {
|
||||
mainWindow.loadFile(path.join(__dirname, 'renderer', 'index.html'));
|
||||
}
|
||||
|
||||
app.whenReady().then(createWindow);
|
||||
app.whenReady().then(() => {
|
||||
createWindow();
|
||||
setTimeout(() => checkForUpdates(false), 5000);
|
||||
});
|
||||
app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit(); });
|
||||
app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) createWindow(); });
|
||||
|
||||
@@ -184,6 +297,24 @@ ipcMain.handle('get-file-paths', () => ({ dropStats: DROPSTATS_FILE, communityDr
|
||||
ipcMain.handle('load-session-history', async () => loadSessionHistory());
|
||||
ipcMain.handle('save-session-history', async (event, obj) => saveSessionHistory(obj));
|
||||
|
||||
// ---------- IPC : mise à jour ----------
|
||||
|
||||
ipcMain.handle('check-update-manual', async () => {
|
||||
try {
|
||||
const release = await fetchLatestRelease();
|
||||
const version = (release.tag_name || '').replace(/^v/, '');
|
||||
const downloadUrl = resolveAssetUrl(release);
|
||||
const current = app.getVersion();
|
||||
if (!version || !downloadUrl || !isNewerVersion(version, current)) {
|
||||
return { checked: true, hasUpdate: false, current };
|
||||
}
|
||||
await showUpdateDialog(version, downloadUrl, true);
|
||||
return { checked: true, hasUpdate: true };
|
||||
} catch (e) {
|
||||
return { checked: false, error: e.message };
|
||||
}
|
||||
});
|
||||
|
||||
// ---------- IPC : sync avec le serveur VPS ----------
|
||||
|
||||
ipcMain.handle('fetch-community-stats', async (event, { serverUrl }) => {
|
||||
|
||||
Reference in New Issue
Block a user