-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
194 lines (147 loc) · 4.23 KB
/
index.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import fs from "fs-extra";
import path from "path";
import config from "./config";
import { Err, Ok, Result, exists } from "./lib/utils";
import { Data } from "./lib/types";
import extract from "extract-zip";
import { loadData } from "./lib/loaders/loaders";
import { writeData } from "./lib/writers/writers";
import { generateData } from "./lib/processors/processors";
const PUBLIC_DIR = path.join(__dirname, "./_public");
const CACHE_DIR = path.join(__dirname, "./.cache");
const CONTENT_DIR = path.join(__dirname, "./.content");
const DEFAULT_DATA: Data = {
blogPosts: {
entityOrder: [],
entities: {},
},
microBlogsPosts: {
entityOrder: [],
entities: {},
},
microPosts: {
entityOrder: [],
entities: {},
},
mastodonPosts: {
entityOrder: [],
entities: {},
},
statusLolPosts: {
entityOrder: [],
entities: {},
},
albums: {
entityOrder: [],
entities: {},
},
albumPhotos: {
entityOrder: [],
entities: {},
},
lego: {},
games: {},
about: "",
faq: "",
now: "",
movies: {},
tvShows: {},
lastUpdated: "",
};
const CONTENT_URL =
"https://github.com/GeekyAubergine/zoeaubert.me-content/archive/refs/heads/main.zip";
async function prepFolders() {
if (!(await exists(PUBLIC_DIR))) {
await fs.promises.mkdir(PUBLIC_DIR, { recursive: true });
}
if (!(await exists(config.cacheDir))) {
await fs.promises.mkdir(config.cacheDir, { recursive: true });
}
}
async function loadArchive(): Promise<Result<Data>> {
try {
const archiveFile = await fs.promises.readFile(
path.join(PUBLIC_DIR, "archive.json"),
"utf-8"
);
return Ok(JSON.parse(archiveFile));
} catch (e) {
// TODO Try loading from backup
return Err({
type: "UNABLE_TO_LOAD_ARCHIVE",
});
}
}
async function downloadContent(): Promise<Result<undefined>> {
try {
const contentDirExists = await exists(CONTENT_DIR);
if (contentDirExists.ok && contentDirExists.value) {
await fs.rm(CONTENT_DIR, { recursive: true });
}
const contentZip = await fetch(CONTENT_URL);
const contentBlob = await contentZip.blob();
const arrayBuffer = await contentBlob.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
await fs.writeFile(path.join(config.cacheDir, "temp.zip"), buffer);
await extract(path.join(config.cacheDir, "temp.zip"), {
dir: path.join(__dirname, "content-temp"),
});
await fs.copy(
path.join(__dirname, "content-temp/zoeaubert.me-content-main"),
CONTENT_DIR
);
await fs.rm(path.join(config.cacheDir, "temp.zip"), { recursive: true });
await fs.rm(path.join(__dirname, "content-temp"), { recursive: true });
return Ok(undefined);
} catch (e) {
console.error(e);
return Err({
type: "UNABLE_TO_DOWNLOAD_CONTENT",
});
}
}
async function main() {
console.log("Building api.zoeaubert.me data");
await prepFolders();
const archiveResult = await loadArchive();
const archive = archiveResult.ok ? archiveResult.value : DEFAULT_DATA;
const contentDownloadResult = await downloadContent();
if (!contentDownloadResult.ok) {
console.error(contentDownloadResult.error);
return;
}
console.log("Loading data");
const loadStart = Date.now();
const loadDataResult = await loadData(archive, CACHE_DIR, CONTENT_DIR);
if (!loadDataResult.ok) {
console.error(loadDataResult.error);
return;
}
const loaderData = loadDataResult.value;
const archiveWithNewData = {
...archive,
...loaderData,
};
const loadEnd = Date.now();
console.log(`Loaded in ${loadEnd - loadStart}ms`);
const transformStart = Date.now();
console.log("Transforming data");
const processedData = await generateData(archiveWithNewData);
const transformEnd = Date.now();
console.log(`Transformed in ${transformEnd - transformStart}ms`);
if (!processedData.ok) {
console.error(processedData.error);
return;
}
console.log("Writing data");
const writeStart = Date.now();
const writingResult = await writeData(processedData.value, PUBLIC_DIR);
if (!writingResult.ok) {
console.error(writingResult.error);
return;
}
const writeEnd = Date.now();
console.log(`Wrote in ${writeEnd - writeStart}ms`);
console.log("Done");
}
main();