rc-plugin/model/config.js
2025-09-10 21:57:09 +08:00

60 lines
1.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import fs from 'node:fs';
import _ from 'lodash';
import YAML from 'yaml';
import chokidar from 'chokidar';
import Base from './base.js';
class RConfig extends Base {
constructor(e) {
super(e);
this.configPath = `./plugins/${RConfig.pluginName}/config/`;
this.previousContent = new Map(); // 存储每个文件之前的内容
}
getConfig(name) {
return this.getYaml(name);
}
getYaml(name, isWatch = true) {
let file = this.getFilePath(name);
const yaml = YAML.parse(fs.readFileSync(file, 'utf8'));
if (isWatch) {
this.previousContent.set(name, yaml); // 保存初始内容
this.watch(file, name);
}
return yaml;
}
getFilePath(name) {
return `${this.configPath}${name}.yaml`;
}
watch(file, name) {
const watcher = chokidar.watch(file);
watcher.on('change', (path) => {
const currentContent = YAML.parse(fs.readFileSync(path, 'utf8'));
const previousContent = this.previousContent.get(name);
if (!_.isEqual(previousContent, currentContent)) {
logger.mark(`[R插件][配置文件]${name}已经被重置`);
this.previousContent.set(name, currentContent); // 更新之前的内容
}
});
}
saveAllConfig(name, data) {
let file = this.getFilePath(name);
if (_.isEmpty(data)) {
fs.existsSync(file) && fs.unlinkSync(file);
} else {
let yaml = YAML.stringify(data);
fs.writeFileSync(file, yaml, 'utf8');
}
this.watch(file, name);
}
}
export default new RConfig();