From f73335daf7b5cb298cb2d284cdcb9a82463ca3ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?ACT=E4=B8=B6=E6=B5=81=E6=98=9F=E9=9B=A8?= <1340145680@qq.com> Date: Mon, 6 Apr 2026 03:56:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=89=88=E6=9C=AC=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/main.ts | 15 +++++++++++++-- src/utils/writeVersion.ts | 11 ++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/scripts/main.ts b/scripts/main.ts index f538578..0b63644 100644 --- a/scripts/main.ts +++ b/scripts/main.ts @@ -9,6 +9,17 @@ app.commandLine.appendSwitch("disable-features", "CalculateNativeWinOcclusion"); declare const __APP_VERSION__: string | undefined; +/** + * 将 extraResources 中的 data 目录复制到用户数据目录(跳过已存在的文件,保留用户修改) + */ + +function getVersionFromUpdateJson(filePath: string): string | null { + try { + return JSON.parse(fs.readFileSync(filePath, "utf8")).version ?? null; + } catch {} + return null; +} + const SKIP_ENTRIES = new Set(["logs", "oss", "db2.sqlite"]); function copyDir(src: string, dest: string, overwrite = false): void { @@ -29,9 +40,9 @@ function copyDir(src: string, dest: string, overwrite = false): void { function initializeData(): void { const srcDir = path.join(process.resourcesPath, "data"); const destDir = path.join(app.getPath("userData"), "data"); - const updateJsonFile = path.join(destDir, "version.txt"); + const updateJsonFile = path.join(destDir, "update.json"); const currentVersion = typeof __APP_VERSION__ !== "undefined" ? __APP_VERSION__ : "0.0.0"; - const userVersion = fs.existsSync(updateJsonFile) ? fs.readFileSync(updateJsonFile, "utf8") : null; + const userVersion = getVersionFromUpdateJson(updateJsonFile); if (!userVersion) { copyDir(srcDir, destDir); diff --git a/src/utils/writeVersion.ts b/src/utils/writeVersion.ts index c1b5db7..54533c7 100644 --- a/src/utils/writeVersion.ts +++ b/src/utils/writeVersion.ts @@ -15,21 +15,22 @@ const APP_VERSION: string = (() => { })(); export default async (version?: string) => { - const versionFile = path.join(getPath(), "version.txt"); + const versionFile = path.join(getPath(), "update.json"); if (!fs.existsSync(versionFile)) { fs.mkdirSync(path.dirname(versionFile), { recursive: true }); } - await fs.promises.writeFile(versionFile, version ?? APP_VERSION, "utf8"); + await fs.promises.writeFile(versionFile, JSON.stringify({ version: version ?? APP_VERSION }, null, 4), "utf8"); }; export const getVersion = async () => { - const versionFile = path.join(getPath(), "version.txt"); + const versionFile = path.join(getPath(), "update.json"); if (fs.existsSync(versionFile)) { - return fs.readFileSync(versionFile, "utf8"); + const data = JSON.parse(fs.readFileSync(versionFile, "utf8")); + return data.version || null; } if (!fs.existsSync(versionFile)) { fs.mkdirSync(path.dirname(versionFile), { recursive: true }); } - await fs.promises.writeFile(versionFile, APP_VERSION, "utf8"); + await fs.promises.writeFile(versionFile, JSON.stringify({ version: APP_VERSION }, null, 4), "utf8"); return APP_VERSION; };