This repository has been archived by the owner on Nov 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
75 lines (67 loc) · 2.47 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* eslint-disable max-statements */
import { SupportedLanguages } from 'db/interfaces'
import { Endpoint } from 'interfaces'
import { promises as fs } from 'fs'
import { objectMap } from '@dzeio/object-util'
import { urlize, fetchRemoteFile } from './utils/util'
import { config } from 'dotenv'
config()
const lang: SupportedLanguages = process.env.TCGDEX_COMPILER_LANG as SupportedLanguages ?? 'en'
const VERSION = 'v2'
;(async () => {
const paths = (await fs.readdir('./endpoints')).filter((f) => f.endsWith('.ts'))
console.log('Prefetching pictures')
await fetchRemoteFile('https://assets.tcgdex.net/datas.json')
// Cleanup
await fs.rm(`./dist/${VERSION}/${lang}`, { force: true, recursive: true })
console.log('Let\'s GO !')
// Process definitions
try {
await fs.mkdir(`./dist/${VERSION}/definitions`)
await fs.copyFile('./db/meta/definitions/openapi.yaml', `./dist/${VERSION}/definitions/openapi.yaml`)
} catch {}
// process endpoints
for await (const file of paths) {
const path = `./endpoints/${file}`
console.log(file, 'Running Init')
const Ep = (await import(path)).default
const endpoint = new Ep(lang) as Endpoint
console.log(file, 'Running Common')
let common: any | null = null
if (endpoint.common) {
common = await endpoint.common()
}
console.log(file, 'Running Index')
const folder = `./dist/${VERSION}/${lang}/${urlize(path.replace('./endpoints/', '').replace('.ts', ''))}`
await fs.mkdir(folder, { recursive: true })
await fs.writeFile(`${folder}/index.json`, JSON.stringify(
await endpoint.index(common)
))
console.log(file, 'Finished Index')
console.log(file, 'Running Item')
const item = await endpoint.item(common)
if (!item) {
console.log(file, 'skipped Item')
continue
}
for await (const key of Object.keys(item)) {
const val = item[key]
const subFolder = `${folder}/${urlize(key)}`
await fs.mkdir(subFolder, { recursive: true })
await fs.writeFile(`${subFolder}/index.json`, JSON.stringify(val))
if (endpoint.sub) {
console.log(file, 'Running subItem', key)
const subItems = await endpoint.sub(common, key)
if (subItems) {
await Promise.all(objectMap(subItems, async (subVal, sybKey) => {
const subSubFolder = `${subFolder}/${urlize(sybKey)}`
await fs.mkdir(subSubFolder, { recursive: true })
await fs.writeFile(`${subSubFolder}/index.json`, JSON.stringify(subVal))
}))
}
console.log(file, 'Finished subItem', key)
}
}
console.log(file, 'Finished Item')
}
})()