This repository has been archived by the owner on Aug 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathconfig.ts
66 lines (59 loc) · 1.62 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
import path from "path";
import fs from "fs";
import process from "process";
// Set some default configuration
const server = {
loggerLevel: "info"
};
const timeZone = "Pacific/Auckland";
function loadConfigFromArgs(strict: boolean = false) {
return loadConfig(getConfigPathFromArgs(strict));
}
function getConfigPathFromArgs(strict: boolean = false): string {
let configPath = "./config/app.js";
for (let i = 2; i < process.argv.length; i++) {
const val = process.argv[i];
if (val.startsWith("--config=")) {
configPath = val.split("=")[1];
} else if (val == "--config") {
i++;
configPath = process.argv[i];
} else if (strict) {
throw new Error(
`Cannot parse '${val}'. The only accepted parameter is --config=<path-to-config-file>`
);
}
}
return configPath;
}
function loadConfig(configPath) {
configPath = path.resolve(configPath);
checkConfigFileExists(configPath);
const config = require(configPath).default;
checkDatabaseConfigAvailable(config);
return config;
}
function checkConfigFileExists(configPath) {
if (!fs.existsSync(configPath)) {
throw (
"Config file " +
configPath +
" does not exist. See README.md for config setup. " +
"NB: The default config file has been renamed to ./config/app.js"
);
}
}
function checkDatabaseConfigAvailable(config) {
if (!("database" in config)) {
throw "Could not find database configuration. database.js has been merged into app.js";
}
}
export default {
getConfigPathFromArgs,
loadConfigFromArgs,
loadConfig,
timeZone,
server,
euaVersion: 3,
...loadConfigFromArgs()
};