diff --git a/src/main/config/controledMihomo.ts b/src/main/config/controledMihomo.ts index c44e195..0f162aa 100644 --- a/src/main/config/controledMihomo.ts +++ b/src/main/config/controledMihomo.ts @@ -5,13 +5,23 @@ import { generateProfile } from '../core/factory' import { getAppConfig } from './app' import { defaultControledMihomoConfig } from '../utils/template' import { deepMerge } from '../utils/merge' +import { existsSync } from 'fs' let controledMihomoConfig: Partial // mihomo.yaml export async function getControledMihomoConfig(force = false): Promise> { if (force || !controledMihomoConfig) { - const data = await readFile(controledMihomoConfigPath(), 'utf-8') - controledMihomoConfig = parse(data) || defaultControledMihomoConfig + if (existsSync(controledMihomoConfigPath())) { + const data = await readFile(controledMihomoConfigPath(), 'utf-8') + controledMihomoConfig = parse(data) || defaultControledMihomoConfig + } else { + controledMihomoConfig = defaultControledMihomoConfig + try { + await writeFile(controledMihomoConfigPath(), stringify(defaultControledMihomoConfig), 'utf-8') + } catch (error) { + console.error('Failed to create mihomo.yaml file:', error) + } + } // 确保配置包含所有必要的默认字段,处理升级场景 controledMihomoConfig = deepMerge(defaultControledMihomoConfig, controledMihomoConfig)