mirror of
https://github.com/apophenia-news/apophenia.news.git
synced 2026-03-17 03:11:01 +00:00
Refactor: Extract file I/O helpers
This commit is contained in:
30
scripts/lib/io.mjs
Normal file
30
scripts/lib/io.mjs
Normal file
@@ -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")
|
||||
]);
|
||||
};
|
||||
Reference in New Issue
Block a user