diff --git a/utils/serieUtil.ts b/utils/serieUtil.ts index 23d5c14..981b8ca 100644 --- a/utils/serieUtil.ts +++ b/utils/serieUtil.ts @@ -1,12 +1,16 @@ import { smartGlob } from "./util" import { setToSetSimple, getSets } from "./setUtil" -import { Serie, SupportedLanguages } from 'db/interfaces' +import { Serie, SupportedLanguages, Set } from 'db/interfaces' import { Serie as SerieSingle, SerieResume } from '@tcgdex/sdk/interfaces' export async function getSeries(): Promise> { - return Promise.all((await smartGlob('./db/data/*.js')) + const series = await Promise.all((await smartGlob('./db/data/*.js')) .map((it) => it.substring(it.lastIndexOf('/') + 1, it.length - 3)) .map((it) => getSerie(it))) + const tmp: Array<[Serie, Set]> = await Promise.all(series.map( async (it) => { + return [it, (await getSets(it.name.en)).reduce((p, c) => p ? p.releaseDate < c.releaseDate ? p : c : c, undefined) as Set] as [Serie, Set] + })) + return tmp.sort((a, b) => a[1].releaseDate > b[1].releaseDate ? 1 : -1).map((it) => it[0]) } export async function getSerie(name: string): Promise { diff --git a/utils/setUtil.ts b/utils/setUtil.ts index 0cb5c9f..d88ca2a 100644 --- a/utils/setUtil.ts +++ b/utils/setUtil.ts @@ -12,17 +12,17 @@ const setCache: t = {} // Dont use cache as it wont necessary have them all export async function getSets(serie = '*'): Promise> { const sets = (await smartGlob(`./db/data/${serie}/*.js`)).map((set) => set.substring(set.lastIndexOf('/')+1, set.lastIndexOf('.'))) - return Promise.all(sets.map((set) => getSet(set))) + return Promise.all(sets.map((set) => getSet(set, serie))) } /** * Return the set * @param name the name of the set (don't include.js/.ts) */ -export async function getSet(name: string): Promise { +export async function getSet(name: string, serie = '*'): Promise { if (!setCache[name]) { try { - const [path] = await smartGlob(`./db/data/*/${name}.js`) + const [path] = await smartGlob(`./db/data/${serie}/${name}.js`) setCache[name] = (await import(path.replace('./', '../'))).default } catch (e) { const set = (await getSets()).find((s) => s.id === name)