-
Notifications
You must be signed in to change notification settings - Fork 3
/
deno.ts
119 lines (96 loc) · 3.87 KB
/
deno.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
import puppeteer from "https://deno.land/x/puppeteer@16.2.0/mod.ts";
import {getAnyEdgeLatest} from "./edgePath.ts"
import exists from "./exists.ts"
const dedent = (string:string) => string.split('\n').map(line => line.trim()).join('\n')
const needsUpdate = async (fileName: string, path:string) =>
!(await exists(fileName)) || ((await Deno.stat(fileName)).mtime || 0) < ((await Deno.stat(path)).mtime || 0)
const web = Deno.args.includes("web");
let jekyll;
let accessURL:string;
if (web) {
accessURL = "https://" + await Deno.readTextFile("./CNAME");
console.log("Generating images from https://manual.ds-homebrew.com...");
} else {
console.log("Generating images from local files...");
jekyll = new Deno.Command('bundle', { args: ['exec', 'jekyll', 'serve']}).spawn();
// Wait 5s for jekyll to be ready
await new Promise(resolve => setTimeout(resolve, 5000));
accessURL = "http://127.0.0.1:4000/";
}
let browser;
try {
browser = await puppeteer.launch({ product: 'chrome' });
} catch (error) {
if (!error.message.includes('Could not find browser revision'))
throw error;
browser = await puppeteer.launch({ product: 'chrome', executablePath: await getAnyEdgeLatest() })
}
const tab = await browser.newPage();
await tab.setViewport({width: 256, height: 3000});
const tempFileNames = {
screenshot: "screenshot.png",
palette: "palette.png"
}
for await (const folder of Deno.readDir("pages")) {
if (!folder.isDirectory)
continue;
const dir = folder.name;
if (dir == "_ic")
continue;
await Deno.mkdir(`nitrofiles/pages/${dir.substring(1)}`, {recursive: true});
for await (const subPageFolder of Deno.readDir(`pages/${dir}`)) {
if (subPageFolder.isDirectory)
continue;
const page = subPageFolder.name;
console.log(dir, page);
const rootPath = `${dir.substring(1)}/${page.substring(0, page.indexOf("."))}`;
await tab.goto([accessURL, rootPath].join('/'), { waitUntil: "networkidle0" });
const pageEval = await tab.evaluate((accessURL) => ({
title: document.title,
height: document.body.clientHeight,
links: Array.from(document.getElementsByTagName('a'))
.filter(element => element.href.startsWith(accessURL))
.map(element => ({
X: Math.round(element.getBoundingClientRect().x),
Y: Math.round(element.getBoundingClientRect().y),
W: Math.round(element.getBoundingClientRect().width),
H: Math.round(element.getBoundingClientRect().height),
DEST: element.href.substring(element.href.lastIndexOf("/") + 1)
}))
}), accessURL);
const imagePath = `nitrofiles/pages/${rootPath}.gif`
if (await needsUpdate(imagePath, `pages/${dir}/${page}`)) {
await tab.screenshot({ path: tempFileNames.screenshot, clip: { x: 0, y: 0, width: 256, height: pageEval.height } });
const paletteProcess = new Deno.Command('ffmpeg', {
args: ["-i", tempFileNames.screenshot, "-vf", "palettegen=max_colors=256", tempFileNames.palette, "-y", "-loglevel", "error"]
});
await paletteProcess.output();
const conversionProcess = new Deno.Command('ffmpeg', {
args: ["-i", tempFileNames.screenshot, "-i", tempFileNames.palette, "-filter_complex", "paletteuse", imagePath, "-y", "-loglevel", "error"]
})
await conversionProcess.output();
}
if (await needsUpdate(`nitrofiles/pages/${rootPath}.ini`, `pages/${dir}/${page}`)) {
let iniContent = dedent(`
[INFO]
TITLE = ${pageEval.title}
BG_COLOR_1 = 0x9CE7
BG_COLOR_2 = 0xA108
`)
const iniLinks = [];
for (const index in pageEval.links) {
iniLinks.push(`[LINK${index}]\n` + Object.entries(pageEval.links[index])
.map(([key, value]) => `${key} = ${value}`)
.join('\n'))
}
iniContent += iniLinks.join('\n\n')
await Deno.writeTextFile(`nitrofiles/pages/${rootPath}.ini`, iniContent.trim());
}
}
}
await browser.close();
if (jekyll)
jekyll.kill();
for (const tempFile of Object.values(tempFileNames))
if (await exists(tempFile))
await Deno.remove(tempFile)