refactor: simplify TOML common config handling by removing markers

- Remove COMMON_CONFIG_MARKER_START/END constants
- Simplify config snippet addition/removal logic
- Use natural append/replace approach instead of markers
- Fix unused variable warning
- Improve user experience with cleaner config output
This commit is contained in:
Jason
2025-09-18 22:33:55 +08:00
parent c6e4f3599e
commit 04e81ebbe3
7 changed files with 116 additions and 112 deletions

View File

@@ -22,7 +22,10 @@ const deepMerge = (
return target;
};
const deepRemove = (target: Record<string, any>, source: Record<string, any>) => {
const deepRemove = (
target: Record<string, any>,
source: Record<string, any>,
) => {
Object.entries(source).forEach(([key, value]) => {
if (!(key in target)) return;
@@ -59,7 +62,7 @@ const isSubset = (target: any, source: any): boolean => {
const deepClone = <T>(obj: T): T => {
if (obj === null || typeof obj !== "object") return obj;
if (obj instanceof Date) return new Date(obj.getTime()) as T;
if (obj instanceof Array) return obj.map(item => deepClone(item)) as T;
if (obj instanceof Array) return obj.map((item) => deepClone(item)) as T;
if (obj instanceof Object) {
const clonedObj = {} as T;
for (const key in obj) {
@@ -78,7 +81,10 @@ export interface UpdateCommonConfigResult {
}
// 验证JSON配置格式
export const validateJsonConfig = (value: string, fieldName: string = "配置"): string => {
export const validateJsonConfig = (
value: string,
fieldName: string = "配置",
): string => {
if (!value.trim()) {
return "";
}
@@ -123,7 +129,7 @@ export const updateCommonConfigSnippet = (
error: snippetError,
};
}
const snippet = JSON.parse(snippetString) as Record<string, any>;
if (enabled) {
@@ -205,14 +211,14 @@ export const setApiKeyInConfig = (
// ========== TOML Config Utilities ==========
const COMMON_CONFIG_MARKER_START = "# === COMMON CONFIG START ===";
const COMMON_CONFIG_MARKER_END = "# === COMMON CONFIG END ===";
export interface UpdateTomlCommonConfigResult {
updatedConfig: string;
error?: string;
}
// 保存之前的通用配置片段,用于替换操作
let previousCommonSnippet = "";
// 将通用配置片段写入/移除 TOML 配置
export const updateTomlCommonConfigSnippet = (
tomlString: string,
@@ -220,71 +226,64 @@ export const updateTomlCommonConfigSnippet = (
enabled: boolean,
): UpdateTomlCommonConfigResult => {
if (!snippetString.trim()) {
// 如果片段为空,移除已存在的通用配置部分
const cleaned = removeTomlCommonConfig(tomlString);
// 如果片段为空,直接返回原始配置
return {
updatedConfig: cleaned,
updatedConfig: tomlString,
};
}
if (enabled) {
// 添加通用配置
const withoutOld = removeTomlCommonConfig(tomlString);
const commonSection = `\n${COMMON_CONFIG_MARKER_START}\n${snippetString}\n${COMMON_CONFIG_MARKER_END}\n`;
// 先移除旧的通用配置(如果有)
let updatedConfig = tomlString;
if (previousCommonSnippet && tomlString.includes(previousCommonSnippet)) {
updatedConfig = tomlString.replace(previousCommonSnippet, "");
}
// 在文件末尾添加新的通用配置
// 确保有适当的换行
const needsNewline = updatedConfig && !updatedConfig.endsWith("\n");
updatedConfig =
updatedConfig + (needsNewline ? "\n\n" : "\n") + snippetString;
// 保存当前通用配置片段
previousCommonSnippet = snippetString;
return {
updatedConfig: withoutOld + commonSection,
updatedConfig: updatedConfig.trim() + "\n",
};
} else {
// 移除通用配置
const cleaned = removeTomlCommonConfig(tomlString);
if (tomlString.includes(snippetString)) {
const updatedConfig = tomlString.replace(snippetString, "");
// 清理多余的空行
const cleaned = updatedConfig.replace(/\n{3,}/g, "\n\n").trim();
// 清空保存的状态
previousCommonSnippet = "";
return {
updatedConfig: cleaned ? cleaned + "\n" : "",
};
}
return {
updatedConfig: cleaned,
updatedConfig: tomlString,
};
}
};
// 从 TOML 中移除通用配置部分
const removeTomlCommonConfig = (tomlString: string): string => {
const startIdx = tomlString.indexOf(COMMON_CONFIG_MARKER_START);
const endIdx = tomlString.indexOf(COMMON_CONFIG_MARKER_END);
if (startIdx === -1 || endIdx === -1) {
return tomlString;
}
// 找到标记前的换行符(如果有)
let realStartIdx = startIdx;
if (startIdx > 0 && tomlString[startIdx - 1] === '\n') {
realStartIdx = startIdx - 1;
}
// 找到标记后的换行符(如果有)
let realEndIdx = endIdx + COMMON_CONFIG_MARKER_END.length;
if (realEndIdx < tomlString.length && tomlString[realEndIdx] === '\n') {
realEndIdx = realEndIdx + 1;
}
return tomlString.slice(0, realStartIdx) + tomlString.slice(realEndIdx);
};
// 检查 TOML 配置是否已包含通用配置片段
export const hasTomlCommonConfigSnippet = (
tomlString: string,
snippetString: string,
): boolean => {
if (!snippetString.trim()) return false;
const startIdx = tomlString.indexOf(COMMON_CONFIG_MARKER_START);
const endIdx = tomlString.indexOf(COMMON_CONFIG_MARKER_END);
if (startIdx === -1 || endIdx === -1 || startIdx >= endIdx) {
return false;
}
// 提取标记之间的内容
const existingSnippet = tomlString
.slice(startIdx + COMMON_CONFIG_MARKER_START.length, endIdx)
.trim();
return existingSnippet === snippetString.trim();
// 简单检查配置是否包含片段内容
// 去除空白字符后比较,避免格式差异影响
const normalizeWhitespace = (str: string) => str.replace(/\s+/g, " ").trim();
return normalizeWhitespace(tomlString).includes(
normalizeWhitespace(snippetString),
);
};