config模块

This commit is contained in:
Jerry 2025-04-23 14:00:39 +08:00
parent 8c58bb0d8f
commit ed4bc5be47
4 changed files with 46 additions and 13 deletions

View File

@ -25,7 +25,7 @@ let fc = {
* @example * @example
* fc.createDir("config/deepseek", "root") // 在 Yunzai 根目录创建 config/deepseek 目录 * fc.createDir("config/deepseek", "root") // 在 Yunzai 根目录创建 config/deepseek 目录
*/ */
createDir(path = '', root = '', includeFile = false) { async createDir(path = '', root = '', includeFile = false) {
root = getRoot(root); root = getRoot(root);
let pathList = path.split('/'); let pathList = path.split('/');
let nowPath = root; let nowPath = root;
@ -50,7 +50,7 @@ let fc = {
* @example * @example
* const config = fc.readJSON("config.json", "root") * const config = fc.readJSON("config.json", "root")
*/ */
readJSON(file = '', root = '') { async readJSON(file = '', root = '') {
root = getRoot(root); root = getRoot(root);
if (fs.existsSync(`${root}/${file}`)) { if (fs.existsSync(`${root}/${file}`)) {
try { try {
@ -62,7 +62,7 @@ let fc = {
return {}; return {};
}, },
statSync(file = '', root = '') { async statSync(file = '', root = '') {
root = getRoot(root); root = getRoot(root);
try { try {
return fs.statSync(`${root}/${file}`); return fs.statSync(`${root}/${file}`);
@ -82,8 +82,8 @@ let fc = {
* @example * @example
* fc.writeJSON("config.json", {key: "value"}, "root", 4) * fc.writeJSON("config.json", {key: "value"}, "root", 4)
*/ */
writeJSON(file, data, root = '', space = 4) { async writeJSON(file, data, root = '', space = 4) {
fc.createDir(file, root, true); await fc.createDir(file, root, true);
root = getRoot(root); root = getRoot(root);
try { try {
fs.writeFileSync(`${root}/${file}`, JSON.stringify(data, null, space)); fs.writeFileSync(`${root}/${file}`, JSON.stringify(data, null, space));
@ -108,8 +108,8 @@ let fc = {
* @example * @example
* fc.safewriteJSON("config.json", {newKey: "value"}) * fc.safewriteJSON("config.json", {newKey: "value"})
*/ */
safeWriteJSON(file, data, root = '', space = 4) { async safeWriteJSON(file, data, root = '', space = 4) {
fc.createDir(file, root, true); await fc.createDir(file, root, true);
root = getRoot(root); root = getRoot(root);
const filePath = `${root}/${file}`; const filePath = `${root}/${file}`;
@ -146,7 +146,7 @@ let fc = {
* const merged = fc.deepMerge({a: 1}, {b: {c: 2}}) * const merged = fc.deepMerge({a: 1}, {b: {c: 2}})
* // 返回 {a: 1, b: {c: 2}} * // 返回 {a: 1, b: {c: 2}}
*/ */
deepMerge(target, source) { async deepMerge(target, source) {
for (const key in source) { for (const key in source) {
if (source.hasOwnProperty(key)) { if (source.hasOwnProperty(key)) {
if ( if (
@ -155,7 +155,7 @@ let fc = {
target[key] && target[key] &&
typeof target[key] === 'object' typeof target[key] === 'object'
) { ) {
this.deepMerge(target[key], source[key]); await this.deepMerge(target[key], source[key]);
} else { } else {
target[key] = source[key]; target[key] = source[key];
} }
@ -176,7 +176,7 @@ let fc = {
* @example * @example
* const jsFiles = fc.readDirRecursive("./plugins", "js", "node_modules") * const jsFiles = fc.readDirRecursive("./plugins", "js", "node_modules")
*/ */
readDirRecursive(directory, extension, excludeDir) { async readDirRecursive(directory, extension, excludeDir) {
let files = fs.readdirSync(directory); let files = fs.readdirSync(directory);
let jsFiles = files.filter( let jsFiles = files.filter(
@ -213,7 +213,7 @@ let fc = {
* const obj = { a: 1, b: [2, 3] }; * const obj = { a: 1, b: [2, 3] };
* const cloned = fc.deepClone(obj); * const cloned = fc.deepClone(obj);
*/ */
deepClone(source) { async deepClone(source) {
const cache = new WeakMap(); const cache = new WeakMap();
const clone = (value) => { const clone = (value) => {

0
config/default.json Normal file
View File

View File

@ -3,14 +3,14 @@ import url from 'url';
const __filename = url.fileURLToPath(import.meta.url); const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
const rootDir = path.join(__dirname, '..'); const rootDir = path.join(__dirname, '..');
const Path = { const Path = {
root: rootDir, root: rootDir,
apps: path.join(rootDir, 'apps'), apps: path.join(rootDir, 'apps'),
components: path.join(rootDir, 'components'), components: path.join(rootDir, 'components'),
config: path.join(rootDir, 'config'), defaultConfig: path.join(rootDir, 'config/default.json'),
config: path.join(rootDir, './../data/crystelf'),
constants: path.join(rootDir, 'constants'), constants: path.join(rootDir, 'constants'),
lib: path.join(rootDir, 'lib'), lib: path.join(rootDir, 'lib'),
models: path.join(rootDir, 'models'), models: path.join(rootDir, 'models'),

View File

@ -0,0 +1,33 @@
import Path from '../../constants/path.js';
import fc from '../../components/json.js';
import path from 'path';
let configCache = null;
let lastModified = 0;
const configPath = Path.config;
const configFile = path.join(configPath, 'config.json');
const configControl = {
async getConfig() {
try {
const stats = await fc.statSync(configFile, 'root');
if (!configCache || stats.mtimeMs > lastModified) {
configCache = await fc.readJSON(configFile, 'root');
lastModified = stats.mtimeMs;
}
return configCache;
} catch (err) {
console.error('读取配置失败:', err);
return {};
}
},
async updateConfig(updater) {
try {
const config = this.getConfig();
configCache = { ...config, ...updater };
fc.safeWriteJSON(configFile, configCache, 'root', 4);
} catch (err) {
console.error('更新配置失败:', err);
}
},
};
export default configControl;