-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathsettings.data.ts
More file actions
102 lines (94 loc) · 2.73 KB
/
Copy pathsettings.data.ts
File metadata and controls
102 lines (94 loc) · 2.73 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import * as fs from "node:fs";
import { load } from "js-toml";
import markdownit from "markdown-it";
const md = markdownit();
export default {
watch: ["./settings.toml"],
load() {
const settings = {};
const raw = fs.readFileSync("./settings.toml", "utf-8");
const doc = load(raw);
function getParseEnv(parseEnv) {
if (parseEnv === "list_by_comma") {
return "comma";
}
if (parseEnv === "list_by_colon") {
return "colon";
}
return undefined;
}
const typeMap = {
String: "string",
Path: "string",
Url: "string",
Duration: "string",
Bool: "boolean",
Integer: "integer",
ListString: "string[]",
ListPath: "string[]",
SetString: "string[]",
"IndexMap<String, String>": "object",
BoolOrString: "boolean | string",
};
function buildElement(key, props) {
const type = typeMap[props.type] || props.type;
let default_ = props.default_docs ?? props.default;
if (default_ === undefined && type === "boolean" && !props.optional) {
default_ = false;
}
if (default_ === undefined && props.optional) {
default_ = "None";
}
const ele = {
key,
default: default_,
docs: md.render(props.docs ?? props.description),
deprecated: props.deprecated,
enum: props.enum?.map((choice) =>
typeof choice === "object" && choice !== null && choice.description
? {
...choice,
description: md.renderInline(choice.description),
}
: choice,
),
env: props.env,
parseEnv: getParseEnv(props.parse_env),
optional: !props.default_docs && props.optional,
type,
};
return ele;
}
function appendSettings(group, node, path) {
for (const key in node) {
const props = node[key];
if (typeof props !== "object" || props === null || props.hide) {
continue;
}
const settingPath = [...path, key];
if (props.type) {
group.settings.push(buildElement(settingPath.join("."), props));
} else {
appendSettings(group, props, settingPath);
}
}
}
for (const key in doc) {
const props = doc[key];
if (props.hide) continue;
if (props.type) {
settings[key] = buildElement(key, props);
} else {
const group = {
key,
additionalProperties: false,
description: props.description,
settings: [],
};
appendSettings(group, props, [key]);
settings[key] = group;
}
}
return Object.values(settings).sort((a, b) => a.key.localeCompare(b.key));
},
};