From 4bfecd462e20b27223618669b2e2c93a49d321a0 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 03:36:05 +0200 Subject: [PATCH] Introduce `fromGlob` helper --- lib/rx-utils.ts | 12 ++++++++---- lib/tasks/articles.ts | 8 +++----- lib/tasks/css.ts | 8 +++----- lib/tasks/fonts.ts | 9 ++++----- lib/tasks/images.ts | 8 +++----- lib/tasks/svg.ts | 12 ++++-------- 6 files changed, 25 insertions(+), 32 deletions(-) diff --git a/lib/rx-utils.ts b/lib/rx-utils.ts index e588364..aac02ab 100644 --- a/lib/rx-utils.ts +++ b/lib/rx-utils.ts @@ -1,9 +1,10 @@ -import { Observable, Subscriber } from "rxjs"; +import { Observable, Subscriber, from, mergeMap } from "rxjs"; import File from "vinyl"; import { readFile } from "node:fs/promises"; import { join, dirname } from "node:path"; import { existsSync } from "node:fs"; import { writeFile, mkdir } from "node:fs/promises"; +import { Glob } from "glob"; export function dest(prefix: string) { return async (file: File) => { @@ -18,9 +19,6 @@ export function dest(prefix: string) { }; } -export const loadFile = async (path: string): Promise => - new File({ path, contents: await readFile(path) }); - export function onComplete(f: (sink: Subscriber) => Promise) { return (observable: Observable) => new Observable((subscriber) => @@ -38,3 +36,9 @@ export function onComplete(f: (sink: Subscriber) => Promise) { }), ); } + +const loadFile = async (path: string): Promise => + new File({ path, contents: await readFile(path) }); + +export const fromGlob = (paths: string | string[]): Observable => + from(new Glob(paths, {})).pipe(mergeMap(loadFile)); diff --git a/lib/tasks/articles.ts b/lib/tasks/articles.ts index 57b456e..71da2bf 100644 --- a/lib/tasks/articles.ts +++ b/lib/tasks/articles.ts @@ -18,9 +18,8 @@ import { renderToStaticMarkup } from "preact-render-to-string"; import { SITE_TITLE, SITE_DESCRIPTION } from "../constants.js"; import { JSX } from "preact/jsx-runtime"; import { reloadAssets } from "../assets.js"; -import { from, mergeMap, Observable, Subscriber } from "rxjs"; -import { dest, loadFile, onComplete } from "../rx-utils.js"; -import { Glob } from "glob"; +import { mergeMap, Observable, Subscriber } from "rxjs"; +import { dest, fromGlob, onComplete } from "../rx-utils.js"; const Asciidoctor = asciidoctor(); const EXTENSION_REGISTRY = Asciidoctor.Extensions.create(); @@ -120,9 +119,8 @@ export const articles = () => { reloadAssets(); const articles = []; - return from(new Glob("articles/**/*.asciidoc", {})) + return fromGlob("articles/**/*.asciidoc") .pipe( - mergeMap(loadFile), mergeMap( (file) => new Observable((subscriber) => { diff --git a/lib/tasks/css.ts b/lib/tasks/css.ts index 92fcbdb..87d0ed7 100644 --- a/lib/tasks/css.ts +++ b/lib/tasks/css.ts @@ -1,14 +1,12 @@ import postcss, { Result } from "postcss"; import config from "../../postcss.config.js"; -import { dest, loadFile } from "../rx-utils.js"; -import { from, lastValueFrom, mergeMap } from "rxjs"; +import { dest, fromGlob } from "../rx-utils.js"; +import { lastValueFrom, mergeMap } from "rxjs"; import hashPaths from "../hash.js"; -import { Glob } from "glob"; export const css = () => lastValueFrom( - from(new Glob("src/index.css", {})).pipe( - mergeMap(loadFile), + fromGlob("src/index.css").pipe( mergeMap( (file) => new Promise((resolve) => diff --git a/lib/tasks/fonts.ts b/lib/tasks/fonts.ts index 6627ce7..6873107 100644 --- a/lib/tasks/fonts.ts +++ b/lib/tasks/fonts.ts @@ -3,9 +3,8 @@ import tmp from "tmp"; import { execFile } from "node:child_process"; import { readFileSync, unlinkSync } from "node:fs"; import hashPaths from "../hash.js"; -import { dest, loadFile } from "../rx-utils.js"; -import { from, mergeMap } from "rxjs"; -import { Glob } from "glob"; +import { dest, fromGlob } from "../rx-utils.js"; +import { mergeMap } from "rxjs"; const FONT_PRESETS = { mono: { ranges: ["20-7F", "2205", "2E22-2E25", "2713", "2717"] }, @@ -35,6 +34,6 @@ function compileFont(font: File): Promise { } export const fonts = () => - from(new Glob("vendor/*.ttf", {})) - .pipe(mergeMap(loadFile), mergeMap(compileFont), hashPaths("fonts.manifest")) + fromGlob("vendor/*.ttf") + .pipe(mergeMap(compileFont), hashPaths("fonts.manifest")) .forEach(dest("dist/_assets")); diff --git a/lib/tasks/images.ts b/lib/tasks/images.ts index 69b5dc7..5e50714 100644 --- a/lib/tasks/images.ts +++ b/lib/tasks/images.ts @@ -1,12 +1,10 @@ -import { Glob } from "glob"; import hashPaths from "../hash.js"; -import { dest, loadFile } from "../rx-utils.js"; -import { from, mergeMap, tap } from "rxjs"; +import { dest, fromGlob } from "../rx-utils.js"; +import { mergeMap, tap } from "rxjs"; export const images = () => - from(new Glob("src/*.avif", {})) + fromGlob("src/*.avif") .pipe( - mergeMap(loadFile), tap((f) => (f.path = f.path.substring(4))), hashPaths("images.manifest"), ) diff --git a/lib/tasks/svg.ts b/lib/tasks/svg.ts index 6bc0e22..1d48e09 100644 --- a/lib/tasks/svg.ts +++ b/lib/tasks/svg.ts @@ -1,12 +1,8 @@ -import { Glob } from "glob"; -import { from, mergeMap, tap } from "rxjs"; -import { dest, loadFile } from "../rx-utils.js"; +import { tap } from "rxjs"; +import { dest, fromGlob } from "../rx-utils.js"; // SVG `use` has no way of allowing cross-origin, so we need to keep them with the HTML files. export const svg = () => - from(new Glob("src/*.svg", {})) - .pipe( - mergeMap(loadFile), - tap((f) => (f.path = f.path.substring(4))), - ) + fromGlob("src/*.svg") + .pipe(tap((f) => (f.path = f.path.substring(4)))) .forEach(dest("dist"));