This commit is contained in:
Asaki Yuki 2026-01-04 10:52:31 +07:00
commit e2f472d802
83 changed files with 6338 additions and 0 deletions

62
scripts/components.ts Normal file
View file

@ -0,0 +1,62 @@
import JSONC from "jsonc-parser"
import fsp from "fs/promises"
import fs from "fs"
if (!fs.existsSync("prefetch")) fs.mkdirSync("prefetch")
export const Github = {
readFile: async (user: string, repo: string, branches: string, path: string) => {
try {
return fetch(`https://raw.githubusercontent.com/${user}/${repo}/refs/heads/${branches}/${path}`).then(res =>
res.text()
)
} catch (error) {
console.error(error)
process.exit(1)
}
},
}
export const PFFS = {
// Sync
readFile: (file: string) => {
try {
return fs.readFileSync(`prefetch/${file}`, "utf-8")
} catch (error) {
console.error(error)
process.exit(1)
}
},
writeFile: (file: string, data: string) => {
try {
file.split("/").reduce((a, b) => {
if (!fs.existsSync(a)) fs.mkdirSync(a)
return `prefetch/${a}/${b}`
})
return fsp.writeFile(`prefetch/${file}`, data)
} catch (error) {
console.error(error)
process.exit(1)
}
},
readFileJSON: (file: string) => {
try {
return JSONC.parse(PFFS.readFile(file))
} catch (error) {
console.error(error)
process.exit(1)
}
},
writeFileJSON: (file: string, data: object) => {
try {
return PFFS.writeFile(file, JSON.stringify(data))
} catch (error) {
console.error(error)
process.exit(1)
}
},
}

17
scripts/custom.ts Normal file
View file

@ -0,0 +1,17 @@
export {} // 👈 BẮT BUỘC
declare global {
interface String {
toCamelCase(upcaseTop?: boolean): string
toSnakeCase(): string
}
}
String.prototype.toCamelCase = function (this: string, upcaseTop: boolean): string {
const a = this.replace(/_([a-z])/g, g => g[1].toUpperCase())
return upcaseTop ? a.charAt(0).toUpperCase() + a.slice(1) : a
}
String.prototype.toSnakeCase = function (this: string): string {
return this.replace(/([A-Z])/g, "_$1").toLowerCase()
}

5
scripts/prefetch.ts Normal file
View file

@ -0,0 +1,5 @@
import { Github, PFFS } from "./components"
Github.readFile("KalmeMarq", "Bugrock-JSON-UI-Schemas", "main", "ui.schema.json").then(data => {
PFFS.writeFile("ui.schema.json", data)
})

43
scripts/write/enum.ts Normal file
View file

@ -0,0 +1,43 @@
import { PFFS } from "../components"
import fs from "fs/promises"
import "../custom"
const schema = PFFS.readFileJSON("ui.schema.json").definitions
const enumPath = "src/types/enums/"
const index: string[] = []
for (const key in schema) {
const data = schema[key]
if (data.enum) {
const enumName = key.match(/\w+$/)?.[0].toCamelCase(true)!
index.push(`export { ${enumName} } from "./${enumName}.js"`)
const count = new Map<string, number>()
const fileData: string[] = [`export enum ${enumName} {`]
for (const value of data.enum as string[]) {
if (enumName === "ButtonId") {
const values = value.match(/\w+/g)!
const name =
values[0] === "button" ? values.slice(1).join("_").toUpperCase() : values.join("_").toUpperCase()
if (name === undefined) continue
fileData.push(` ${count.get(name) ? `${name}_${count.get(name)}` : name} = "${value}",`)
count.set(name, (count.get(name) || 0) + 1)
} else {
let name = value.match(/\w+$/g)?.join("_")?.toUpperCase()!
if (name === undefined) continue
if (/\d/.test(name?.[0])) name = "_" + name
fileData.push(` ${count.get(name) ? `${name}_${count.get(name)}` : name} = "${value}",`)
count.set(name, (count.get(name) || 0) + 1)
}
}
fileData.push("}")
fs.writeFile(`${enumPath}${enumName}.ts`, fileData.join("\n"))
}
}
fs.writeFile(`${enumPath}index.ts`, index.join("\n"))