/** * 读写用户配置 * 实时维护状态 */ const fs = require("fs") const JSON5 = require('json5') module.exports = class UserConfig { constructor(path) { this.path = path this.init() } init(){ let txt = "{}"; try { txt = fs.readFileSync(this.path, 'utf8') } catch ( err ) { if (err.code === 'ENOENT') { // File does not exist. txt = '{}'; } else { // Some other error occurred. Throw it. throw err; } } this.configs = JSON5.parse( txt ) this.setDefault("man", "https://flowus.cn/fullstack/share/acd5b26f-b3d1-4d41-a58e-e4f23daf9db8") } get(key) { return this.configs[key] } set(key, value) { this.configs[key] = value this.write() } setDefault(key, value){ if ( ! this.configs[key] ) { this.configs[key] = value } } write(){ fs.writeFileSync(this.path, JSON5.stringify(this.configs, undefined, 4)) } }