-
-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathforgeConfig.ts
More file actions
179 lines (172 loc) · 5 KB
/
Copy pathforgeConfig.ts
File metadata and controls
179 lines (172 loc) · 5 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Represent the forge config file
*/
export interface ForgeConfig {
[category: string]: {
comment?: string
properties: Array<ForgeConfig.Property<any>>
}
}
export class CorruptedForgeConfigError extends Error {
name = 'CorruptedForgeConfigError'
constructor(
readonly reason: string,
readonly line: string,
) {
super(`CorruptedForgeConfigError by ${reason}: ${line}`)
}
}
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace ForgeConfig {
export type Type = 'I' | 'D' | 'S' | 'B'
export interface Property<T = number | boolean | string | number[] | boolean[] | string[]> {
readonly type: Type
readonly name: string
readonly comment?: string
value: T
}
/**
* Convert a forge config to string
*/
export function stringify(config: ForgeConfig) {
let content = '# Configuration file\n\n\n'
const propIndent = ' '
const arrIndent = ' '
Object.keys(config).forEach((cat) => {
content += `${cat} {\n\n`
config[cat].properties.forEach((prop) => {
if (prop.comment) {
const lines = prop.comment.split('\n')
for (const l of lines) {
content += `${propIndent}# ${l}\n`
}
}
if (prop.value instanceof Array) {
content += `${propIndent}${prop.type}:${prop.name} <\n`
prop.value.forEach((v) => {
content += `${arrIndent}${v}\n`
})
content += `${propIndent}>\n`
} else {
content += `${propIndent}${prop.type}:${prop.name}=${prop.value}\n`
}
content += '\n'
})
content += '}\n\n'
})
return content
}
/**
* Parse a forge config string into `Config` object
* @param body The forge config string
*/
export function parse(body: string): ForgeConfig {
const lines = body
.split('\n')
.map((s) => s.trim())
.filter((s) => s.length !== 0)
let category: string | undefined
let pendingCategory: string | undefined
const parseVal = (type: Type, value: any) => {
const map: { [key: string]: (s: string) => any } = {
I: Number.parseInt,
D: Number.parseFloat,
S: (s: string) => s,
B: (s: string) => s === 'true',
}
const handler = map[type]
return handler(value)
}
const config: ForgeConfig = {}
let inlist = false
let comment: string | undefined
let last: any
const readProp = (type: Type, line: string) => {
line = line.substring(line.indexOf(':') + 1, line.length)
const pair = line.split('=')
if (pair.length === 0 || pair.length === 1) {
let value
let name
if (line.endsWith(' <')) {
value = []
name = line.substring(0, line.length - 2)
inlist = true
}
if (!category) {
throw new CorruptedForgeConfigError('MissingCategory', line)
}
config[category].properties.push((last = { name, type, value, comment } as Property))
} else {
inlist = false
if (!category) {
throw new CorruptedForgeConfigError('MissingCategory', line)
}
config[category].properties.push({
name: pair[0],
value: parseVal(type, pair[1]),
type,
comment,
} as Property)
}
comment = undefined
}
for (const line of lines) {
if (inlist) {
if (!last) {
throw new CorruptedForgeConfigError('CorruptedList', line)
}
if (line === '>') {
inlist = false
} else if (line.endsWith(' >')) {
last.value.push(parseVal(last.type, line.substring(0, line.length - 2)))
inlist = false
} else {
last.value.push(parseVal(last.type, line))
}
continue
}
switch (line.charAt(0)) {
case '#':
if (!comment) {
comment = line.substring(1, line.length).trim()
} else {
comment = comment.concat('\n', line.substring(1, line.length).trim())
}
break
case 'I':
case 'D':
case 'S':
case 'B':
readProp(line.charAt(0) as Type, line)
break
case '<':
break
case '{':
if (pendingCategory) {
category = pendingCategory
config[category] = { comment, properties: [] }
comment = undefined
} else {
throw new CorruptedForgeConfigError('MissingCategory', line)
}
break
case '}':
category = undefined
break
default:
if (!category) {
if (line.endsWith('{')) {
category = line.substring(0, line.length - 1).trim()
config[category] = { comment, properties: [] }
comment = undefined
} else {
pendingCategory = line
}
} else {
throw new CorruptedForgeConfigError('Duplicated', line)
}
}
}
return config
}
}