merge yaml anchor

This commit is contained in:
pompurin404 2024-09-16 12:44:08 +08:00
parent 64902fb598
commit cf56966fbe
No known key found for this signature in database
7 changed files with 7 additions and 8 deletions

View File

@ -9,7 +9,7 @@ let appConfig: IAppConfig // config.yaml
export async function getAppConfig(force = false): Promise<IAppConfig> { export async function getAppConfig(force = false): Promise<IAppConfig> {
if (force || !appConfig) { if (force || !appConfig) {
const data = await readFile(appConfigPath(), 'utf-8') const data = await readFile(appConfigPath(), 'utf-8')
appConfig = yaml.parse(data) || defaultConfig appConfig = yaml.parse(data, { merge: true }) || defaultConfig
} }
if (typeof appConfig !== 'object') appConfig = defaultConfig if (typeof appConfig !== 'object') appConfig = defaultConfig
return appConfig return appConfig

View File

@ -12,7 +12,7 @@ let controledMihomoConfig: Partial<IMihomoConfig> // mihomo.yaml
export async function getControledMihomoConfig(force = false): Promise<Partial<IMihomoConfig>> { export async function getControledMihomoConfig(force = false): Promise<Partial<IMihomoConfig>> {
if (force || !controledMihomoConfig) { if (force || !controledMihomoConfig) {
const data = await readFile(controledMihomoConfigPath(), 'utf-8') const data = await readFile(controledMihomoConfigPath(), 'utf-8')
controledMihomoConfig = yaml.parse(data) || defaultControledMihomoConfig controledMihomoConfig = yaml.parse(data, { merge: true }) || defaultControledMihomoConfig
} }
if (typeof controledMihomoConfig !== 'object') if (typeof controledMihomoConfig !== 'object')
controledMihomoConfig = defaultControledMihomoConfig controledMihomoConfig = defaultControledMihomoConfig

View File

@ -10,7 +10,7 @@ let overrideConfig: IOverrideConfig // override.yaml
export async function getOverrideConfig(force = false): Promise<IOverrideConfig> { export async function getOverrideConfig(force = false): Promise<IOverrideConfig> {
if (force || !overrideConfig) { if (force || !overrideConfig) {
const data = await readFile(overrideConfigPath(), 'utf-8') const data = await readFile(overrideConfigPath(), 'utf-8')
overrideConfig = yaml.parse(data) || { items: [] } overrideConfig = yaml.parse(data, { merge: true }) || { items: [] }
} }
if (typeof overrideConfig !== 'object') overrideConfig = { items: [] } if (typeof overrideConfig !== 'object') overrideConfig = { items: [] }
return overrideConfig return overrideConfig

View File

@ -14,7 +14,7 @@ let profileConfig: IProfileConfig // profile.yaml
export async function getProfileConfig(force = false): Promise<IProfileConfig> { export async function getProfileConfig(force = false): Promise<IProfileConfig> {
if (force || !profileConfig) { if (force || !profileConfig) {
const data = await readFile(profileConfigPath(), 'utf-8') const data = await readFile(profileConfigPath(), 'utf-8')
profileConfig = yaml.parse(data) || { items: [] } profileConfig = yaml.parse(data, { merge: true }) || { items: [] }
} }
if (typeof profileConfig !== 'object') profileConfig = { items: [] } if (typeof profileConfig !== 'object') profileConfig = { items: [] }
return profileConfig return profileConfig
@ -169,7 +169,7 @@ export async function setProfileStr(id: string, content: string): Promise<void>
export async function getProfile(id: string | undefined): Promise<IMihomoConfig> { export async function getProfile(id: string | undefined): Promise<IMihomoConfig> {
const profile = await getProfileStr(id) const profile = await getProfileStr(id)
let result = yaml.parse(profile) || {} let result = yaml.parse(profile, { merge: true }) || {}
if (typeof result !== 'object') result = {} if (typeof result !== 'object') result = {}
return result return result
} }

View File

@ -44,7 +44,7 @@ async function overrideProfile(
profile = runOverrideScript(profile, content, item) profile = runOverrideScript(profile, content, item)
break break
case 'yaml': { case 'yaml': {
let patch = yaml.parse(content) || {} let patch = yaml.parse(content, { merge: true }) || {}
if (typeof patch !== 'object') patch = {} if (typeof patch !== 'object') patch = {}
profile = deepMerge(profile, patch) profile = deepMerge(profile, patch)
break break

View File

@ -83,7 +83,6 @@ export const mihomoGroups = async (): Promise<IMihomoMixedGroup[]> => {
const runtime = await getRuntimeConfig() const runtime = await getRuntimeConfig()
const groups: IMihomoMixedGroup[] = [] const groups: IMihomoMixedGroup[] = []
runtime?.['proxy-groups']?.forEach((group: { name: string; url?: string }) => { runtime?.['proxy-groups']?.forEach((group: { name: string; url?: string }) => {
group = Object.assign(group, group['<<'])
const { name, url } = group const { name, url } = group
if (proxies.proxies[name] && 'all' in proxies.proxies[name] && !proxies.proxies[name].hidden) { if (proxies.proxies[name] && 'all' in proxies.proxies[name] && !proxies.proxies[name].hidden) {
const newGroup = proxies.proxies[name] const newGroup = proxies.proxies[name]

View File

@ -21,7 +21,7 @@ export async function checkUpdate(): Promise<IAppVersion | undefined> {
} }
} }
) )
const latest = yaml.parse(res.data) as IAppVersion const latest = yaml.parse(res.data, { merge: true }) as IAppVersion
const currentVersion = app.getVersion() const currentVersion = app.getVersion()
if (latest.version !== currentVersion) { if (latest.version !== currentVersion) {
return latest return latest