From 7849e417dfa0c93648e8ccf0d5263c73073de2da Mon Sep 17 00:00:00 2001 From: multipleof4 Date: Thu, 26 Feb 2026 17:39:28 -0800 Subject: [PATCH] Refactor: Extract file I/O helpers --- scripts/lib/io.mjs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 scripts/lib/io.mjs diff --git a/scripts/lib/io.mjs b/scripts/lib/io.mjs new file mode 100644 index 0000000..9eba13e --- /dev/null +++ b/scripts/lib/io.mjs @@ -0,0 +1,30 @@ +import fs from "node:fs/promises"; +import path from "node:path"; +import { PUBLIC, SRC } from "./constants.mjs"; +import { renderRobots, renderRss, renderSitemap } from "./renderers.mjs"; + +export const ensureCleanGenerated = async () => { + await fs.mkdir(SRC, { recursive: true }); + const children = await fs.readdir(SRC, { withFileTypes: true }); + const keep = new Set(["assets"]); + await Promise.all( + children + .filter((d) => d.isDirectory() && !keep.has(d.name)) + .map((d) => fs.rm(path.join(SRC, d.name), { recursive: true, force: true })) + ); +}; + +export const writePage = async (parts, html) => { + const filePath = path.join(...parts); + await fs.mkdir(path.dirname(filePath), { recursive: true }); + await fs.writeFile(filePath, html, "utf8"); +}; + +export const writeDiscoveryFiles = async (articles, authors) => { + await fs.mkdir(PUBLIC, { recursive: true }); + await Promise.all([ + fs.writeFile(path.join(PUBLIC, "rss.xml"), renderRss(articles), "utf8"), + fs.writeFile(path.join(PUBLIC, "sitemap.xml"), renderSitemap(articles, authors), "utf8"), + fs.writeFile(path.join(PUBLIC, "robots.txt"), renderRobots(), "utf8") + ]); +};