feat(update-center): 实现集中式软件更新中心功能

新增更新中心模块,支持管理 APM 和传统 deb 软件更新任务
- 添加更新任务队列管理、状态跟踪和日志记录功能
- 实现更新项忽略配置持久化存储
- 新增更新确认对话框和迁移提示
- 优化主窗口关闭时的任务保护机制
- 添加单元测试覆盖核心逻辑
This commit is contained in:
2026-04-09 08:19:51 +08:00
parent 97bb8e5f59
commit 0b17ada45a
37 changed files with 6389 additions and 342 deletions

View File

@@ -18,6 +18,11 @@ import { handleCommandLine } from "./deeplink.js";
import { isLoaded } from "../global.js";
import { tasks } from "./backend/install-manager.js";
import { sendTelemetryOnce } from "./backend/telemetry.js";
import { initializeUpdateCenter } from "./backend/update-center/index.js";
import {
getMainWindowCloseAction,
type MainWindowCloseGuardState,
} from "./window-close-guard.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
process.env.APP_ROOT = path.join(__dirname, "../..");
@@ -81,6 +86,7 @@ if (!app.requestSingleInstanceLock()) {
}
let win: BrowserWindow | null = null;
let allowAppExit = false;
const preload = path.join(__dirname, "../preload/index.mjs");
const indexHtml = path.join(RENDERER_DIST, "index.html");
@@ -107,6 +113,44 @@ ipcMain.handle("get-store-filter", (): "spark" | "apm" | "both" =>
ipcMain.handle("get-app-version", (): string => getAppVersion());
const getMainWindowCloseGuardState = (): MainWindowCloseGuardState => ({
installTaskCount: tasks.size,
hasRunningUpdateCenterTasks:
initializeUpdateCenter().getState().hasRunningTasks,
});
const applyMainWindowCloseAction = (): void => {
if (!win) {
return;
}
const action = getMainWindowCloseAction(getMainWindowCloseGuardState());
if (action === "hide") {
win.hide();
win.setSkipTaskbar(true);
return;
}
win.destroy();
};
const requestApplicationExit = (): void => {
if (!win) {
allowAppExit = true;
app.quit();
return;
}
if (getMainWindowCloseAction(getMainWindowCloseGuardState()) === "hide") {
win.hide();
win.setSkipTaskbar(true);
return;
}
allowAppExit = true;
app.quit();
};
async function createWindow() {
win = new BrowserWindow({
title: "星火应用商店",
@@ -148,16 +192,13 @@ async function createWindow() {
// win.webContents.on('will-navigate', (event, url) => { }) #344
win.on("close", (event) => {
if (allowAppExit) {
return;
}
// 截获 close 默认行为
event.preventDefault();
// 点击关闭时触发close事件我们按照之前的思路在关闭时隐藏窗口隐藏任务栏窗口
if (tasks.size > 0) {
win.hide();
win.setSkipTaskbar(true);
} else {
// 如果没有下载任务,才允许关闭窗口
win.destroy();
}
applyMainWindowCloseAction();
});
}
@@ -173,26 +214,6 @@ ipcMain.on("set-theme-source", (event, theme: "system" | "light" | "dark") => {
nativeTheme.themeSource = theme;
});
// 启动系统更新工具(使用 pkexec 提升权限)
ipcMain.handle("run-update-tool", async () => {
try {
const { spawn } = await import("node:child_process");
const pkexecPath = "/usr/bin/pkexec";
const args = ["spark-update-tool"];
const child = spawn(pkexecPath, args, {
detached: true,
stdio: "ignore",
});
// 让子进程在后台运行且不影响主进程退出
child.unref();
logger.info("Launched pkexec spark-update-tool");
return { success: true };
} catch (err) {
logger.error({ err }, "Failed to launch spark-update-tool");
return { success: false, message: (err as Error)?.message || String(err) };
}
});
// 启动安装设置脚本(可能需要提升权限)
ipcMain.handle("open-install-settings", async () => {
try {
@@ -220,12 +241,14 @@ app.whenReady().then(() => {
});
createWindow();
handleCommandLine(process.argv);
initializeUpdateCenter();
// 启动后执行一次遥测(仅 Linux不阻塞
sendTelemetryOnce(getAppVersion());
});
app.on("window-all-closed", () => {
win = null;
allowAppExit = false;
if (process.platform !== "darwin") app.quit();
});
@@ -302,7 +325,7 @@ app.whenReady().then(() => {
{
label: "退出程序",
click: () => {
win.destroy();
requestApplicationExit();
},
},
]);