-
Notifications
You must be signed in to change notification settings - Fork 1
/
build-static.js
70 lines (62 loc) · 1.96 KB
/
build-static.js
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
const fs = require("fs");
const glob = require("glob");
const sass = require("node-sass");
const { dirname, join } = require("path");
const rimraf = require("rimraf");
const request = require("supertest");
const buildApp = require("./src/app");
const data = require("./src/data");
const root = "docs";
rimraf.sync(root);
fs.mkdirSync(root);
fs.writeFileSync(join(root, ".nojekyll"), "");
fs.writeFileSync(join(root, "CNAME"), "well.since-you-ask.me");
const staticFiles = glob
.sync("src/public/static/**/*", { nodir: true })
.map((path) => path.replace("src/public/static", ""));
for (const file of staticFiles) {
fs.mkdirSync(join(root, dirname(file)), { recursive: true });
fs.copyFileSync(join("src/public/static", file), join("docs", file));
}
const { css } = sass.renderSync({
file: "src/styles/main.scss",
outputStyle: "compressed",
});
fs.mkdirSync(join(root, "css"), { recursive: true });
fs.writeFileSync(join(root, "css", "main.css"), css);
const episodes = [...new Set(data.events.map(({ episode }) => episode))];
const paths = [
"/",
...data.characters.map(({ id }) => `/for-the-life-history-of/${id}`),
...data.themes.map(({ id }) => `/for-a-tale-of/${id}`),
...data.events.map(
({ episode, scene }) =>
`/what-happened-in/episode-${episode}/scene-${scene}`
),
...episodes.map((episode) => `/what-happened-in/episode-${episode}`),
].flatMap((path) => [
path,
path === "/"
? "/starting-from-the-beginning"
: `${path}/starting-from-the-beginning`,
]);
const app = buildApp(false, false);
Promise.all(
paths.map((path) => {
fs.mkdirSync(join(root, path), { recursive: true });
return request(app)
.get(path)
.then((res) =>
fs.writeFileSync(join(root, path, "index.html"), res.text)
);
})
)
.then(() =>
request(app)
.get("/an/unmapped/route")
.then((res) => fs.writeFileSync(join(root, "404.html"), res.text))
)
.then(() => {
console.log("Build complete.");
process.exit();
});