From df73e1e5a3fe7b4c2020709c6a77a8961f92eb4a Mon Sep 17 00:00:00 2001 From: linyuchen Date: Fri, 3 May 2024 13:04:02 +0800 Subject: [PATCH] refactor: ordered onebot11.json keys --- src/common/utils/ConfigBase.ts | 9 +++++++-- src/onebot11/config.ts | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/common/utils/ConfigBase.ts b/src/common/utils/ConfigBase.ts index 6bfce0fc..a11d221e 100644 --- a/src/common/utils/ConfigBase.ts +++ b/src/common/utils/ConfigBase.ts @@ -11,6 +11,11 @@ export class ConfigBase{ 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{ 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{ 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); } diff --git a/src/onebot11/config.ts b/src/onebot11/config.ts index 41d03e61..03ad92a8 100644 --- a/src/onebot11/config.ts +++ b/src/onebot11/config.ts @@ -55,6 +55,10 @@ class Config extends ConfigBase 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();