From 0b373fc57ea4a800143d45e16731af33c5afef9d 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 08:51:56 +0200 Subject: [PATCH] Minor adjustments --- bin/build.ts | 17 +++++++++-------- lib/tasks/fonts.ts | 16 +++++++++------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/bin/build.ts b/bin/build.ts index 7119a3d..c61cd1a 100644 --- a/bin/build.ts +++ b/bin/build.ts @@ -6,31 +6,32 @@ import { svg } from "../lib/tasks/svg.ts"; import { argv, exit } from "node:process"; -const wrapTask = (name: string, task: () => Promise) => async () => { +type Task = () => Promise; + +const wrapTask = (name: string, task: Task) => async () => { console.log("[start]", name); await task(); console.log("[end]", name); }; -const TASKS: { [task: string]: () => Promise } = { +const TASKS = new Map(Object.entries({ articles: wrapTask("articles", articles), css: wrapTask("css", css), fonts: wrapTask("fonts", fonts), images: wrapTask("images", images), svg: wrapTask("svg", svg), -}; +})); const ALL_TASKS = ["fonts", "images", "svg", "css", "articles"]; const args = argv.slice(2); -console.log("args", args); -(args.length ? args : ALL_TASKS) +await (args.length ? args : ALL_TASKS) .map((task: string) => { - if (!TASKS.hasOwnProperty(task)) { + if (!TASKS.has(task)) { console.error("Unknown task", task); exit(1); } - return TASKS[task]; + return TASKS.get(task)!; }) - .reduce((prev: Promise, cur: () => Promise) => prev.then(cur), Promise.resolve()); + .reduce((prev: Promise, cur: Task) => prev.then(cur), Promise.resolve()); diff --git a/lib/tasks/fonts.ts b/lib/tasks/fonts.ts index e0d86a3..6a26c7d 100644 --- a/lib/tasks/fonts.ts +++ b/lib/tasks/fonts.ts @@ -4,7 +4,7 @@ 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 { mergeMap } from "rxjs"; +import { lastValueFrom, mergeMap } from "rxjs"; const FONT_PRESETS: { [variant: string]: { ranges: string[] } } = { mono: { ranges: ["20-7F", "2205", "2E22-2E25", "2713", "2717"] }, @@ -16,13 +16,13 @@ function compileFont(font: File): Promise { const tmpOutput = tmp.fileSync({ discardDescriptor: true }); const unicodes = FONT_PRESETS[variant.toLowerCase()].ranges; - return new Promise((resolve) => { + return new Promise((resolve, reject) => { execFile("pyftsubset", [ font.path, `--unicodes=${unicodes.join(",")}`, `--output-file=${tmpOutput.name}`, "--flavor=woff2", - ]).once("exit", () => { + ]).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; @@ -33,7 +33,9 @@ function compileFont(font: File): Promise { }); } -export const fonts = () => - fromGlob("vendor/*.ttf") - .pipe(mergeMap(compileFont), hashPaths("fonts.manifest")) - .forEach(dest("dist/_assets")); +export const fonts = async () => { + await lastValueFrom( + fromGlob("vendor/*.ttf") + .pipe(mergeMap(compileFont), hashPaths("fonts.manifest"), mergeMap(dest("dist/_assets"))), + ); +};