chore:AI大修改
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
import fs from "node:fs";
|
||||
|
||||
/**
|
||||
* 应用配置接口
|
||||
*/
|
||||
export interface IConfig {
|
||||
mirror: {
|
||||
bmclapi: boolean;
|
||||
@@ -9,32 +13,65 @@ export interface IConfig {
|
||||
dexpub: boolean;
|
||||
mixins: boolean;
|
||||
};
|
||||
oaf: boolean
|
||||
oaf: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认配置
|
||||
*/
|
||||
const DEFAULT_CONFIG: IConfig = {
|
||||
mirror: {
|
||||
bmclapi: true,
|
||||
mcimirror: true,
|
||||
},
|
||||
filter: {
|
||||
hashes: true,
|
||||
dexpub: false,
|
||||
mixins: true,
|
||||
},
|
||||
oaf: true
|
||||
};
|
||||
|
||||
/**
|
||||
* 配置文件路径
|
||||
*/
|
||||
const CONFIG_PATH = "./config.json";
|
||||
|
||||
/**
|
||||
* 配置管理器
|
||||
*/
|
||||
export class Config {
|
||||
private readonly default_config: IConfig = {
|
||||
mirror: {
|
||||
bmclapi: true,
|
||||
mcimirror: true,
|
||||
},
|
||||
filter: {
|
||||
hashes: true,
|
||||
dexpub: false,
|
||||
mixins: true,
|
||||
},
|
||||
oaf:true
|
||||
};
|
||||
config(): IConfig {
|
||||
if (!fs.existsSync("./config.json")) {
|
||||
fs.writeFileSync("./config.json", JSON.stringify(this.default_config));
|
||||
return this.default_config;
|
||||
/**
|
||||
* 获取配置
|
||||
* @returns 配置对象
|
||||
*/
|
||||
public static getConfig(): IConfig {
|
||||
if (!fs.existsSync(CONFIG_PATH)) {
|
||||
fs.writeFileSync(CONFIG_PATH, JSON.stringify(DEFAULT_CONFIG, null, 2));
|
||||
return DEFAULT_CONFIG;
|
||||
}
|
||||
|
||||
try {
|
||||
const content = fs.readFileSync(CONFIG_PATH, "utf-8");
|
||||
return JSON.parse(content);
|
||||
} catch (err) {
|
||||
console.error("Failed to read config file, using defaults", err);
|
||||
return DEFAULT_CONFIG;
|
||||
}
|
||||
return JSON.parse(fs.readFileSync("./config.json", "utf-8"));
|
||||
}
|
||||
static write_config(config: IConfig) {
|
||||
fs.writeFileSync("./config.json", JSON.stringify(config));
|
||||
|
||||
/**
|
||||
* 写入配置
|
||||
* @param config 配置对象
|
||||
*/
|
||||
public static writeConfig(config: IConfig): void {
|
||||
try {
|
||||
fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2));
|
||||
} catch (err) {
|
||||
console.error("Failed to write config file", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new Config().config();
|
||||
// 默认导出配置实例
|
||||
export default Config.getConfig();
|
||||
|
||||
Reference in New Issue
Block a user