mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-21 18:02:33 +01:00
Refactor config utils (#963)
This commit is contained in:
parent
4df7bbcfe5
commit
85c880b230
@ -18,8 +18,9 @@
|
|||||||
|
|
||||||
import { ConfigEntity } from "../entities/Config";
|
import { ConfigEntity } from "../entities/Config";
|
||||||
import fs from "fs/promises";
|
import fs from "fs/promises";
|
||||||
import syncFs from "fs";
|
import { existsSync } from "fs";
|
||||||
import { ConfigValue } from "../config";
|
import { ConfigValue } from "../config";
|
||||||
|
import { OrmUtils } from "..";
|
||||||
|
|
||||||
// TODO: yaml instead of json
|
// TODO: yaml instead of json
|
||||||
const overridePath = process.env.CONFIG_PATH ?? "";
|
const overridePath = process.env.CONFIG_PATH ?? "";
|
||||||
@ -34,32 +35,24 @@ export const Config = {
|
|||||||
init: async function init() {
|
init: async function init() {
|
||||||
if (config) return config;
|
if (config) return config;
|
||||||
console.log("[Config] Loading configuration...");
|
console.log("[Config] Loading configuration...");
|
||||||
pairs = await ConfigEntity.find();
|
if (!process.env.CONFIG_PATH) {
|
||||||
config = pairsToConfig(pairs);
|
pairs = await ConfigEntity.find();
|
||||||
// TODO: this overwrites existing config values with defaults.
|
config = pairsToConfig(pairs);
|
||||||
// we actually want to extend the object with new keys instead.
|
} else {
|
||||||
// config = (config || {}).merge(new ConfigValue());
|
console.log(`[Config] Using CONFIG_PATH rather than database`);
|
||||||
// Object.assign(config, new ConfigValue());
|
if (existsSync(process.env.CONFIG_PATH)) {
|
||||||
|
const file = JSON.parse(
|
||||||
|
(await fs.readFile(process.env.CONFIG_PATH)).toString(),
|
||||||
|
);
|
||||||
|
config = file;
|
||||||
|
} else config = new ConfigValue();
|
||||||
|
pairs = generatePairs(config);
|
||||||
|
}
|
||||||
|
|
||||||
// If a config doesn't exist, create it.
|
// If a config doesn't exist, create it.
|
||||||
if (Object.keys(config).length == 0) config = new ConfigValue();
|
if (Object.keys(config).length == 0) config = new ConfigValue();
|
||||||
|
|
||||||
if (process.env.CONFIG_PATH) {
|
config = OrmUtils.mergeDeep({}, { ...new ConfigValue() }, config);
|
||||||
console.log(
|
|
||||||
`[Config] Using config path from environment rather than database.`,
|
|
||||||
);
|
|
||||||
try {
|
|
||||||
const overrideConfig = JSON.parse(
|
|
||||||
await fs.readFile(overridePath, { encoding: "utf8" }),
|
|
||||||
);
|
|
||||||
config = overrideConfig.merge(config);
|
|
||||||
} catch (error) {
|
|
||||||
await fs.writeFile(
|
|
||||||
overridePath,
|
|
||||||
JSON.stringify(config, null, 4),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.set(config);
|
return this.set(config);
|
||||||
},
|
},
|
||||||
@ -83,29 +76,31 @@ export const Config = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
function applyConfig(val: ConfigValue) {
|
// TODO: better types
|
||||||
// TODO: typings
|
const generatePairs = (obj: object | null, key = ""): ConfigEntity[] => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
if (typeof obj == "object" && obj != null) {
|
||||||
async function apply(obj: any, key = ""): Promise<any> {
|
return Object.keys(obj)
|
||||||
if (typeof obj === "object" && obj !== null)
|
.map((k) =>
|
||||||
return Promise.all(
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
Object.keys(obj).map((k) =>
|
generatePairs((obj as any)[k], key ? `${key}_${k}` : k),
|
||||||
apply(obj[k], key ? `${key}_${k}` : k),
|
)
|
||||||
),
|
.flat();
|
||||||
);
|
|
||||||
|
|
||||||
let pair = pairs.find((x) => x.key === key);
|
|
||||||
if (!pair) pair = new ConfigEntity();
|
|
||||||
|
|
||||||
pair.key = key;
|
|
||||||
pair.value = obj;
|
|
||||||
return pair.save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (process.env.CONFIG_PATH)
|
const ret = new ConfigEntity();
|
||||||
syncFs.writeFileSync(overridePath, JSON.stringify(val, null, 4));
|
ret.key = key;
|
||||||
|
ret.value = obj;
|
||||||
|
return [ret];
|
||||||
|
};
|
||||||
|
|
||||||
return apply(val);
|
async function applyConfig(val: ConfigValue) {
|
||||||
|
if (process.env.CONFIG_PATH)
|
||||||
|
await fs.writeFile(overridePath, JSON.stringify(val, null, 4));
|
||||||
|
else {
|
||||||
|
const pairs = generatePairs(val);
|
||||||
|
await Promise.all(pairs.map((pair) => pair.save()));
|
||||||
|
}
|
||||||
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
function pairsToConfig(pairs: ConfigEntity[]) {
|
function pairsToConfig(pairs: ConfigEntity[]) {
|
||||||
|
Loading…
Reference in New Issue
Block a user