diff --git a/client/public/config.toml b/client/public/config.toml new file mode 100644 index 0000000..cfae125 --- /dev/null +++ b/client/public/config.toml @@ -0,0 +1,4 @@ +# Конфиг таймера + +[sound] +sound_id = "alarm_beep" diff --git a/client/src/composables/useConfig.ts b/client/src/composables/useConfig.ts new file mode 100644 index 0000000..ac01cae --- /dev/null +++ b/client/src/composables/useConfig.ts @@ -0,0 +1,36 @@ +import { readTextFile, writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs'; +import TOML from 'toml'; +import { Config } from '@/types/configType'; + + +/** + * Загрузит или создаст TOML-конфиг + */ +export async function loadConfig(): Promise { + let text: string | null = null; + const configName = 'config.toml'; + + // 1. пробуем прочитать пользовательский конфиг + try { + text = await readTextFile(configName, { + baseDir: BaseDirectory.AppConfig + }); + } catch { + text = null; + } + + // 2. если нет — создаст из дефолта + if (!text) { + const defaultConfig = await fetch(`/${configName}`) + .then(r => r.text()); + + await writeTextFile(configName, defaultConfig, { + baseDir: BaseDirectory.AppConfig + }); + + text = defaultConfig; + } + + // 3. парсим TOML + return TOML.parse(text) as Config; +} diff --git a/client/src/types/configType.ts b/client/src/types/configType.ts new file mode 100644 index 0000000..b30194d --- /dev/null +++ b/client/src/types/configType.ts @@ -0,0 +1,6 @@ +//** Тип для конфига */ +export type Config = { + sound: { + sound_id: string; + } +}