From ec4234f0a7fedd25f4da44c00f4eebbe2f8a6d92 Mon Sep 17 00:00:00 2001 From: Makisuo <31933546+Makisuo@users.noreply.github.com> Date: Wed, 15 Jan 2025 11:31:00 +0100 Subject: [PATCH] add parsing --- src/utils/config.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/utils/config.ts b/src/utils/config.ts index 6da1708..11c5a40 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -26,6 +26,16 @@ export class ConfigManager { this.filePath = path.resolve(process.cwd(), options.filePath) } + parseConfig(config: Config): Config { + const out = configType(config) + + if (out instanceof type.errors) { + throw new Error(`Failed to parse config: ${out.message}`) + } + + return config + } + async doesConfigExist(): Promise { try { await fs.access(this.filePath) @@ -37,11 +47,7 @@ export class ConfigManager { async loadConfig(): Promise { const data = await fs.readFile(this.filePath, "utf-8") - const out = configType(JSON.parse(data)) - - if (out instanceof type.errors) { - throw new Error(`Failed to parse config: ${out.message}`) - } + const out = this.parseConfig(JSON.parse(data)) return out } @@ -50,7 +56,9 @@ export class ConfigManager { try { const currentConfig = await this.loadConfig() const updatedConfig = { ...currentConfig, ...updates } - await fs.writeFile(this.filePath, JSON.stringify(updatedConfig, null, 2), "utf-8") + const parsed = this.parseConfig(updatedConfig) + + await fs.writeFile(this.filePath, JSON.stringify(parsed, null, 2), "utf-8") return updatedConfig } catch (error) { throw new Error(`Failed to update config: ${error}`) @@ -59,9 +67,10 @@ export class ConfigManager { async createConfig(config: Config): Promise { try { + const parsed = this.parseConfig(config) const dirPath = path.dirname(this.filePath) await fs.mkdir(dirPath, { recursive: true }) - await fs.writeFile(this.filePath, JSON.stringify(config, null, 2), "utf-8") + await fs.writeFile(this.filePath, JSON.stringify(parsed, null, 2), "utf-8") return config } catch (error) { throw new Error(`Failed to create config: ${error}`)