-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-config.ts
32 lines (26 loc) · 834 Bytes
/
parse-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
import fs from "fs";
import YAML from "yaml";
import { Config } from "../types/config";
import { validateConfig } from "./validate-config";
export const parseConfig = (filePath: string): Config => {
const file = fs
.readFileSync(filePath, "utf8")
.replace(/\$\{(.+?)\}/g, (match, name) => process.env[name] || match);
const config = YAML.parse(file);
const valid = validateConfig(config);
if (!valid) {
throw new Error(
`Invalid config: ${JSON.stringify(validateConfig.errors, null, 2)}`,
);
}
const endpointNames = new Set<string>();
config.endpoints.forEach((endpoint) => {
if (endpointNames.has(endpoint.name)) {
throw new Error(
`Invalid config: Duplicate endpoint name "${endpoint.name}"`,
);
}
endpointNames.add(endpoint.name);
});
return config;
};