// pulls recent commits from my repos on tangled and writes them into the
// static html.
// run with `mise run sync-commits`. no dependencies, node 18+.
//
// a page opts in with a pair of marker comments naming the repo and how many
// commits to show; everything between them is replaced on every run:
//
// ...
//
// commits come from main, newest first, read from the knot that hosts my
// repos (no login needed). if it can't be reached, the script exits non-zero
// and touches nothing.
import { readFile, writeFile } from "node:fs/promises";
const KNOT = "https://knot1.tangled.sh";
const OWNER = "did:plc:e3z5gj5iyuwgqao562bo4bdf"; // danieldaum.net
const TZ = "America/Los_Angeles";
const PAGES = ["projects/index.html", "projects/holonet/index.html"];
const MARKER = /^([ \t]*)[\s\S]*?/gm;
// ---------------------------------------------------------------------------
// data
async function fetchCommits(repo, limit) {
const url = new URL("/xrpc/sh.tangled.repo.log", KNOT);
url.searchParams.set("repo", `${OWNER}/${repo}`);
url.searchParams.set("ref", "main");
url.searchParams.set("limit", String(limit));
const res = await fetch(url);
if (!res.ok) throw new Error(`${repo} commits: ${res.status} ${res.statusText}`);
const { commits } = await res.json();
return commits.map((c) => ({ sha: c.this, message: c.message, date: c.author.When }));
}
// ---------------------------------------------------------------------------
// html
function render(commits) {
if (commits.length === 0) return `