compiler/endpoints/hp/index.ts
Florian Bouillon 7809913d18 Updated
Signed-off-by: Florian BOUILLON <florian.bouillon@delta-wings.net>
2021-02-03 14:02:44 +01:00

40 lines
1.0 KiB
TypeScript

import { getAllCards, getBaseFolder } from "../util"
import { fetchCard, isCardAvailable } from "../cardUtil"
import { Langs } from "@tcgdex/sdk/interfaces/LangList"
import { HpList } from "@tcgdex/sdk/interfaces/Hp"
import { promises as fs } from 'fs'
import Card from "@tcgdex/sdk/interfaces/Card"
import Logger from '@dzeio/logger'
const logger = new Logger('hp/index')
const lang = process.env.CARDLANG as Langs || "en"
const endpoint = getBaseFolder(lang, "hp")
export default async () => {
logger.log('Fetching Cards')
const cards = await getAllCards()
const hps: Array<number> = []
for (const i of cards) {
const card: Card = (await import(i)).default
if (!(await isCardAvailable(card, lang)) || !card.hp) continue
if (hps.includes(card.hp)) continue
hps.push(card.hp)
}
const hpList: HpList = {
count: hps.length,
list: hps.sort((a, b) => a > b ? 1 : -1)
}
logger.log('Writing to files')
await fs.mkdir(endpoint, {recursive: true})
await fs.writeFile(`${endpoint}/index.json`, JSON.stringify(hpList))
logger.log('Finished')
}