-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
46 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import fs from 'fs'; | ||
import { _CfgPath } from '../data/system/pluginConstants.js'; | ||
import kuroLogger from './logger.js'; | ||
|
||
class ConfigReader { | ||
constructor() { | ||
this.filePath = _CfgPath + '/config.json'; | ||
this.configObject = this.readConfig(); // 初始读取配置文件 | ||
this.watchConfig(); // 监听配置文件变化 | ||
} | ||
|
||
readConfig() { | ||
try { | ||
const data = fs.readFileSync(this.filePath, 'utf8'); | ||
const configObject = JSON.parse(data); | ||
return configObject; | ||
} catch (error) { | ||
kuroLogger.error('读取配置文件失败:', error.message); | ||
return {}; | ||
} | ||
} | ||
|
||
watchConfig() { | ||
fs.watchFile(this.filePath, (curr, prev) => { | ||
if (curr.mtime > prev.mtime) { | ||
this.configObject = this.readConfig(); | ||
kuroLogger.info('配置文件已更新'); | ||
kuroLogger.setLogLevel(this.configObject.logger.logLevel); | ||
} | ||
}); | ||
} | ||
|
||
getConfig() { | ||
return this.configObject; | ||
} | ||
} | ||
|
||
export default new ConfigReader().readConfig(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters