Files
UDN/config-handler.ts
T

31 lines
688 B
TypeScript
Raw Normal View History

2024-07-05 16:04:12 +02:00
// CONFIG
const configPath = "config.json";
export const defaultConfig = {
port: 3000,
connectedServers: [],
subscribedChannels: [],
};
// METHODS
export async function createConfig() {
const configString = JSON.stringify(defaultConfig, null, 4);
2024-07-05 19:58:42 +02:00
await Bun.write(configPath, configString);
2024-07-05 16:04:12 +02:00
}
export async function getConfig(): Promise<typeof defaultConfig> {
try {
2024-07-05 19:58:42 +02:00
const configString = await Bun.file(configPath).text();
2024-07-05 16:04:12 +02:00
const configObject = JSON.parse(configString);
Object.keys(defaultConfig).forEach((key) => {
if (!configObject[key]) throw null;
});
return configObject;
} catch {
2024-07-07 18:06:12 +02:00
await createConfig();
2024-07-05 16:04:12 +02:00
return defaultConfig;
}
}