✨ config override file
This commit is contained in:
parent
b610e3f872
commit
3a76f0f33c
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,3 +8,4 @@ database.db
|
|||||||
tsconfig.tsbuildinfo
|
tsconfig.tsbuildinfo
|
||||||
files/
|
files/
|
||||||
.env
|
.env
|
||||||
|
config.json
|
@ -1,5 +1,10 @@
|
|||||||
import "missing-native-js-functions";
|
import "missing-native-js-functions";
|
||||||
import { ConfigValue, ConfigEntity, DefaultConfigOptions } from "../entities/Config";
|
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 config: ConfigValue;
|
||||||
var pairs: ConfigEntity[];
|
var pairs: ConfigEntity[];
|
||||||
@ -12,8 +17,16 @@ export const Config = {
|
|||||||
if (config) return config;
|
if (config) return config;
|
||||||
pairs = await ConfigEntity.find();
|
pairs = await ConfigEntity.find();
|
||||||
config = pairsToConfig(pairs);
|
config = pairsToConfig(pairs);
|
||||||
|
config = (config || {}).merge(DefaultConfigOptions);
|
||||||
|
|
||||||
return this.set((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);
|
||||||
},
|
},
|
||||||
get: function get() {
|
get: function get() {
|
||||||
return config;
|
return config;
|
||||||
@ -38,6 +51,7 @@ function applyConfig(val: ConfigValue) {
|
|||||||
pair.value = obj;
|
pair.value = obj;
|
||||||
return pair.save();
|
return pair.save();
|
||||||
}
|
}
|
||||||
|
fs.writeFileSync(overridePath, JSON.stringify(val, null, 4));
|
||||||
|
|
||||||
return apply(val);
|
return apply(val);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user