import { getControledMihomoConfig, getProfileConfig, getProfile, getProfileItem, getOverride, getOverrideItem } from '../config' import { mihomoWorkConfigPath } from '../utils/dirs' import yaml from 'yaml' import { readFile, writeFile } from 'fs/promises' import { deepMerge } from '../utils/merge' export async function generateProfile(): Promise { const { current } = await getProfileConfig() const currentProfile = await overrideProfile(current, await getProfile(current)) const controledMihomoConfig = await getControledMihomoConfig() const profile = deepMerge(currentProfile, controledMihomoConfig) await writeFile(mihomoWorkConfigPath(), yaml.stringify(profile)) } async function overrideProfile( current: string | undefined, profile: IMihomoConfig ): Promise { const { override = [] } = (await getProfileItem(current)) || {} for (const ov of override) { const item = await getOverrideItem(ov) const content = await getOverride(ov, item?.ext || 'js') switch (item?.ext) { case 'js': profile = runOverrideScript(profile, content) break case 'yaml': profile = deepMerge(profile, yaml.parse(content)) break } } return profile } function runOverrideScript(profile: IMihomoConfig, script: string): IMihomoConfig { try { const func = eval(`${script} main`) const newProfile = func(profile) if (typeof newProfile !== 'object') return profile return newProfile } catch (e) { return profile } } export async function getRuntimeConfigStr(): Promise { return await readFile(mihomoWorkConfigPath(), 'utf8') } export async function getRuntimeConfig(): Promise { return yaml.parse(await getRuntimeConfigStr()) }