forked from iLLeniumStudios/fivem-lua-lint-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-rc.ts
174 lines (159 loc) · 4.38 KB
/
generate-rc.ts
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
import fetch from "node-fetch"
import * as fs from "fs"
import * as path from "path"
import * as ansi from "ansi-colors"
interface CfxNative {
name: string
params: {
name: string
type: string
description: string
}[]
results: "int" | "void" | "long" | "BOOL" | string
description: string
examples: {
lang: "lua" | string
code: string
}[]
hash: string
ns: string
aliases?: string[]
apiset: "client" | "server" | "shared"
game: "gta5" | "rdr3" | "ny"
}
type CfxNativesResponse = {
[group: string]: { [native: string]: CfxNative }
}
const macroCaseToSnake = (s: string): string => {
return s
.split("_")
.map(str =>
str
.split("")
.map((c, i) => {
if (+c > 0) return `_${c}`
return i === 0 ? c.toUpperCase() : c.toLowerCase()
})
.join("")
)
.join("")
}
const uniqueArray = <T>(a: T[]): T[] => {
const b: T[] = []
a.forEach(item => {
if (b.includes(item)) return
b.push(item)
})
return b
}
const reduceNativesToNames = (results: string[], item: CfxNative): string[] => {
let name = item.name || `N_${item.hash}`
name = macroCaseToSnake(name)
results.push(name)
;(item.aliases || []).forEach(a => {
if (a.slice(0, 1) === "_") {
let aliasName = macroCaseToSnake(a.slice(1))
if (aliasName === "GetGroundZFor3dCoord") {
aliasName = "GetGroundZFor_3dCoord"
}
results.push(aliasName)
}
})
return results
}
interface MappedNativeResponse {
shared: string[]
client: string[]
server: string[]
}
async function fetchAllNatives(): Promise<MappedNativeResponse> {
const clientNatives: string[] = []
const serverNatives: string[] = []
const sharedNatives: string[] = []
const urls = [
"https://runtime.fivem.net/doc/natives_cfx.json",
"https://runtime.fivem.net/doc/natives.json"
]
for (const url of urls) {
console.log(ansi.cyan(`fetch => ${ansi.blueBright(url)}...`))
await fetch(url)
.then<CfxNativesResponse>(r => r.json())
.then(data => {
const nativesList: CfxNative[] = Object.entries(data)
.reduce((natives: CfxNative[], [_, list]) => {
natives.push(...Object.values(list))
return natives
}, [])
.filter(n => !!n.name)
clientNatives.push(
...nativesList
.filter(n => !n.apiset || n.apiset === "client")
.reduce(reduceNativesToNames, [])
)
serverNatives.push(
...nativesList
.filter(n => n.apiset === "server")
.reduce(reduceNativesToNames, [])
)
sharedNatives.push(
...nativesList
.filter(n => n.apiset === "shared")
.reduce(reduceNativesToNames, [])
)
})
}
return {
shared: uniqueArray(sharedNatives),
client: uniqueArray(clientNatives),
server: uniqueArray(serverNatives)
}
}
fetchAllNatives().then(natives => {
let template = fs.readFileSync(
path.join(__dirname, ".luacheckrc.template"),
"utf-8"
)
template = template
.replace("%%SHARED_GLOBALS%%", natives.shared.map(s => `'${s}'`).join(", "))
.replace("%%SERVER_GLOBALS%%", natives.server.map(s => `'${s}'`).join(", "))
.replace("%%CLIENT_GLOBALS%%", natives.client.map(s => `'${s}'`).join(", "))
let extraLibs = ""
const extraLibUserArg = process.argv[2]
if (extraLibUserArg?.length) {
extraLibs = `+${extraLibUserArg}`
}
if (extraLibs.length) {
console.log(
ansi.gray(
`${ansi.yellow(`extra`)} ${ansi.cyan(`=>`)} ${ansi.magentaBright(
extraLibs
)}`
)
)
}
template = template.replace(/%%EXTRA%%/g, extraLibs)
fs.writeFileSync(path.join(__dirname, ".luacheckrc.default"), template)
console.log(ansi.gray(`=`.repeat(29)))
console.log(
ansi.gray(
`=== ${ansi.yellow(
natives.shared.length.toString()
)} ${ansi.magentaBright("shared")} generated`.padEnd(45, " ") + " ==="
)
)
console.log(
ansi.gray(
`=== ${ansi.blue(natives.server.length.toString())} ${ansi.magentaBright(
"server"
)} generated`.padEnd(45, " ") + " ==="
)
)
console.log(
ansi.gray(
`=== ${ansi.green(natives.client.length.toString())} ${ansi.magentaBright(
"client"
)} generated`.padEnd(45, " ") + " ==="
)
)
console.log(ansi.gray(`========[ ${ansi.greenBright("COMPLETED")} ]========`))
})