Introduce fromGlob helper

This commit is contained in:
Thibault “Adædra” Hamel 2024-06-27 03:36:05 +02:00
parent 500361c023
commit 4bfecd462e
6 changed files with 25 additions and 32 deletions

View File

@ -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<File> =>
new File({ path, contents: await readFile(path) });
export function onComplete<T>(f: (sink: Subscriber<T>) => Promise<void>) {
return (observable: Observable<T>) =>
new Observable<T>((subscriber) =>
@ -38,3 +36,9 @@ export function onComplete<T>(f: (sink: Subscriber<T>) => Promise<void>) {
}),
);
}
const loadFile = async (path: string): Promise<File> =>
new File({ path, contents: await readFile(path) });
export const fromGlob = (paths: string | string[]): Observable<File> =>
from(new Glob(paths, {})).pipe(mergeMap(loadFile));

View File

@ -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<File>((subscriber) => {

View File

@ -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) =>

View File

@ -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<File> {
}
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"));

View File

@ -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"),
)

View File

@ -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"));