@@ -38,9 +38,21 @@ export async function changeCurrentProfile(id: string): Promise<void> {
38
38
}
39
39
}
40
40
41
+ export async function updateProfileItem ( item : IProfileItem ) : Promise < void > {
42
+ const index = profileConfig . items . findIndex ( ( i ) => i . id === item . id )
43
+ profileConfig . items [ index ] = item
44
+ fs . writeFileSync ( profileConfigPath ( ) , yaml . stringify ( profileConfig ) )
45
+ window ?. webContents . send ( 'profileConfigUpdated' )
46
+ }
47
+
41
48
export async function addProfileItem ( item : Partial < IProfileItem > ) : Promise < void > {
42
49
const newItem = await createProfile ( item )
43
- profileConfig . items . push ( newItem )
50
+ if ( profileConfig . items . find ( ( i ) => i . id === newItem . id ) ) {
51
+ updateProfileItem ( newItem )
52
+ } else {
53
+ profileConfig . items . push ( newItem )
54
+ }
55
+
44
56
if ( ! getProfileConfig ( ) . current ) {
45
57
changeCurrentProfile ( newItem . id )
46
58
}
@@ -134,7 +146,7 @@ export async function createProfile(item: Partial<IProfileItem>): Promise<IProfi
134
146
if ( headers [ 'subscription-userinfo' ] ) {
135
147
newItem . extra = parseSubinfo ( headers [ 'subscription-userinfo' ] )
136
148
}
137
- fs . writeFileSync ( profilePath ( id ) , data , 'utf-8' )
149
+ setProfileStr ( id , data )
138
150
} catch ( e ) {
139
151
dialog . showErrorBox ( 'Failed to fetch remote profile' , `${ e } \nurl: ${ item . url } ` )
140
152
throw new Error ( `Failed to fetch remote profile ${ e } ` )
@@ -150,21 +162,33 @@ export async function createProfile(item: Partial<IProfileItem>): Promise<IProfi
150
162
throw new Error ( 'File is required for local profile' )
151
163
}
152
164
const data = item . file
153
- fs . writeFileSync ( profilePath ( id ) , yaml . stringify ( data ) )
165
+ setProfileStr ( id , data )
154
166
break
155
167
}
156
168
}
157
169
158
170
return newItem
159
171
}
160
172
173
+ export function getProfileStr ( id : string ) : string {
174
+ return fs . readFileSync ( profilePath ( id ) , 'utf-8' )
175
+ }
176
+
177
+ export function setProfileStr ( id : string , content : string ) : void {
178
+ fs . writeFileSync ( profilePath ( id ) , content , 'utf-8' )
179
+ if ( id === getProfileConfig ( ) . current ) {
180
+ getCurrentProfile ( true )
181
+ restartCore ( )
182
+ }
183
+ }
184
+
161
185
export function getCurrentProfile ( force = false ) : Partial < IMihomoConfig > {
162
186
if ( force || ! currentProfile ) {
163
187
const current = getProfileConfig ( ) . current
164
188
if ( current ) {
165
- currentProfile = yaml . parse ( fs . readFileSync ( profilePath ( current ) , 'utf-8' ) )
189
+ currentProfile = yaml . parse ( getProfileStr ( current ) )
166
190
} else {
167
- currentProfile = yaml . parse ( fs . readFileSync ( profilePath ( 'default' ) , 'utf-8 ') )
191
+ currentProfile = yaml . parse ( getProfileStr ( 'default' ) )
168
192
}
169
193
}
170
194
return currentProfile
0 commit comments