import { from, mergeMap, Observable, Subscriber } from "rxjs"; import { dirname, join } from "node:path"; import { existsSync } from "node:fs"; import { mkdir, writeFile } from "node:fs/promises"; import { Glob } from "glob"; import { VFile } from "vfile"; import { read } from "to-vfile"; import { info } from "./log.ts"; export function dest(prefix: string) { return async (file: VFile) => { const actualPath = join(prefix, file.path); if (!existsSync(dirname(actualPath))) { await mkdir(dirname(actualPath), { recursive: true }); } await writeFile(actualPath, file.value); info("Written", { path: actualPath }); return file; }; } export function onComplete(f: (sink: Subscriber) => Promise) { return (observable: Observable) => new Observable((subscriber) => observable.subscribe({ next(value) { subscriber.next(value); }, error(err) { subscriber.error(err); }, complete() { f(subscriber); subscriber.complete(); }, }) ); } const loadFile = (path: string): Promise => read(join(Deno.cwd(), path)).then((vfile) => { vfile.path = path; return vfile; }); export const fromGlob = (paths: string | string[]): Observable => from(new Glob(paths, {})).pipe(mergeMap(loadFile));