This repository has been archived on 2026-04-20. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
AsaJS/scripts/components.ts
2026-01-04 10:52:31 +07:00

62 lines
1.3 KiB
TypeScript

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)
}
},
}