From 3a76f0f33c61f31a04e93ac5b2d7594c25b43c4a Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 17 Oct 2021 22:27:54 +0200 Subject: :sparkles: config override file --- .gitignore | 3 ++- util/src/util/Config.ts | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a09d215a..bd301f07 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ api/assets/*.css database.db tsconfig.tsbuildinfo files/ -.env \ No newline at end of file +.env +config.json \ No newline at end of file diff --git a/util/src/util/Config.ts b/util/src/util/Config.ts index 704f3f2f..92907d0c 100644 --- a/util/src/util/Config.ts +++ b/util/src/util/Config.ts @@ -1,5 +1,10 @@ import "missing-native-js-functions"; import { ConfigValue, ConfigEntity, DefaultConfigOptions } from "../entities/Config"; +import path from "path"; +import fs from "fs"; + +// TODO: yaml instead of json +const overridePath = path.join(process.cwd(), "config.json"); var config: ConfigValue; var pairs: ConfigEntity[]; @@ -12,8 +17,16 @@ export const Config = { if (config) return config; pairs = await ConfigEntity.find(); config = pairsToConfig(pairs); + config = (config || {}).merge(DefaultConfigOptions); + + try { + const overrideConfig = JSON.parse(fs.readFileSync(overridePath, { encoding: "utf8" })); + config = overrideConfig.merge(config); + } catch (error) { + fs.writeFileSync(overridePath, JSON.stringify(config, null, 4)); + } - return this.set((config || {}).merge(DefaultConfigOptions)); + return this.set(config); }, get: function get() { return config; @@ -38,6 +51,7 @@ function applyConfig(val: ConfigValue) { pair.value = obj; return pair.save(); } + fs.writeFileSync(overridePath, JSON.stringify(val, null, 4)); return apply(val); } -- cgit 1.4.1