diff --git a/src/main/config/profile.ts b/src/main/config/profile.ts index a199686..5761359 100644 --- a/src/main/config/profile.ts +++ b/src/main/config/profile.ts @@ -107,6 +107,7 @@ export async function updateProfileItem(item: IProfileItem): Promise { export async function addProfileItem(item: Partial): Promise { const newItem = await createProfile(item) let shouldChangeCurrent = false + let newProfileIsCurrentAfterUpdate = false await updateProfileConfig((config) => { const existingIndex = config.items.findIndex((i) => i.id === newItem.id) if (existingIndex !== -1) { @@ -116,10 +117,25 @@ export async function addProfileItem(item: Partial): Promise } if (!config.current) { shouldChangeCurrent = true + newProfileIsCurrentAfterUpdate = true } return config }) + // If the new profile will become the current profile, ensure generateProfile is called + // to prepare working directory before restarting core + if (newProfileIsCurrentAfterUpdate) { + const { diffWorkDir } = await getAppConfig() + if (diffWorkDir) { + try { + const { generateProfile } = await import('../core/factory') + await generateProfile() + } catch (error) { + profileLogger.warn('Failed to generate profile for new subscription', error) + } + } + } + if (shouldChangeCurrent) { await changeCurrentProfile(newItem.id) }