From 6b3d96b321b4230a70817e6a8030e84925e71e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20=E2=80=9CAd=C3=A6dra=E2=80=9D=20Hamel?= Date: Thu, 27 Jun 2024 09:03:53 +0200 Subject: [PATCH] Fonts: use Deno methods --- lib/tasks/fonts.ts | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/lib/tasks/fonts.ts b/lib/tasks/fonts.ts index 6a26c7d..d0a75bb 100644 --- a/lib/tasks/fonts.ts +++ b/lib/tasks/fonts.ts @@ -1,36 +1,30 @@ import File from "vinyl"; import tmp from "tmp"; -import { execFile } from "node:child_process"; -import { readFileSync, unlinkSync } from "node:fs"; import hashPaths from "../hash.ts"; import { dest, fromGlob } from "../rx-utils.ts"; import { lastValueFrom, mergeMap } from "rxjs"; +import { Buffer } from "node:buffer"; const FONT_PRESETS: { [variant: string]: { ranges: string[] } } = { mono: { ranges: ["20-7F", "2205", "2E22-2E25", "2713", "2717"] }, text: { ranges: ["20-7F", "A0-FF", "2000-206F", "20AC"] }, }; -function compileFont(font: File): Promise { +async function compileFont(font: File): Promise { const [, variant, weight] = /([A-Z][a-z]+)-(\w+)\.ttf$/.exec(font.basename) as string[]; const tmpOutput = tmp.fileSync({ discardDescriptor: true }); const unicodes = FONT_PRESETS[variant.toLowerCase()].ranges; - return new Promise((resolve, reject) => { - execFile("pyftsubset", [ - font.path, - `--unicodes=${unicodes.join(",")}`, - `--output-file=${tmpOutput.name}`, - "--flavor=woff2", - ]).on("error", (e) => reject(e)).once("exit", () => { - font.path = `iosevka-adaedra-${variant.toLowerCase()}-${weight.toLowerCase()}.woff2`; - font.contents = readFileSync(tmpOutput.name); - (font as any).base = null; + await new Deno.Command("pyftsubset", { + args: [font.path, `--unicodes=${unicodes.join(",")}`, `--output-file=${tmpOutput.name}`, "--flavor=woff2"], + }).output(); - unlinkSync(tmpOutput.name); - resolve(font); - }); - }); + font.path = `iosevka-adaedra-${variant.toLowerCase()}-${weight.toLowerCase()}.woff2`; + font.contents = Buffer.from(await Deno.readFile(tmpOutput.name)); + font.base = null; + + await Deno.remove(tmpOutput.name); + return font; } export const fonts = async () => {