stort/src/lib/settings.ts

58 lines
1.8 KiB
TypeScript
Raw Normal View History

import { merge } from "lodash";
import Paths from "./path";
import { readTomlFile, writeTomlFile } from "./utils/toml";
export const defaultSettings = {
2024-08-07 12:15:01 +08:00
background: {
2024-08-07 15:04:05 +08:00
background_color: "#202124" as string,
background_image_path: "" as string,
2024-08-07 12:15:01 +08:00
},
style: {
accent_color: "#8ab4f8" as string,
2024-08-07 11:43:32 +08:00
dark_mode: true as boolean,
font_family: "monospace" as string,
font_scaling: 100,
2024-08-07 11:43:32 +08:00
radius: 8 as number,
transition_duration: 200 as number,
window_height: 80 as number,
window_width: 400 as number,
},
2024-08-06 23:31:05 +08:00
window: {
maximize_button: false as boolean,
2024-08-07 11:43:32 +08:00
minimize_button: false as boolean,
start_fullscreen: false as boolean, // TODO: this should be true on prod
2024-08-06 23:31:05 +08:00
},
};
export type SettingsType = typeof defaultSettings;
export const readConfigFile = async (): Promise<SettingsType> => {
let existingData: SettingsType = defaultSettings;
try {
// try to read the config file
await Paths.initialize();
const data = await readTomlFile<SettingsType>(Paths.getPath("configDirectory") + "config.toml");
if (data) {
existingData = data;
console.log("existing data");
console.log(existingData);
}
} catch (error) {
// file does not exist, called function will throw error
console.error(`Failed to read settings file: ${error}, using default settings.`);
existingData = defaultSettings;
}
// merge the existing data with the default settings
// existing data will overwrite the default settings for the properties that exist
const mergedSettings = merge({}, defaultSettings, existingData);
return mergedSettings;
};
export const writeConfigFile = async (settingsValues: SettingsType): Promise<void> => {
await writeTomlFile<SettingsType>(Paths.getPath("configDirectory") + "config.toml", settingsValues);
console.debug("Settings file written successfully.");
};