-
Notifications
You must be signed in to change notification settings - Fork 16
/
gather-fonts.mjs
81 lines (75 loc) · 2.25 KB
/
gather-fonts.mjs
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
import got from "got";
import { promises as fs } from "fs";
import { promisify } from "node:util";
import stream from "node:stream";
import css from "css";
import "dotenv/config";
const base = "gwfh.mranftl.com";
const data = await got.get(`https://${base}/api/fonts/`).json();
const storageZoneName = "coolfonts";
const families = data.map((d) => d.id).filter((n) => n);
const pipeline = promisify(stream.pipeline);
const subsets = new Set();
for (const subset of data) {
for (const range of subset.subsets) {
subsets.add(range);
}
}
// Generate subsets
const allSubsets = {};
let currentSubset;
for (const subset of Array.from(subsets)) {
console.log("Checking subset: ", subset);
const example = data.find((d) => d.subsets.includes(subset));
const gf = await got
.get(`https://fonts.googleapis.com/css2?family=${example.family}`, {
headers: {
"User-Agent":
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36",
},
})
.text();
const parsedCss = css.parse(gf);
parsedCss.stylesheet.rules.map((a) => {
if (a.type === "comment") {
currentSubset = a.comment.trim();
allSubsets[a.comment.trim()] = "";
}
if (a.type === "font-face") {
const value = a.declarations.find(
(d) => d.property === "unicode-range"
).value;
allSubsets[currentSubset] = value;
}
});
}
await fs.writeFile("./subsets.json", JSON.stringify(allSubsets));
// Upload fonts to BunnyCDN
if (process.env.BUNNY_API_KEY) {
for (const family of families) {
console.log("Doing: ", family);
const { variants } = await got
.get(`https://${base}/api/fonts/${family}`)
.json();
for (const variant of variants) {
const id = variant.fontWeight;
const dir = `./${family}/${variant.fontStyle}`;
try {
await pipeline(
got.stream(`${variant.woff2}`),
await got.stream.put(
`https://storage.bunnycdn.com/${storageZoneName}/${dir}/${id}.woff2`,
{
headers: {
AccessKey: process.env.BUNNY_API_KEY,
},
}
),
new stream.PassThrough()
);
} catch (err) {
console.log(err);
}
}
}
}