Skip to content

Commit d215897

Browse files
committed
feat: add profile migration functionality on setup
1 parent c923735 commit d215897

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

frontend/src/stores/profiles.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { parse } from 'yaml'
55
import { ReadFile, WriteFile } from '@/bridge'
66
import { ProfilesFilePath } from '@/constant/app'
77
import { useAppSettingsStore } from '@/stores'
8-
import { ignoredError, eventBus, stringifyNoFolding } from '@/utils'
8+
import { ignoredError, eventBus, stringifyNoFolding, migrateProfiles } from '@/utils'
99

1010
export const useProfilesStore = defineStore('profiles', () => {
1111
const appSettingsStore = useAppSettingsStore()
@@ -16,6 +16,8 @@ export const useProfilesStore = defineStore('profiles', () => {
1616
const setupProfiles = async () => {
1717
const data = await ignoredError(ReadFile, ProfilesFilePath)
1818
data && (profiles.value = parse(data))
19+
20+
await migrateProfiles(profiles.value, saveProfiles)
1921
}
2022

2123
const saveProfiles = () => {

frontend/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export * from './tray'
1111
export * from './completion'
1212
export * from './interaction'
1313
export * from './eventBus'
14+
export * from './migration'

frontend/src/utils/migration.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export const migrateProfiles = async (profiles: IProfile[], save: () => Promise<string>) => {
2+
let needSync = false
3+
4+
profiles.forEach((profile) => {
5+
profile.dns.rules.forEach((rule) => {
6+
if (typeof rule.enable === 'undefined') {
7+
rule.enable = true
8+
needSync = true
9+
}
10+
})
11+
profile.route.rules.forEach((rule) => {
12+
if (typeof rule.enable === 'undefined') {
13+
rule.enable = true
14+
needSync = true
15+
}
16+
})
17+
})
18+
19+
if (needSync) await save()
20+
}

0 commit comments

Comments
 (0)