rc-plugin/model/index.js
zhiyu1998 f131e93830 feat: V1.6.3 增加bili扫码登录
现在使用 #R插件B站扫码 即可登录体验AI总结功能
2024-04-01 20:26:00 +08:00

105 lines
2.1 KiB
JavaScript

import fs from 'node:fs'
import _ from 'lodash'
import YAML from 'yaml'
import chokidar from 'chokidar'
class RConfig {
constructor () {
// 配置文件
this.configPath = './plugins/rconsole-plugin/config/'
this.config = {}
// 监听文件
this.watcher = { config: {} }
}
/**
* 获取配置文件
* @param name
* @returns {any}
*/
getConfig (name) {
let ignore = []
if (ignore.includes(`${name}`)) {
return this.getYaml(name)
}
return this.getYaml(name)
}
// 获取指定配置的某个字段
getField(name, field) {
const config = this.getConfig(name);
return config[field];
}
// 更新指定配置的某个字段
updateField(name, field, value) {
let config = this.getConfig(name);
config[field] = value; // 更新字段值
this.saveSet(name, config); // 保存更改
}
// 删除指定配置的某个字段
deleteField(name, field) {
let config = this.getConfig(name);
delete config[field]; // 删除指定字段
this.saveSet(name, config); // 保存更改
}
/**
* 获取配置yaml
* @param name 名称
*/
getYaml (name) {
// 获取文件路径
let file = this.getFilePath(name)
// 解析xml
const yaml = YAML.parse(fs.readFileSync(file, 'utf8'))
// 监听文件
this.watch(file, name)
return yaml
}
/**
* 获取文件路径
* @param name
* @returns {string}
*/
getFilePath (name) {
return `${this.configPath}${name}.yaml`
}
/**
* 听配置文件
* @param file
* @param name
*/
watch (file, name) {
const watcher = chokidar.watch(file)
watcher.on('change', (path) => {
delete
logger.mark(`[修改配置文件][${name}]`)
})
}
/**
* 保存配置
* @param name
* @param data
*/
saveSet (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')
}
}
}
export default new RConfig()