forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExplorerBaker.tsx
73 lines (68 loc) · 2.61 KB
/
ExplorerBaker.tsx
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
import fs from "fs-extra"
import path from "path"
import { ExplorerProgram } from "../explorer/ExplorerProgram.js"
import { explorerUrlMigrationsById } from "../explorer/urlMigrations/ExplorerUrlMigrations.js"
import { ExplorerAdminServer } from "../explorerAdminServer/ExplorerAdminServer.js"
import { explorerRedirectTable } from "../explorerAdminServer/ExplorerRedirects.js"
import { renderExplorerPage } from "./siteRenderers.js"
import * as db from "../db/db.js"
export const bakeAllPublishedExplorers = async (
outputFolder: string,
explorerAdminServer: ExplorerAdminServer,
knex: db.KnexReadonlyTransaction
) => {
// remove all existing explorers, since we're re-baking every single one anyway
await fs.remove(outputFolder)
await fs.mkdirp(outputFolder)
const published = await explorerAdminServer.getAllPublishedExplorers()
await bakeExplorersToDir(outputFolder, published, knex)
}
const bakeExplorersToDir = async (
directory: string,
explorers: ExplorerProgram[] = [],
knex: db.KnexReadonlyTransaction
) => {
for (const explorer of explorers) {
await write(
`${directory}/${explorer.slug}.html`,
await renderExplorerPage(explorer, knex)
)
}
}
export const bakeAllExplorerRedirects = async (
outputFolder: string,
explorerAdminServer: ExplorerAdminServer,
knex: db.KnexReadonlyTransaction
) => {
const explorers = await explorerAdminServer.getAllExplorers()
const redirects = explorerRedirectTable.rows
for (const redirect of redirects) {
const { migrationId, path: redirectPath, baseQueryStr } = redirect
const transform = explorerUrlMigrationsById[migrationId]
if (!transform) {
throw new Error(
`No explorer URL migration with id '${migrationId}'. Fix the list of explorer redirects and retry.`
)
}
const { explorerSlug } = transform
const program = explorers.find(
(program) => program.slug === explorerSlug
)
if (!program) {
throw new Error(
`No explorer with slug '${explorerSlug}'. Fix the list of explorer redirects and retry.`
)
}
const html = await renderExplorerPage(program, knex, {
explorerUrlMigrationId: migrationId,
baseQueryStr,
})
await write(path.join(outputFolder, `${redirectPath}.html`), html)
}
}
// todo: merge with SiteBaker's?
const write = async (outPath: string, content: string) => {
await fs.mkdirp(path.dirname(outPath))
await fs.writeFile(outPath, content)
console.log(outPath)
}