gamecult-site/scripts/shipped/ledger.mjs
Void e7ac10ecdb
Some checks failed
Deploy Quartz / deploy (push) Has been cancelled
Deploy Quartz / build (push) Has been cancelled
Deploy Quartz / deploy-1 (push) Has been cancelled
Shipped: dated ledger of merged work (do not merge) (#14)
2026-11-12 23:55:56 +00:00

120 lines
4.8 KiB
JavaScript

// Pure ledger logic: normalize, group by week, herald, render. No I/O.
export const SITE = "https://gamecult.org"
const DAY = 86400000
// Monday 00:00 UTC of the week containing the instant.
export function weekStart(iso) {
const d = new Date(iso)
const day = (d.getUTCDay() + 6) % 7
return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate() - day)).toISOString().slice(0, 10)
}
export function pullEntry(repo, pr) {
return { kind: "pr", repo, title: pr.title.trim(), date: pr.merged_at, url: pr.html_url }
}
export function releaseEntry(repo, rel) {
return {
kind: "release",
repo,
title: `Release ${rel.name?.trim() || rel.tag_name}`,
date: rel.published_at,
url: rel.html_url,
}
}
const byNewest = (a, b) => (a.date < b.date ? 1 : a.date > b.date ? -1 : a.url < b.url ? 1 : -1)
// Weeks newest first, entries newest first. Weeks with nothing between the
// oldest and newest entry are kept (empty), so a gap shows as a gap.
export function groupByWeek(entries) {
if (entries.length === 0) return []
const buckets = new Map()
for (const e of entries) {
const k = weekStart(e.date)
if (!buckets.has(k)) buckets.set(k, [])
buckets.get(k).push(e)
}
const keys = [...buckets.keys()].sort()
const weeks = []
for (let t = Date.parse(keys[0]); t <= Date.parse(keys[keys.length - 1]); t += 7 * DAY) {
const start = new Date(t).toISOString().slice(0, 10)
weeks.push({ start, entries: (buckets.get(start) ?? []).sort(byNewest) })
}
weeks.reverse()
const busiest = Math.max(...weeks.map((w) => w.entries.length))
for (const w of weeks) w.herald = herald(w, busiest)
return weeks
}
// A line only where a computed fact backs it. First rule that holds wins.
export function herald(week, busiest) {
const n = week.entries.length
const repos = new Set(week.entries.map((e) => e.repo))
const releases = week.entries.filter((e) => e.kind === "release").length
if (n === 0) return "Nothing merged. The ledger records it anyway."
if (n === busiest && n >= 10) return `The busiest week on the ledger: ${n} entries.`
if (repos.size >= 5) return `${repos.size} repos moved in one week. The studio is not one project in a trench coat.`
const counts = [...repos].map((r) => [r, week.entries.filter((e) => e.repo === r).length]).sort((a, b) => b[1] - a[1])
if (n >= 6 && counts[0][1] / n >= 0.8) return `Mostly ${counts[0][0]}. The rest kept their heads down.`
if (releases > 0) return releases === 1 ? "A release went out." : `${releases} releases went out.`
return null
}
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
const dayLabel = (iso) => `${+iso.slice(8, 10)} ${MONTHS[+iso.slice(5, 7) - 1]}`
export const weekLabel = (start) => `Week of ${dayLabel(start)} ${start.slice(0, 4)}`
const mdEscape = (s) => s.replace(/([\\`*_[\]<>|])/g, "\\$1")
export function renderPage(weeks, generated) {
const total = weeks.reduce((s, w) => s + w.entries.length, 0)
const out = [
"---",
"title: Shipped",
'description: "What GameCult actually merged, by week, with a receipt for each."',
'socialDeck: "A dated ledger of merged work, linked to the receipts."',
"---",
"",
"<!-- Generated by scripts/shipped/refresh.mjs from data/shipped.json. Do not edit by hand. -->",
"",
`Merged pull requests and releases across the public GameCult repositories on [our forge](https://eureka.gamecult.org/forge/GameCult), newest first. Each line links to the pull request or release it came from. ${total} entries; snapshot taken ${generated.slice(0, 10)}. [Feed](/static/shipped.xml).`,
"",
]
for (const w of weeks) {
out.push(`## ${weekLabel(w.start)}`, "")
if (w.herald) out.push(`*${w.herald}*`, "")
for (const e of w.entries) {
out.push(`- ${e.date.slice(0, 10)} · ${e.repo} · ${mdEscape(e.title)} — [receipt](${e.url})`)
}
if (w.entries.length) out.push("")
}
return out.join("\n")
}
const xml = (s) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;")
export function renderAtom(entries, generated, limit = 50) {
const items = [...entries].sort(byNewest).slice(0, limit)
const updated = items[0]?.date ?? generated
return [
'<?xml version="1.0" encoding="utf-8"?>',
'<feed xmlns="http://www.w3.org/2005/Atom">',
" <title>GameCult: Shipped</title>",
` <id>${SITE}/Shipped</id>`,
` <link href="${SITE}/Shipped"/>`,
` <link rel="self" href="${SITE}/static/shipped.xml"/>`,
` <updated>${updated}</updated>`,
...items.flatMap((e) => [
" <entry>",
` <title>${xml(`${e.repo}: ${e.title}`)}</title>`,
` <id>${xml(e.url)}</id>`,
` <link href="${xml(e.url)}"/>`,
` <updated>${e.date}</updated>`,
" </entry>",
]),
"</feed>",
"",
].join("\n")
}