Files
Max-Cocos-Demo/extensions/max-studio/source/hotupdate/cfgutils.ts
2025-10-28 21:55:41 +08:00

39 lines
1.2 KiB
TypeScript

import { existsSync, readFileSync, writeFileSync } from "fs";
import { ensureFileSync } from "fs-extra";
import path from "path";
export class CfgData {
version: string = "1.0.0";
md5: string = "";
}
export class CfgUtils {
private static getCfgFile(pkg: string) {
if (!pkg || pkg.trim() === '') {
throw new Error('包名不能为空');
}
return path.join(__dirname, `../../../temp/max-framework-hotupdate/${pkg}.json`);
}
static getCfgData(pkg: string): CfgData {
const cfgFile = CfgUtils.getCfgFile(pkg);
if (existsSync(cfgFile)) {
const data = readFileSync(cfgFile, 'utf-8');
const cfgData = JSON.parse(data) as CfgData;
console.log('读取配置文件', pkg, JSON.stringify(cfgData));
return cfgData;
} else {
const data = new CfgData();
CfgUtils.savaConfig(pkg, data);
return data;
}
}
static savaConfig(pkg: string, data: CfgData) {
console.log('保存配置文件', pkg, JSON.stringify(data));
const cfgFile = CfgUtils.getCfgFile(pkg);
ensureFileSync(cfgFile);
writeFileSync(cfgFile, JSON.stringify(data), 'utf-8');
}
}