forked from vercel/turborepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch-google-font-data.js
49 lines (41 loc) · 1.4 KB
/
fetch-google-font-data.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
// Derived from https://github.com/vercel/next.js/blob/a19f04c5a1bbb27a9c7cbbc77a137e4a288abe1a/scripts/update-google-fonts.js
// Only includes generating the font-data.json, as TypeScript typings are maintained in the `next` npm package in the Next.js repo.
// This script writes the single file to stdout instead of directly to disk.
(async () => {
const { familyMetadataList } = await fetch(
"https://fonts.google.com/metadata/fonts"
).then((r) => {
if (r.status >= 400) {
throw new Error(
`Received bad status ${r.status} when retrieving font metadata`
);
}
return r.json();
});
const fontData = {};
for (let { family, fonts, axes, subsets } of familyMetadataList) {
subsets = subsets.filter((subset) => subset !== "menu");
const weights = new Set();
const styles = new Set();
for (const variant of Object.keys(fonts)) {
if (variant.endsWith("i")) {
styles.add("italic");
weights.add(variant.slice(0, -1));
continue;
} else {
styles.add("normal");
weights.add(variant);
}
}
const hasVariableFont = axes.length > 0;
if (hasVariableFont) {
weights.add("variable");
}
fontData[family] = {
weights: [...weights],
styles: [...styles],
axes: hasVariableFont ? axes : undefined,
};
}
process.stdout.write(JSON.stringify(fontData, null, 2) + "\n");
})();