Moved files to link with the SDK

Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
2020-03-11 11:21:40 +01:00
parent b36dd9046f
commit ef866401a4
31 changed files with 207 additions and 103 deletions

31
endpoints/types/index.ts Normal file
View File

@ -0,0 +1,31 @@
import Type, { TypeSimple } from "@tcgdex/sdk/interfaces/Type"
import TranslationUtil from "@tcgdex/sdk/TranslationUtil"
import { getBaseFolder } from "../util"
import { Langs } from "@tcgdex/sdk/interfaces/LangList"
import { promises as fs } from "fs"
import { List } from "@tcgdex/sdk/interfaces/General"
const lang = process.env.CARDLANG as Langs || "en"
const endpoint = getBaseFolder(lang, "types")
const btsp = async () => {
const typeArr: Array<TypeSimple> = []
for (const i of Object.values(Type)) {
if (typeof i !== "number") continue
typeArr.push({
id: i,
name: TranslationUtil.translate("type", i, lang)
})
}
const typeList: List<TypeSimple> = {
count: typeArr.length,
list: typeArr
}
await fs.mkdir(endpoint, {recursive: true})
await fs.writeFile(`${endpoint}/index.json`, JSON.stringify(typeList))
}
btsp()

53
endpoints/types/item.ts Normal file
View File

@ -0,0 +1,53 @@
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<Card>
}
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<Card> = 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()