hornwitser.no/cli.ts
Hornwitser cf3229423b Fix create folder logic in cli.js
The paths changed in d003ea01 to start with a forward slash, this broke
the create folder detection logic as now it matched the first slash of
the path instead of sub-folder in the path.  Fix by ignoring the first
character in the path.
2025-01-25 08:41:25 +01:00

40 lines
1 KiB
TypeScript

import * as fs from "node:fs";
import * as posix from "node:path/posix";
import { prettify, htmlDocument } from "antihtml";
import { pages } from "./content/pages.js";
import type { Page } from "./content/types.js";
import { resolveRefs } from "./utils/resolve-refs.js";
function pageToHtml(page: Page) {
if (!page.ref.startsWith("/")) {
throw new Error(`ref "${page.ref}" for "${page.title}" is not absolute.`);
}
return htmlDocument(
prettify(
resolveRefs(
page.content,
posix.dirname(page.ref),
)
)
)
}
const outDir = "build/web";
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir);
}
for (const page of pages) {
const dirSep = page.ref.indexOf("/", 1);
if (dirSep !== -1) {
const dir = `${outDir}/${page.ref.slice(0, dirSep)}`;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
}
console.log(`writing ${outDir}${page.ref}`);
fs.writeFileSync(`${outDir}${page.ref}`, pageToHtml(page));
}
console.log(`writing ${outDir}/style.css`);
fs.writeFileSync(`${outDir}/style.css`, fs.readFileSync("public/style.css"));