-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (56 loc) · 1.86 KB
/
index.js
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
/**
* @param {string} input The input
*/
export function parse(input) {
if (!input.startsWith('config'))
throw new Error('input must start with config <name>')
input.replace('config ', '')
if (
!input.split('\n')[0].replace('config ', '')[0].startsWith('"') ||
!input.split('\n')[0].replace('{', '').trim().endsWith('"')
)
throw new Error('config name must have " in the end and the start.')
if (!input.split('\n')[0].endsWith('{'))
throw new Error('a { must go after the config name.')
if (!input.endsWith('}'))
throw new Error('a } must go in the end of the config.')
const name = input
.split('\n')[0]
.split(/ +/g)[1]
.replace('{', '')
.trim()
.replace(/"/g, '')
const new1 = input.split('\n')
new1.shift()
new1.pop()
const output = { name, properties: {} }
for (const property of new1) {
const args = property.trim().split(/ +/g)
const type = args[0].replace(/\t/g, '')
const name = args[1]
let value = args.slice(2).join(' ')
for (const prop in Deno.env.toObject()) {
const regex = new RegExp(`{${prop}}`, 'g')
value = value.replace(regex, Deno.env.get[prop])
}
switch (type) {
case 'string':
output.properties[name] = String(value)
break
case 'number':
output.properties[name] = Number(value)
break
case 'json':
output.properties[name] = JSON.parse(value)
break
case 'boolean':
output.properties[name] = Boolean(value)
break
default:
throw new Error(
'invalid type, type must be string|number|json|boolean'
)
}
}
return output
}