refactor: ordered onebot11.json keys

This commit is contained in:
linyuchen 2024-05-03 13:04:02 +08:00
parent 64a0037265
commit df73e1e5a3
2 changed files with 11 additions and 2 deletions

View File

@ -11,6 +11,11 @@ export class ConfigBase<T>{
constructor() {
}
protected getKeys(): string[] | null {
// 决定 key 在json配置文件中的顺序
return null;
}
getConfigDir(){
const configDir = path.resolve(__dirname, 'config');
fs.mkdirSync(configDir, { recursive: true });
@ -24,7 +29,7 @@ export class ConfigBase<T>{
const configPath = this.getConfigPath();
if (!fs.existsSync(configPath)) {
try{
fs.writeFileSync(configPath, JSON.stringify(this, null, 2));
fs.writeFileSync(configPath, JSON.stringify(this, this.getKeys(), 2));
log(`配置文件${configPath}已创建\n如果修改此文件后需要重启 NapCat 生效`);
}
catch (e: any) {
@ -54,7 +59,7 @@ export class ConfigBase<T>{
Object.assign(this, config);
const configPath = this.getConfigPath();
try {
fs.writeFileSync(configPath, JSON.stringify(this, null, 2));
fs.writeFileSync(configPath, JSON.stringify(this, this.getKeys(), 2));
} catch (e: any) {
logError(`保存配置文件 ${configPath} 时发生错误:`, e.message);
}

View File

@ -55,6 +55,10 @@ class Config extends ConfigBase<OB11Config> implements OB11Config {
getConfigPath() {
return path.join(this.getConfigDir(), `onebot11_${selfInfo.uin}.json`);
}
protected getKeys(): string[] {
return ['httpHost', 'enableHttp', 'httpPort', 'wsHost', 'enableWs', 'wsPort', 'enableWsReverse', 'wsReverseUrls', 'enableHttpPost', 'httpPostUrls', 'enableHttpHeart', 'httpSecret', 'messagePostFormat', 'reportSelfMessage', 'debug', 'enableLocalFile2Url', 'heartInterval', 'token', 'musicSignUrl'];
}
}
export const ob11Config = new Config();