forked from p2r3/convert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildCache.js
More file actions
53 lines (44 loc) · 1.35 KB
/
buildCache.js
File metadata and controls
53 lines (44 loc) · 1.35 KB
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
import puppeteer from "puppeteer";
const minify = (process.argv[3] === "--minify");
const outputPath = process.argv[2] || "cache.json";
// delete previous cache.json so regeneration is forced to happen
const outputFile = Bun.file(outputPath);
if (await outputFile.exists()) {
await outputFile.delete();
}
const server = Bun.serve({
async fetch (req) {
const path = new URL(req.url).pathname.replace("/convert/", "") || "index.html";
const file = Bun.file(`${__dirname}/dist/${path}`.replaceAll("..", ""));
if (!(await file.exists())) return new Response("Not Found", { status: 404 });
return new Response(file);
},
port: 8080
});
const browser = await puppeteer.launch({
headless: "new",
args: ["--no-sandbox", "--disable-setuid-sandbox"]
});
const page = await browser.newPage();
await Promise.all([
new Promise(resolve => {
page.on("console", msg => {
const text = msg.text();
if (text === "Built initial format list.") resolve();
});
}),
page.goto("http://localhost:8080/convert/index.html")
]);
const cacheJSON = await page.evaluate((minify) => {
if (minify === true) {
return JSON.stringify(
JSON.parse(
window.printSupportedFormatCache()
)
);
}
return window.printSupportedFormatCache();
}, minify);
await Bun.write(outputPath, cacheJSON);
await browser.close();
server.stop();