-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathfetch-google-fonts.ts
More file actions
101 lines (81 loc) · 2.8 KB
/
fetch-google-fonts.ts
File metadata and controls
101 lines (81 loc) · 2.8 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
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
/**
* Fetch Google Fonts Catalog
*
* Downloads the full Google Fonts catalog and saves it as a static JSON file.
* This avoids requiring users to set up a Google Fonts API key.
*
* Usage: npm run fonts:update
*
* The output file is committed to the repo so forked projects get it out of the box.
* Re-run periodically to pick up newly added Google Fonts.
*/
import fs from 'fs';
import path from 'path';
const OUTPUT_DIR = path.join(process.cwd(), 'storage', 'fonts');
const OUTPUT_FILE = path.join(OUTPUT_DIR, 'google-fonts.json');
const API_URL = 'https://www.googleapis.com/webfonts/v1/webfonts?sort=popularity&capability=VF';
interface GoogleFontAxis {
tag: string;
start: number;
end: number;
}
interface GoogleFontItem {
family: string;
variants: string[];
category: string;
axes?: GoogleFontAxis[];
}
/** Load key=value pairs from .env into process.env */
function loadEnv() {
const envPath = path.join(process.cwd(), '.env');
if (!fs.existsSync(envPath)) return;
const lines = fs.readFileSync(envPath, 'utf-8').split('\n');
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) continue;
const idx = trimmed.indexOf('=');
if (idx === -1) continue;
const key = trimmed.slice(0, idx).trim();
const val = trimmed.slice(idx + 1).trim().replace(/^["']|["']$/g, '');
if (!process.env[key]) process.env[key] = val;
}
}
async function fetchGoogleFonts() {
loadEnv();
// Allow API key via .env or CLI arg
const apiKey = process.env.GOOGLE_FONTS_API_KEY || process.argv[2];
if (!apiKey) {
throw new Error(
'GOOGLE_FONTS_API_KEY is required.\n' +
'Add it to .env or pass as argument: npm run fonts:update -- YOUR_KEY'
);
}
const url = `${API_URL}&key=${apiKey}`;
console.log('Fetching Google Fonts catalog...');
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Google Fonts API returned ${response.status}: ${response.statusText}`);
}
const data = await response.json();
const items: GoogleFontItem[] = (data.items || []).map((font: Record<string, unknown>) => {
const item: GoogleFontItem = {
family: font.family as string,
variants: font.variants as string[],
category: font.category as string,
};
const axes = font.axes as GoogleFontAxis[] | undefined;
if (axes && axes.length > 0) {
item.axes = axes;
}
return item;
});
// Ensure output directory exists
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
// Write the catalog
fs.writeFileSync(OUTPUT_FILE, JSON.stringify(items, null, 2), 'utf-8');
console.log(`Saved ${items.length} fonts to ${path.relative(process.cwd(), OUTPUT_FILE)}`);
}
fetchGoogleFonts().catch((error) => {
console.error('Failed to fetch Google Fonts:', error);
process.exit(1);
});