mirror of
https://github.com/Jerryplusy/crystelf-plugin.git
synced 2026-01-29 01:07:27 +00:00
✨ feat(configControl.js): ensure data and plugin config directories exist and handle missing files gracefully
This commit is contained in:
parent
9a642aa560
commit
58c4f19dd6
@ -15,32 +15,61 @@ let watchers = [];
|
|||||||
*/
|
*/
|
||||||
async function init() {
|
async function init() {
|
||||||
try {
|
try {
|
||||||
|
// 确保数据配置目录存在
|
||||||
try {
|
try {
|
||||||
await fsp.access(dataConfigPath);
|
await fsp.access(dataConfigPath);
|
||||||
} catch {
|
} catch {
|
||||||
await fsp.mkdir(dataConfigPath, { recursive: true });
|
await fsp.mkdir(dataConfigPath, { recursive: true });
|
||||||
logger.mark(`[crystelf-plugin] 配置目录创建成功: ${dataConfigPath}`);
|
logger.mark(`[crystelf-plugin] 配置目录创建成功: ${dataConfigPath}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 确保默认配置目录存在
|
||||||
|
try {
|
||||||
|
await fsp.access(pluginConfigPath);
|
||||||
|
} catch {
|
||||||
|
logger.warn(`[crystelf-plugin] 默认配置目录不存在: ${pluginConfigPath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理主配置文件
|
||||||
const pluginDefaultFile = path.join(pluginConfigPath, 'config.json');
|
const pluginDefaultFile = path.join(pluginConfigPath, 'config.json');
|
||||||
try {
|
try {
|
||||||
await fsp.access(configFile);
|
await fsp.access(configFile);
|
||||||
} catch {
|
} catch {
|
||||||
await fsp.copyFile(pluginDefaultFile, configFile);
|
try {
|
||||||
logger.mark(`[crystelf-plugin] 默认配置复制成功: ${configFile}`);
|
await fsp.copyFile(pluginDefaultFile, configFile);
|
||||||
|
logger.mark(`[crystelf-plugin] 默认配置复制成功: ${configFile}`);
|
||||||
|
} catch (copyError) {
|
||||||
|
logger.warn(`[crystelf-plugin] 复制默认配置失败,创建空配置: ${copyError.message}`);
|
||||||
|
await fc.writeJSON(configFile, {});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const pluginFiles = (await fsp.readdir(pluginConfigPath)).filter((f) => f.endsWith('.json'));
|
let pluginFiles = [];
|
||||||
|
try {
|
||||||
|
pluginFiles = (await fsp.readdir(pluginConfigPath)).filter((f) => f.endsWith('.json'));
|
||||||
|
} catch (error) {
|
||||||
|
logger.warn(`[crystelf-plugin] 读取默认配置目录失败: ${error.message}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 复制缺失的配置文件
|
||||||
for (const file of pluginFiles) {
|
for (const file of pluginFiles) {
|
||||||
const pluginFilePath = path.join(pluginConfigPath, file);
|
const pluginFilePath = path.join(pluginConfigPath, file);
|
||||||
const dataFilePath = path.join(dataConfigPath, file);
|
const dataFilePath = path.join(dataConfigPath, file);
|
||||||
try {
|
try {
|
||||||
await fsp.access(dataFilePath);
|
await fsp.access(dataFilePath);
|
||||||
} catch {
|
} catch {
|
||||||
await fsp.copyFile(pluginFilePath, dataFilePath);
|
try {
|
||||||
logger.mark(`[crystelf-plugin] 配置文件缺失,已复制: ${file}`);
|
await fsp.copyFile(pluginFilePath, dataFilePath);
|
||||||
|
logger.mark(`[crystelf-plugin] 配置文件缺失,已复制: ${file}`);
|
||||||
|
} catch (copyError) {
|
||||||
|
logger.warn(`[crystelf-plugin] 复制配置文件失败 ${file}: ${copyError.message}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 读取所有配置文件
|
||||||
const files = (await fsp.readdir(dataConfigPath)).filter((f) => f.endsWith('.json'));
|
const files = (await fsp.readdir(dataConfigPath)).filter((f) => f.endsWith('.json'));
|
||||||
configCache = {};
|
configCache = {};
|
||||||
|
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const filePath = path.join(dataConfigPath, file);
|
const filePath = path.join(dataConfigPath, file);
|
||||||
const name = path.basename(file, '.json');
|
const name = path.basename(file, '.json');
|
||||||
@ -50,7 +79,9 @@ async function init() {
|
|||||||
try {
|
try {
|
||||||
await fsp.access(pluginFilePath);
|
await fsp.access(pluginFilePath);
|
||||||
const pluginData = await fc.readJSON(pluginFilePath);
|
const pluginData = await fc.readJSON(pluginFilePath);
|
||||||
|
|
||||||
if (Array.isArray(data) && Array.isArray(pluginData)) {
|
if (Array.isArray(data) && Array.isArray(pluginData)) {
|
||||||
|
// 合并数组类型配置
|
||||||
const strSet = new Set(data.map((x) => JSON.stringify(x)));
|
const strSet = new Set(data.map((x) => JSON.stringify(x)));
|
||||||
for (const item of pluginData) {
|
for (const item of pluginData) {
|
||||||
const str = JSON.stringify(item);
|
const str = JSON.stringify(item);
|
||||||
@ -60,20 +91,28 @@ async function init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (!Array.isArray(data) && !Array.isArray(pluginData)) {
|
} else if (!Array.isArray(data) && !Array.isArray(pluginData)) {
|
||||||
|
// 合并对象类型配置
|
||||||
data = fc.mergeConfig(data, pluginData);
|
data = fc.mergeConfig(data, pluginData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 保存合并后的配置
|
||||||
await fc.writeJSON(filePath, data);
|
await fc.writeJSON(filePath, data);
|
||||||
} catch {}
|
} catch (mergeError) {
|
||||||
|
logger.error('[crystelf-plugin]合并配置失败..');
|
||||||
|
// 忽略合并错误,使用现有数据
|
||||||
|
}
|
||||||
|
|
||||||
configCache[name] = data;
|
configCache[name] = data;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.warn(`[crystelf-plugin] 读取配置文件 ${file} 失败:`, e);
|
logger.warn(`[crystelf-plugin] 读取配置文件 ${file} 失败:`, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (configCache.debug) {
|
if (configCache.debug) {
|
||||||
logger.info('[crystelf-plugin] 配置模块初始化成功..');
|
logger.info('[crystelf-plugin] 配置模块初始化成功..');
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.warn('[crystelf-plugin] 配置初始化失败,使用空配置..', err);
|
logger.warn('[crystelf-plugin] 配置初始化失败,使用空配置..', err);
|
||||||
configCache = {};
|
configCache = {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,36 +159,65 @@ const configControl = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async set(key, value) {
|
async set(key, value) {
|
||||||
|
// 更新内存中的配置
|
||||||
configCache[key] = value;
|
configCache[key] = value;
|
||||||
const filePath = path.join(dataConfigPath, `${key}.json`);
|
const filePath = path.join(dataConfigPath, `${key}.json`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// 尝试访问文件,如果存在则直接写入
|
||||||
await fsp.access(filePath);
|
await fsp.access(filePath);
|
||||||
await fc.writeJSON(filePath, value);
|
await fc.writeJSON(filePath, value);
|
||||||
} catch {
|
} catch (error) {
|
||||||
let cfg = await fc.readJSON(configFile);
|
// 文件不存在,创建新文件
|
||||||
if (Array.isArray(cfg)) {
|
try {
|
||||||
cfg.push(value);
|
// 确保目录存在
|
||||||
} else {
|
await fsp.mkdir(dataConfigPath, { recursive: true });
|
||||||
cfg[key] = value;
|
// 直接写入新文件
|
||||||
|
await fc.writeJSON(filePath, value);
|
||||||
|
logger.mark(`[crystelf-plugin] 创建新配置文件: ${filePath}`);
|
||||||
|
} catch (writeError) {
|
||||||
|
logger.error(`[crystelf-plugin] 创建配置文件失败: ${writeError.message}`);
|
||||||
|
throw writeError;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量设置配置
|
||||||
|
* @param {Object} configs - 配置对象,键为配置名,值为配置数据
|
||||||
|
*/
|
||||||
|
async setMultiple(configs) {
|
||||||
|
// 确保目录存在
|
||||||
|
await fsp.mkdir(dataConfigPath, { recursive: true });
|
||||||
|
|
||||||
|
for (const [key, value] of Object.entries(configs)) {
|
||||||
|
try {
|
||||||
|
// 更新内存中的配置
|
||||||
|
configCache[key] = value;
|
||||||
|
const filePath = path.join(dataConfigPath, `${key}.json`);
|
||||||
|
|
||||||
|
// 写入配置文件
|
||||||
|
await fc.writeJSON(filePath, value);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`[crystelf-plugin] 设置配置失败 ${key}: ${error.message}`);
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
await fc.writeJSON(configFile, cfg);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async save() {
|
async save() {
|
||||||
|
// 确保目录存在
|
||||||
|
await fsp.mkdir(dataConfigPath, { recursive: true });
|
||||||
|
|
||||||
for (const [key, value] of Object.entries(configCache)) {
|
for (const [key, value] of Object.entries(configCache)) {
|
||||||
const filePath = path.join(dataConfigPath, `${key}.json`);
|
const filePath = path.join(dataConfigPath, `${key}.json`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fsp.access(filePath);
|
// 直接写入配置文件
|
||||||
await fc.writeJSON(filePath, value);
|
await fc.writeJSON(filePath, value);
|
||||||
} catch {
|
} catch (error) {
|
||||||
let cfg = await fc.readJSON(configFile);
|
logger.error(`[crystelf-plugin] 保存配置文件失败 ${filePath}: ${error.message}`);
|
||||||
if (Array.isArray(cfg)) {
|
throw error;
|
||||||
cfg = value;
|
|
||||||
} else {
|
|
||||||
cfg[key] = value;
|
|
||||||
}
|
|
||||||
await fc.writeJSON(configFile, cfg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user