feat: added work with config toml
This commit is contained in:
4
client/public/config.toml
Normal file
4
client/public/config.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
# Конфиг таймера
|
||||
|
||||
[sound]
|
||||
sound_id = "alarm_beep"
|
||||
36
client/src/composables/useConfig.ts
Normal file
36
client/src/composables/useConfig.ts
Normal file
@@ -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<Config> {
|
||||
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;
|
||||
}
|
||||
6
client/src/types/configType.ts
Normal file
6
client/src/types/configType.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
//** Тип для конфига */
|
||||
export type Config = {
|
||||
sound: {
|
||||
sound_id: string;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user