Updated Expansions

Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
Florian Bouillon 2020-03-10 14:52:24 +01:00
parent 6cb861d16e
commit a30d338103
No known key found for this signature in database
GPG Key ID: B143FF27EF555D16
4 changed files with 84 additions and 1 deletions

View File

@ -2,6 +2,9 @@ import Expansion from "../db/interfaces/Expansion"
import Set from "../db/interfaces/Set"
import * as glob from 'glob'
import { Langs } from "../db/interfaces/LangList"
import { ExpansionSingle } from "../sdk/dist/types/interfaces/Expansion"
import { getAllSets } from "./util"
import { setToSetSimple, fetchSet } from "./setUtil"
export function getExpansion(set: Set): Expansion {
if ("expansion" in set) return set.expansion
@ -12,9 +15,21 @@ export function getAllExpansions(): Array<string> {
return glob.sync("./db/expansions/*.ts").map(el => el.substr(16, el.length-15-1-3)) // -15 = start -1 = 0 index -3 = .ts
}
export function fetchExpansion(name: string): Expansion {
return require(`../db/expansions/${name}.js`).default
}
export function expansionToExpansionSimple(expansion: Expansion, lang: Langs) {
return {
code: expansion.code,
name: typeof expansion.name === "string" ? expansion.name : expansion.name[lang]
}
}
export function expansionToExpansionSingle(expansion: Expansion, lang: Langs): ExpansionSingle {
return {
code: expansion.code,
name: typeof expansion.name === "string" ? expansion.name : expansion.name[lang],
sets: getAllSets(expansion.code, true).map(el => setToSetSimple(fetchSet(expansion.code, el), lang))
}
}

View File

@ -0,0 +1,46 @@
import { getAllExpansions, expansionToExpansionSimple } from "../expansionUtil"
import Expansion from "../../db/interfaces/Expansion"
import { getAllSets, getBaseFolder } from "../util"
import { fetchSet } from "../setUtil"
import { promises as fs } from 'fs'
import { ExpansionList } from '../../sdk/dist/types/interfaces/Expansion'
import { Langs } from "../../db/interfaces/LangList"
const lang = process.env.CARDLANG as Langs || "en"
const endpoint = getBaseFolder(lang, "expansions")
const btsp = async () => {
const expansions = getAllExpansions()
let list: Array<{
release: string,
expansion: Expansion
}> = []
for (const i of expansions) {
const expansion: Expansion = require(`../../db/expansions/${i}`).default
const sets = getAllSets(expansion.code, true)
expansion.sets = sets
let oldestRelease = "9999-99-99"
for (const j of sets) {
console.log(j)
const set = fetchSet(expansion.code, j)
oldestRelease = set.releaseDate < oldestRelease ? set.releaseDate : oldestRelease
}
list.push({
release: oldestRelease,
expansion
})
}
list = list.sort((a, b) => a.release > b.release ? 1 : -1)
const finalList = list.map(el => el.expansion)
const res: ExpansionList = {
count: finalList.length,
list: finalList.map(el => expansionToExpansionSimple(el, lang))
}
await fs.mkdir(endpoint, {recursive: true})
await fs.writeFile(`${endpoint}/index.json`, JSON.stringify(res))
}
btsp()

View File

@ -0,0 +1,22 @@
import { getAllExpansions, fetchExpansion, expansionToExpansionSingle } from "../expansionUtil"
import { Langs } from "../../db/interfaces/LangList"
import { getBaseFolder } from "../util"
import { promises as fs } from 'fs'
const lang = process.env.CARDLANG as Langs || "en"
const endpoint = getBaseFolder(lang, "expansions")
const btsp = async () => {
const list = getAllExpansions()
for (const i of list) {
const expansion = fetchExpansion(i)
await fs.mkdir(`${endpoint}/${expansion.code}/`, {recursive: true})
await fs.writeFile(`${endpoint}/${expansion.code}/index.json`, JSON.stringify(expansionToExpansionSingle(expansion, lang)))
}
}
btsp()

View File

@ -40,7 +40,7 @@ export function isSetAvailable(set: Set, lang: Langs) {
export function setToSetSimple(set: Set, lang: Langs): SetSimple {
return {
code: set.code,
name: set.name[lang],
name: typeof set.name === "string" ? set.name : set.name[lang],
total: set.cardCount.total
}
}