修补更新程序
This commit is contained in:
parent
f73335daf7
commit
45c04bd729
1
data/update.json
Normal file
1
data/update.json
Normal file
@ -0,0 +1 @@
|
||||
{"version":"1.0.11"}
|
||||
@ -1 +1 @@
|
||||
1.0.121
|
||||
1.0.11
|
||||
@ -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 {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
20
src/app.ts
20
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);
|
||||
|
||||
@ -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;
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user