修补更新程序

This commit is contained in:
ACT丶流星雨 2026-04-06 04:12:45 +08:00
parent f73335daf7
commit 45c04bd729
5 changed files with 57 additions and 34 deletions

1
data/update.json Normal file
View File

@ -0,0 +1 @@
{"version":"1.0.11"}

View File

@ -1 +1 @@
1.0.121
1.0.11

View File

@ -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 {}
}
}

View File

@ -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);

View File

@ -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;
};