import { getAllCards, getBaseFolder, urlize } from "../util" import { fetchCard, isCardAvailable, cardToCardSimple } from "../cardUtil" import Type, { TypeSingle } from "@tcgdex/sdk/interfaces/Type" import Card from "@tcgdex/sdk/interfaces/Card" import { Langs } from "@tcgdex/sdk/interfaces/LangList" import TranslationUtil from "@tcgdex/sdk/TranslationUtil" import { promises } from "fs" type typeCards = { [key in Type]?: Array } const lang = process.env.CARDLANG as Langs || "en" const endpoint = getBaseFolder(lang, "types") const btsp = async () => { const list = getAllCards() const arr: typeCards = {} for (const i of list) { const card = await fetchCard(i) if (!isCardAvailable(card, lang) || !card.type) continue for (const type of card.type) { if (!(type in arr)) arr[type] = [] arr[type].push(card) } } for (const type in arr) { if (arr.hasOwnProperty(type)) { const cards: Array = arr[type]; const rType: Type = parseInt(type) const toSave: TypeSingle = { id: rType, name: TranslationUtil.translate("type", rType, lang), cards: cards.map(el => cardToCardSimple(el, lang)) } const index = `${endpoint}/${toSave.id}` const name = `${endpoint}/${urlize(toSave.name)}` await promises.mkdir(index, {recursive: true}) await promises.mkdir(name, {recursive: true}) await promises.writeFile(`${index}/index.json`, JSON.stringify(toSave)) await promises.writeFile(`${name}/index.json`, JSON.stringify(toSave)) } } } btsp()