-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparseUserConfig.ts
More file actions
65 lines (55 loc) · 1.38 KB
/
parseUserConfig.ts
File metadata and controls
65 lines (55 loc) · 1.38 KB
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
import { readFile } from "fs-extra";
import { load, YAMLException } from "js-yaml";
import { parse } from "json5";
import { extname } from "path";
import log from "@utils/log";
const parseUserConfig = async (
configFilePath: string
): Promise<Record<string, unknown> | false> => {
const ext = extname(configFilePath);
if (ext.match(/^\.js$/)) {
try {
return require(configFilePath);
} catch (error) {
log.configJSError(error as Error);
return false;
}
}
const content = await readFile(configFilePath, "utf8");
if (ext.match(/^\.ya?ml$/)) {
try {
return load(content) as Record<string, unknown>;
} catch (error) {
log.configYamlError(error as YAMLException);
return false;
}
}
if (ext.match(/^\.json[c5]?$/)) {
try {
return parse(content, (key, value) => {
if (typeof value == "string") {
const matches = value.match(/%env:([\w-]+)%/g);
if (matches) {
matches.forEach((match) => {
const env = match.match(/%env:([\w-]+)%/)![1];
if (process.env[env]) {
value = value.replace(
`%env:${env}%`,
process.env[env]
);
} else {
log.configENVNotFound(match, env);
}
});
}
}
return value;
});
} catch (error) {
log.configJSONError(error as Error);
return false;
}
}
throw new Error(`Unsupported Extension: ${ext}`);
};
export default parseUserConfig;