-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.ts
More file actions
109 lines (89 loc) · 3.33 KB
/
Copy pathgenerate.ts
File metadata and controls
109 lines (89 loc) · 3.33 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
import fs from "fs"
// generate API functions from:
// https://github.com/ChurchApps/FreeShow/blob/main/src/frontend/components/actions/api.ts
fetch("https://raw.githubusercontent.com/ChurchApps/FreeShow/refs/heads/main/src/frontend/components/actions/api.ts", {
method: "GET"
})
.then((res) => res.text())
.then(parseApi)
.catch((err) => console.log(err))
function parseApi(text: string) {
// get types
let types: { [key: string]: any } = {}
let currentIndex = 0
let nextTypeIndex = 0
do {
nextTypeIndex = text.indexOf("type API_", currentIndex)
currentIndex = nextTypeIndex + 10
if (nextTypeIndex > -1) {
let currentType = text.slice(nextTypeIndex, text.indexOf("}", nextTypeIndex))
let name = currentType.slice(9, currentType.indexOf(" = {"))
let value = parseTypeValue(currentType.slice(currentType.indexOf("{") + 1))
types[name] = value
}
} while (nextTypeIndex > -1)
// get actions
let actions: { [key: string]: null | { [key: string]: any } } = {}
let actionsIndex = text.indexOf("API_ACTIONS = {")
let actionsObj = text.slice(text.indexOf("{", actionsIndex) + 1, text.indexOf(",\n}", actionsIndex))
actionsObj
.split("\n")
.filter((a) => a)
.forEach((actionLine) => {
let functionIndex = actionLine.indexOf(": (")
if (functionIndex < 0) return
if (actionLine.trim().indexOf("//") === 0) return
let actionId = actionLine.slice(0, functionIndex).trim()
actions[actionId] = null
let apiTypeIndex = actionLine.indexOf(": API_")
if (apiTypeIndex < 0) return
actions[actionId] = types[actionLine.slice(apiTypeIndex + 6, actionLine.indexOf(")"))]
})
// console.log(types)
// console.log(actions)
// write to file
// fs.writeFileSync("actions.json", JSON.stringify(actions))
fs.writeFileSync("dist/actions.js", `export const actions = ${JSON.stringify(actions)}`)
}
function parseTypeValue(value: string) {
let valueObj: { [key: string]: any } = {}
let currentObjectKey = ""
let objectValue: any = {}
value
.replaceAll("; ", "\n")
.split("\n")
.filter((a) => a)
.forEach((val) => {
if (val === "{") {
if (currentObjectKey) {
valueObj[currentObjectKey] = objectValue
objectValue = {}
currentObjectKey = ""
}
return
}
// remove comments
let commentIndex = val.indexOf("//")
if (commentIndex > -1) val = val.slice(0, commentIndex)
val = val.trim()
if (!val) return
let key = val.slice(0, val.indexOf(":"))
let type = val.slice(val.indexOf(":") + 1).trim()
if (currentObjectKey) {
objectValue[key] = type
return
}
if (type === "{") {
type = "OBJECT"
objectValue = {}
currentObjectKey = key
} else if (type.includes("{")) {
// do something
}
valueObj[key] = type
})
if (currentObjectKey) {
valueObj[currentObjectKey] = objectValue
}
return valueObj
}