-
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.
- Loading branch information
Showing
6 changed files
with
92 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,7 +1,30 @@ | ||
import path from 'node:path'; | ||
import { FileSystemStorage } from '/shared/types/storage'; | ||
import { SETTINGS_DIRECTORY } from '/shared/paths'; | ||
import { SETTINGS_DIRECTORY, CONFIG_FILE_NAME, getFullScenesPath } from '/shared/paths'; | ||
import { DEFAULT_CONFIG, mergeConfig } from '/shared/types/config'; | ||
import { getUserDataPath } from './electron'; | ||
|
||
export const CONFIG_PATH = path.join(getUserDataPath(), SETTINGS_DIRECTORY, 'config.json'); | ||
export const config = await FileSystemStorage.getOrCreate(CONFIG_PATH); | ||
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()); | ||
|
||
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 const config = storage; |
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,3 +1,10 @@ | ||
import path from 'node:path'; | ||
|
||
export const SCENES_DIRECTORY = 'Scenes'; | ||
export const SETTINGS_DIRECTORY = 'Settings'; | ||
export const CUSTOM_ASSETS_DIRECTORY = 'Custom Items'; | ||
export const CONFIG_FILE_NAME = 'config.json'; | ||
|
||
export function getFullScenesPath(userDataPath: string): string { | ||
return path.join(userDataPath, SCENES_DIRECTORY); | ||
} |
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,9 +1,31 @@ | ||
import deepmerge from 'deepmerge'; | ||
import { type AppSettings } from './settings'; | ||
import { DEFAULT_DEPENDENCY_UPDATE_STRATEGY } from './settings'; | ||
import { SCENES_DIRECTORY } from '/shared/paths'; | ||
|
||
export type Config = { | ||
version: number; | ||
workspace: { | ||
paths: string[]; | ||
}; | ||
settings: AppSettings; | ||
userId?: string; | ||
}; | ||
|
||
export const DEFAULT_CONFIG: Config = { | ||
version: 1, | ||
workspace: { | ||
paths: [], | ||
}, | ||
settings: { | ||
scenesPath: SCENES_DIRECTORY, // Base directory name, will be joined with userDataPath by main/preload | ||
dependencyUpdateStrategy: DEFAULT_DEPENDENCY_UPDATE_STRATEGY, | ||
}, | ||
}; | ||
|
||
export function mergeConfig(target: Partial<Config>, source: Config): Config { | ||
return deepmerge(source, target, { | ||
// Clone arrays instead of merging them | ||
arrayMerge: (_, sourceArray) => sourceArray, | ||
}); | ||
} |