-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: config reset on app restart and improve migrations handling (#394)
* fix: config reset * fix: add migration that auto imports all scenes
- Loading branch information
Showing
6 changed files
with
130 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,36 @@ | ||
import path from 'node:path'; | ||
import { FileSystemStorage } from '/shared/types/storage'; | ||
import { FileSystemStorage, type IFileSystemStorage } from '/shared/types/storage'; | ||
import { SETTINGS_DIRECTORY, CONFIG_FILE_NAME, getFullScenesPath } from '/shared/paths'; | ||
import { DEFAULT_CONFIG, mergeConfig } from '/shared/types/config'; | ||
import { DEFAULT_CONFIG, mergeConfig, type Config } from '/shared/types/config'; | ||
import { getUserDataPath } from './electron'; | ||
import { waitForMigrations } from './migrations'; | ||
import log from 'electron-log/main'; | ||
|
||
export const CONFIG_PATH = path.join(getUserDataPath(), SETTINGS_DIRECTORY, CONFIG_FILE_NAME); | ||
const storage = await FileSystemStorage.getOrCreate(CONFIG_PATH); | ||
|
||
// Initialize with default values if empty | ||
const existingConfig = await storage.get<Record<string, any>>(''); | ||
const defaultConfig = { ...DEFAULT_CONFIG }; | ||
defaultConfig.settings.scenesPath = getFullScenesPath(getUserDataPath()); | ||
let configStorage: IFileSystemStorage | undefined; | ||
|
||
if (!existingConfig || Object.keys(existingConfig).length === 0) { | ||
// Write the default config | ||
for (const [key, value] of Object.entries(defaultConfig)) { | ||
await storage.set(key, value); | ||
} | ||
} else { | ||
// Deep merge with defaults if config exists but might be missing properties | ||
const mergedConfig = mergeConfig(existingConfig, defaultConfig); | ||
if (JSON.stringify(existingConfig) !== JSON.stringify(mergedConfig)) { | ||
for (const [key, value] of Object.entries(mergedConfig)) { | ||
await storage.set(key, value); | ||
export async function getConfig(): Promise<IFileSystemStorage> { | ||
await waitForMigrations(); | ||
|
||
if (!configStorage) { | ||
configStorage = await FileSystemStorage.getOrCreate(CONFIG_PATH); | ||
|
||
// Initialize with default values if empty or merge with defaults if partial | ||
const defaultConfig = { ...DEFAULT_CONFIG }; | ||
defaultConfig.settings.scenesPath = getFullScenesPath(getUserDataPath()); | ||
|
||
const existingConfig = await configStorage.getAll<Partial<Config>>(); | ||
|
||
// Deep merge with defaults if config exists but might be missing properties | ||
const mergedConfig = mergeConfig(existingConfig, defaultConfig); | ||
if (JSON.stringify(existingConfig) !== JSON.stringify(mergedConfig)) { | ||
log.info('[Config] Writing merged config to storage'); | ||
await configStorage.setAll(mergedConfig); | ||
} else { | ||
log.info('[Config] Config already exists and is up to date'); | ||
} | ||
} | ||
} | ||
|
||
export const config = storage; | ||
return configStorage; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters