mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-24 19:32:46 +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...");
|
||||||
|
if (!process.env.CONFIG_PATH) {
|
||||||
pairs = await ConfigEntity.find();
|
pairs = await ConfigEntity.find();
|
||||||
config = pairsToConfig(pairs);
|
config = pairsToConfig(pairs);
|
||||||
// TODO: this overwrites existing config values with defaults.
|
} else {
|
||||||
// we actually want to extend the object with new keys instead.
|
console.log(`[Config] Using CONFIG_PATH rather than database`);
|
||||||
// config = (config || {}).merge(new ConfigValue());
|
if (existsSync(process.env.CONFIG_PATH)) {
|
||||||
// Object.assign(config, new ConfigValue());
|
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[] => {
|
||||||
|
if (typeof obj == "object" && obj != null) {
|
||||||
|
return Object.keys(obj)
|
||||||
|
.map((k) =>
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
async function apply(obj: any, key = ""): Promise<any> {
|
generatePairs((obj as any)[k], key ? `${key}_${k}` : k),
|
||||||
if (typeof obj === "object" && obj !== null)
|
)
|
||||||
return Promise.all(
|
.flat();
|
||||||
Object.keys(obj).map((k) =>
|
|
||||||
apply(obj[k], key ? `${key}_${k}` : k),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
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