From 45c04bd729f45de6c2d66eb111b16d2e61295893 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 04:12:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E8=A1=A5=E6=9B=B4=E6=96=B0=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/update.json | 1 + data/version.txt | 2 +- scripts/main.ts | 57 +++++++++++++++++++++------------------ src/app.ts | 20 +++++++++++++- src/utils/writeVersion.ts | 11 ++++---- 5 files changed, 57 insertions(+), 34 deletions(-) create mode 100644 data/update.json diff --git a/data/update.json b/data/update.json new file mode 100644 index 0000000..7c547a6 --- /dev/null +++ b/data/update.json @@ -0,0 +1 @@ +{"version":"1.0.11"} \ No newline at end of file diff --git a/data/version.txt b/data/version.txt index a23818b..8684498 100644 --- a/data/version.txt +++ b/data/version.txt @@ -1 +1 @@ -1.0.121 \ No newline at end of file +1.0.11 \ No newline at end of file diff --git a/scripts/main.ts b/scripts/main.ts index 0b63644..d4472a8 100644 --- a/scripts/main.ts +++ b/scripts/main.ts @@ -15,26 +15,20 @@ declare const __APP_VERSION__: string | undefined; function getVersionFromUpdateJson(filePath: string): string | null { try { - return JSON.parse(fs.readFileSync(filePath, "utf8")).version ?? null; + if (fs.existsSync(filePath)) { + const data = JSON.parse(fs.readFileSync(filePath, "utf8")); + return data.version ?? null; + } } catch {} return null; } -const SKIP_ENTRIES = new Set(["logs", "oss", "db2.sqlite"]); - -function copyDir(src: string, dest: string, overwrite = false): void { +function copyDirForce(src: string, dest: string): void { if (!fs.existsSync(src)) return; - fs.mkdirSync(dest, { recursive: true }); - for (const entry of fs.readdirSync(src, { withFileTypes: true })) { - if (SKIP_ENTRIES.has(entry.name)) continue; - const srcPath = path.join(src, entry.name); - const destPath = path.join(dest, entry.name); - if (entry.isDirectory()) { - copyDir(srcPath, destPath, overwrite); - } else if (overwrite || !fs.existsSync(destPath)) { - fs.copyFileSync(srcPath, destPath); - } + if (fs.existsSync(dest)) { + fs.rmSync(dest, { recursive: true, force: true }); } + copyDirRecursive(src, dest); } function initializeData(): void { @@ -44,23 +38,34 @@ function initializeData(): void { const currentVersion = typeof __APP_VERSION__ !== "undefined" ? __APP_VERSION__ : "0.0.0"; const userVersion = getVersionFromUpdateJson(updateJsonFile); - if (!userVersion) { - copyDir(srcDir, destDir); + // 首次安装或无update.json,直接全量拷贝 + if (!fs.existsSync(destDir) || !userVersion) { + copyDirRecursive(srcDir, destDir); return; } + // 版本号不同则覆盖 serve 和 web 目录 if (userVersion !== currentVersion) { - for (const dir of ["serve", "web"]) { - const dest = path.join(destDir, dir); - if (fs.existsSync(dest)) fs.rmSync(dest, { recursive: true, force: true }); - copyDir(path.join(srcDir, dir), dest); + copyDirForce(path.join(srcDir, "serve"), path.join(destDir, "serve")); + copyDirForce(path.join(srcDir, "web"), path.join(destDir, "web")); + } +} + +function copyDirRecursive(src: string, dest: string): void { + if (!fs.existsSync(src)) return; + if (!fs.existsSync(dest)) fs.mkdirSync(dest, { recursive: true }); + for (const entry of fs.readdirSync(src, { withFileTypes: true })) { + // 跳过 oss 文件夹和 db2.sqlite 文件 + if (entry.isDirectory() && entry.name === "logs") continue; + if (entry.isDirectory() && entry.name === "oss") continue; + if (!entry.isDirectory() && entry.name === "db2.sqlite") continue; + const srcPath = path.join(src, entry.name); + const destPath = path.join(dest, entry.name); + if (entry.isDirectory()) { + copyDirRecursive(srcPath, destPath); + } else if (!fs.existsSync(destPath)) { + fs.copyFileSync(srcPath, destPath); } - copyDir(srcDir, destDir); - try { - const data = JSON.parse(fs.readFileSync(updateJsonFile, "utf8")); - data.version = currentVersion; - fs.writeFileSync(updateJsonFile, JSON.stringify(data, null, 4), "utf8"); - } catch {} } } diff --git a/src/app.ts b/src/app.ts index 90b3d48..952cab6 100644 --- a/src/app.ts +++ b/src/app.ts @@ -12,13 +12,31 @@ import fs from "fs"; import u from "@/utils"; import jwt from "jsonwebtoken"; import socketInit from "@/socket/index"; +import path from "path"; + +declare const __APP_VERSION__: string; + +const APP_VERSION: string = (() => { + if (typeof __APP_VERSION__ !== "undefined") { + return __APP_VERSION__; + } + // 开发环境回退:从 package.json 读取 + const pkgPath = path.resolve(process.cwd(), "package.json"); + const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8")); + return pkg.version; +})(); const app = express(); const server = http.createServer(app); export default async function startServe(randomPort: Boolean = false) { + const verJsonPath = path.join(u.getPath(), "update.json"); + const data = JSON.parse(fs.readFileSync(verJsonPath, "utf8")); + if (data.version !== APP_VERSION) { + await fs.promises.writeFile(u.getPath("update.json"), JSON.stringify({ version: APP_VERSION }), "utf-8"); + } - await u.writeVersion() + await u.writeVersion(); const io = new Server(server, { cors: { origin: "*" } }); socketInit(io); diff --git a/src/utils/writeVersion.ts b/src/utils/writeVersion.ts index 54533c7..c1b5db7 100644 --- a/src/utils/writeVersion.ts +++ b/src/utils/writeVersion.ts @@ -15,22 +15,21 @@ const APP_VERSION: string = (() => { })(); export default async (version?: string) => { - const versionFile = path.join(getPath(), "update.json"); + const versionFile = path.join(getPath(), "version.txt"); if (!fs.existsSync(versionFile)) { fs.mkdirSync(path.dirname(versionFile), { recursive: true }); } - await fs.promises.writeFile(versionFile, JSON.stringify({ version: version ?? APP_VERSION }, null, 4), "utf8"); + await fs.promises.writeFile(versionFile, version ?? APP_VERSION, "utf8"); }; export const getVersion = async () => { - const versionFile = path.join(getPath(), "update.json"); + const versionFile = path.join(getPath(), "version.txt"); if (fs.existsSync(versionFile)) { - const data = JSON.parse(fs.readFileSync(versionFile, "utf8")); - return data.version || null; + return fs.readFileSync(versionFile, "utf8"); } if (!fs.existsSync(versionFile)) { fs.mkdirSync(path.dirname(versionFile), { recursive: true }); } - await fs.promises.writeFile(versionFile, JSON.stringify({ version: APP_VERSION }, null, 4), "utf8"); + await fs.promises.writeFile(versionFile, APP_VERSION, "utf8"); return APP_VERSION; };