-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ts
71 lines (61 loc) · 2.04 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import * as process from "process";
import DB from "./db";
import Robot from "./robot";
// const CONFIG_COLLECTION = "config"
export default class Config {
private db: DB;
private robot: Robot;
private loadedConfig: {[key: string]: any} = {};
private defaultConfig: {[key: string]: any} = {};
constructor(robot: Robot, db: DB, defaultConfig?: {[key: string]: any}) {
this.robot = robot;
this.db = db;
this.defaultConfig = defaultConfig;
}
public async init() {
await this.refreshConfig();
await this.setDefaultConfig();
}
public async refreshConfig() {
this.loadedConfig = await this.db.getConfig();
}
public requireKeys(...keys) {
for (let key of keys) {
this.get(key);
}
}
public async set(key: string, value: any) {
const config = await this.db.getConfig();
config[key] = value;
await this.db.setConfig(config);
await this.refreshConfig();
}
public async setDefaultConfig() {
if (this.loadedConfig && Object.keys(this.loadedConfig).length > 0) {
this.robot.logger.error(
"[config] Not setting default config since there is config data already there"
);
return;
}
// eslint-disable-next-line @typescript-eslint/no-var-requires
const exampleConfig = require("../configuration.example.json");
await this.db.setConfig(exampleConfig);
for (let key of Object.keys(exampleConfig)) {
console.log("Loading default", key, exampleConfig[key]);
}
}
// Fetch a config variable. Picks environment variables, then config file variables, then
public get(key: string, defaultValue: any = undefined) {
if (process.env[key] !== undefined) {
return process.env[key];
} else if (this.loadedConfig[key] !== undefined) {
return this.loadedConfig[key];
} else if (this.defaultConfig && this.defaultConfig[key] !== undefined) {
return this.defaultConfig[key];
} else if (defaultValue !== undefined) {
return defaultValue;
} else {
throw new Error(`Cannot find config variable: ${key}`);
}
}
}