-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathprepare-font.js
65 lines (56 loc) · 1.68 KB
/
prepare-font.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const { loadEnvConfig } = require("@next/env");
const fs = require("node:fs").promises;
loadEnvConfig(process.cwd());
const fileNames = [
"Rosart-Regular",
"Rosart-RegularItalic",
"Rosart-Medium",
"Rosart-SemiBold",
"Rosart-Bold",
];
const downloadFile = async (url, destination) => {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
}
const buffer = await response.arrayBuffer();
await fs.writeFile(destination, Buffer.from(buffer));
};
(async () => {
if (
// Check if the environment is production or force downloading fonts
(process.env.NODE_ENV === "production" &&
process.env.VERCEL_ENV === "production") ||
process.env.FORCE_DOWNLOAD_FONTS
) {
const BLOB_URL = process.env.BLOB_URL;
if (!BLOB_URL) {
console.error("BLOB_URL is not defined");
return;
}
const filesToDownload = fileNames.map((fileName) => ({
url: `${BLOB_URL}/fonts/${fileName}.woff2`,
destination: `./app/fonts/${fileName}.woff2`,
}));
fs.copyFile("./vendor/fonts/rosart.ts", "./app/fonts/index.ts");
for (const file of filesToDownload) {
try {
const fileExists = await fs
.access(file.destination)
.then(() => true)
.catch(() => false);
if (!fileExists) {
await downloadFile(file.url, file.destination);
console.log(`Downloaded: ${file.destination}`);
} else {
console.log(`File already exists: ${file.destination}`);
}
} catch (error) {
console.error(`Error downloading ${file.url}:`, error);
}
}
} else {
fs.copyFile("./vendor/fonts/alternative.ts", "./app/fonts/index.ts");
console.log("Using an alternative font file in development mode.");
}
})();