Page MenuHomeSealhub

get-fonts.ts
No OneTemporary

get-fonts.ts

import { readFile, writeFile } from "fs/promises";
import { constructURL, download } from "google-fonts-helper";
import { resolve } from "node:path";
import { target_locreq } from "./target-locreq.js";
import JSON5 from "json5";
export const FONTS_CONFIG_PATH = "src/fonts.json";
export async function getFonts(_: Record<string, string | boolean> = {}) {
const fonts_config_path = target_locreq.resolve(FONTS_CONFIG_PATH);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const fonts_config = JSON5.parse(await readFile(fonts_config_path, "utf8"));
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
const fonts_url = constructURL(fonts_config.googleFonts);
if (!fonts_url) {
throw new Error("Invalid font config");
}
const downloader = download(fonts_url, {
base64: false,
overwriting: false,
outputDir: target_locreq.resolve("public/dist/fonts"),
stylePath: "fonts.css",
fontsDir: "./",
fontsPath: "/dist/fonts",
});
await downloader.execute();
const fonts_css_file_path = resolve(
target_locreq.resolve("public/dist/fonts"),
"fonts.css"
);
const content = await readFile(fonts_css_file_path, "utf-8");
await writeFile(
fonts_css_file_path,
content.replace(/@font-face {/g, "@font-face {\nfont-display: swap;")
);
console.log("Downloaded new fonts pack");
}

File Metadata

Mime Type
text/x-java
Expires
Wed, May 7, 19:47 (1 d, 4 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
625474
Default Alt Text
get-fonts.ts (1 KB)

Event Timeline