Skip to content

Commit 7746003

Browse files
committed
feat: add profile migration functionality on setup
1 parent a183260 commit 7746003

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

frontend/src/stores/profiles.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ReadFile, WriteFile } from '@/bridge'
66
import { ProfilesFilePath } from '@/constant'
77
import { ProxyGroup, RulesetBehavior, RulesetFormat, RuleType } from '@/enums/kernel'
88
import { useAppSettingsStore } from '@/stores'
9-
import { eventBus, ignoredError, stringifyNoFolding } from '@/utils'
9+
import { eventBus, ignoredError, stringifyNoFolding, migrateProfiles } from '@/utils'
1010

1111
export type ProfileType = {
1212
id: string
@@ -150,6 +150,8 @@ export const useProfilesStore = defineStore('profiles', () => {
150150
const setupProfiles = async () => {
151151
const data = await ignoredError(ReadFile, ProfilesFilePath)
152152
data && (profiles.value = parse(data))
153+
154+
await migrateProfiles(profiles.value, saveProfiles)
153155
}
154156

155157
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { ProfileType } from '@/stores'
2+
3+
export const migrateProfiles = async (profiles: ProfileType[], save: () => Promise<string>) => {
4+
let needSync = false
5+
6+
profiles.forEach((profile) => {
7+
profile.rulesConfig.forEach((rule) => {
8+
if (typeof rule.enable === 'undefined') {
9+
rule.enable = true
10+
needSync = true
11+
}
12+
})
13+
})
14+
15+
if (needSync) await save()
16+
}

0 commit comments

Comments
 (0)