1
0
mirror of https://github.com/tcgdex/cards-database.git synced 2025-06-13 00:09:18 +00:00

feat: Move internals to bunJS (#459)

This commit is contained in:
2024-01-03 00:40:03 +01:00
committed by GitHub
parent f8c1d7dce9
commit 034b7e2cec
14 changed files with 95 additions and 4359 deletions

View File

@ -118,7 +118,7 @@ export async function cardToCardSingle(localId: string, card: Card, lang: Suppor
* @returns [the local id, the Card object]
*/
export async function getCard(serie: string, setName: string, id: string): Promise<Card> {
return (await import(`../../${DB_PATH}/data/${serie}/${setName}/${id}.js`)).default
return (await import(`../../${DB_PATH}/data/${serie}/${setName}/${id}.ts`)).default
}
/**
@ -128,7 +128,7 @@ export async function getCard(serie: string, setName: string, id: string): Promi
* @returns An array with the 0 = localId, 1 = Card Object
*/
export async function getCards(lang: SupportedLanguages, set?: Set): Promise<Array<[string, Card]>> {
const cards = await smartGlob(`${DB_PATH}/data/${(set && set.serie.name.en) ?? '*'}/${(set && set.name.en) ?? '*'}/*.js`)
const cards = await smartGlob(`${DB_PATH}/data/${(set && set.serie.name.en) ?? '*'}/${(set && set.name.en) ?? '*'}/*.ts`)
const list: Array<[string, Card]> = []
for (const path of cards) {
let items = path.split('/')

View File

@ -1,10 +1,10 @@
import { Serie, Set, SupportedLanguages } from '../../../interfaces'
import { SerieResume, Serie as SerieSingle } from '../../../meta/definitions/api'
import { getSets, setToSetSimple } from './setUtil'
import { DB_PATH, smartGlob } from './util'
import { setToSetSimple, getSets } from './setUtil'
import { Serie, SupportedLanguages, Set } from '../../../interfaces'
import { Serie as SerieSingle, SerieResume } from '../../../meta/definitions/api'
export async function getSerie(name: string): Promise<Serie> {
return (await import(`../../${DB_PATH}/data/${name}.js`)).default
return (await import(`../../${DB_PATH}/data/${name}.ts`)).default
}
export async function isSerieAvailable(serie: Serie, lang: SupportedLanguages): Promise<boolean> {
@ -16,7 +16,7 @@ export async function isSerieAvailable(serie: Serie, lang: SupportedLanguages):
}
export async function getSeries(lang: SupportedLanguages): Promise<Array<Serie>> {
let series: Array<Serie> = (await Promise.all((await smartGlob(`${DB_PATH}/data/*.js`))
let series: Array<Serie> = (await Promise.all((await smartGlob(`${DB_PATH}/data/*.ts`))
// Find Serie's name
.map((it) => it.substring(it.lastIndexOf('/') + 1, it.length - 3))
// Fetch the Serie

View File

@ -1,7 +1,7 @@
import { Set, SupportedLanguages } from '../../../interfaces'
import { DB_PATH, fetchRemoteFile, setIsLegal, smartGlob } from './util'
import { cardToCardSimple, getCards } from './cardUtil'
import { SetResume, Set as SetSingle } from '../../../meta/definitions/api'
import { cardToCardSimple, getCards } from './cardUtil'
import { DB_PATH, fetchRemoteFile, setIsLegal, smartGlob } from './util'
interface t {
[key: string]: Set
@ -20,11 +20,11 @@ export function isSetAvailable(set: Set, lang: SupportedLanguages): boolean {
export async function getSet(name: string, serie = '*'): Promise<Set> {
if (!setCache[name]) {
try {
const [path] = await smartGlob(`${DB_PATH}/data/${serie}/${name}.js`)
const [path] = await smartGlob(`${DB_PATH}/data/${serie}/${name}.ts`)
setCache[name] = (await import(`../../${path}`)).default
} catch (error) {
console.error(error)
console.error(`Error trying to import importing (${`db/data/${serie}/${name}.js`})`)
console.error(`Error trying to import importing (${`db/data/${serie}/${name}.ts`})`)
process.exit(1)
}
}
@ -34,7 +34,7 @@ export async function getSet(name: string, serie = '*'): Promise<Set> {
// Dont use cache as it wont necessary have them all
export async function getSets(serie = '*', lang: SupportedLanguages): Promise<Array<Set>> {
// list sets names
const rawSets = (await smartGlob(`${DB_PATH}/data/${serie}/*.js`)).map((set) => set.substring(set.lastIndexOf('/') + 1, set.lastIndexOf('.')))
const rawSets = (await smartGlob(`${DB_PATH}/data/${serie}/*.ts`)).map((set) => set.substring(set.lastIndexOf('/') + 1, set.lastIndexOf('.')))
// Fetch sets
const sets = (await Promise.all(rawSets.map((set) => getSet(set, serie))))
// Filter sets