2024-08-07 10:47:14 +08:00
|
|
|
import { merge } from "lodash";
|
2024-08-06 21:47:34 +08:00
|
|
|
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: {
|
2024-08-06 21:47:34 +08:00
|
|
|
accent_color: "#8ab4f8" as string,
|
2024-08-07 11:43:32 +08:00
|
|
|
dark_mode: true as boolean,
|
2024-08-06 21:47:34 +08:00
|
|
|
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 21:47:34 +08:00
|
|
|
},
|
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
|
|
|
},
|
2024-08-06 21:47:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export type SettingsType = typeof defaultSettings;
|
|
|
|
|
|
|
|
export const readConfigFile = async (): Promise<SettingsType> => {
|
|
|
|
let existingData: SettingsType = defaultSettings;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// try to read the config file
|
2024-08-07 10:47:14 +08:00
|
|
|
await Paths.initialize();
|
2024-08-06 21:47:34 +08:00
|
|
|
const data = await readTomlFile<SettingsType>(Paths.getPath("configDirectory") + "config.toml");
|
|
|
|
if (data) {
|
|
|
|
existingData = data;
|
2024-08-07 10:47:14 +08:00
|
|
|
console.log("existing data");
|
|
|
|
console.log(existingData);
|
2024-08-06 21:47:34 +08:00
|
|
|
}
|
|
|
|
} 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
|
2024-08-07 10:47:14 +08:00
|
|
|
const mergedSettings = merge({}, defaultSettings, existingData);
|
2024-08-06 21:47:34 +08:00
|
|
|
|
|
|
|
return mergedSettings;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const writeConfigFile = async (settingsValues: SettingsType): Promise<void> => {
|
|
|
|
await writeTomlFile<SettingsType>(Paths.getPath("configDirectory") + "config.toml", settingsValues);
|
2024-08-07 10:47:14 +08:00
|
|
|
console.debug("Settings file written successfully.");
|
2024-08-06 21:47:34 +08:00
|
|
|
};
|