releases.ts

4.3 KB · raw

1import type { Hono } from "hono";2import type { ReleaseFile } from "../bucket/repo.ts";3import { saveByPrefix } from "../mirror/queries.ts";4import { downloadCounts, type Release, visibleReleases } from "../releases/releases.ts";5import { type Html, html } from "./html.ts";6import {7	cacheFor,8	formatDate,9	formatSize,10	notFound,11	openPublicRepo,12	type PageContext,13	type PublicRepo,14	page,15	plural,16} from "./layout.ts";17import { renderMarkdown } from "./markdown.ts";1819// §11.1's labels.20const PLATFORM_LABELS: Record<string, string> = {21	"linux-x86_64": "Linux · x86_64",22	"linux-aarch64": "Linux · ARM64",23	"macos-aarch64": "macOS · Apple Silicon",24	"macos-x86_64": "macOS · Intel",25	"windows-x86_64": "Windows · x86_64",26	"android-arm64": "Android · ARM64",27	"android-x86": "Android · x86_64",28};2930/** §21.9's builds panel: every visible release, newest first. */31export function registerReleasesPage(app: Hono, ctx: PageContext): void {32	app.get("/:repo/releases", (c) => {33		const repo = openPublicRepo(ctx, c.req.param("repo"));34		if (!repo) return notFound(c, ctx);35		const name = repo.settings.name;36		const releases = visibleReleases(ctx.kepr, repo.mirror, name);37		const counts = downloadCounts(ctx.kepr, name);38		cacheFor(c, false);39		return c.html(40			page(ctx, {41				title: `builds · ${name}`,42				description: `Releases of ${name}, with downloads and checksums.`,43				path: `/${name}/releases`,44				body: html`<p class="breadcrumb mono"><a href="/${name}">${name}</a></p>45<h1 class="file-page-title">builds</h1>46<section class="repo-section">47${48	releases.length > 049		? releases.map((release, index) =>50				index === 051					? html`<div class="release-latest">52<p class="muted mono">latest release</p>53${releaseBlock(repo, release, counts)}54</div>`55					: html`<details class="release-older">56<summary class="mono">${index === 1 ? "earlier releases · " : ""}<span>v${release.version}</span>57· ${formatDate(release.publishedAt)}</summary>58${releaseBlock(repo, release, counts)}59</details>`,60			)61		: html`<p class="muted mono save-empty">no releases yet</p>`62}63</section>`,64			}),65		);66	});67}6869function releaseBlock(repo: PublicRepo, release: Release, counts: Map<string, number>): Html {70	const name = repo.settings.name;71	const save = saveByPrefix(repo.mirror, release.save);72	// A checkpoint has no page of its own (§21.4), and a save the mirror lacks has none either.73	const saveLink =74		save && !save.is_checkpoint75			? html`<a class="history-id" href="/${name}/saves/${save.id}">${save.id}</a>`76			: html`<span class="history-id">${release.save}</span>`;77	// §11.2: empty notes show the release save's message, else "Release <v> of <repo>."78	const notes =79		release.notes !== ""80			? renderMarkdown(release.notes, { repo: name, dir: "", at: null })81			: html`<p>${save?.message ?? `Release ${release.version} of ${name}.`}</p>`;82	return html`<article class="release" id="${release.version}">83<p class="release-head"><span class="release-version">v${84		release.version85	}</span>${saveLink}<span class="history-when">${formatDate(release.publishedAt)}</span></p>86<div class="release-notes markdown">${notes}</div>87<div class="release-files">88${release.files.map((file) =>89	fileRow(name, release.version, file, counts.get(`${release.version}/${file.platform}`) ?? 0),90)}91</div>92</article>`;93}9495function fileRow(repo: string, version: string, file: ReleaseFile, downloads: number): Html {96	const hint = installHint(file.name);97	// The `<repo>-<platform>` form, as Kepr 2 linked it, so `curl -O` saves a self-describing name.98	return html`<div class="release-file">99<p class="release-file-head"><span class="release-platform">${100		PLATFORM_LABELS[file.platform]101	}</span>${html`<a class="release-download mono" href="${`/${repo}/releases/${version}/${repo}-${file.platform}`}">↓ ${file.name}</a>`}${html`<span class="release-meta mono">${formatSize(file.size)} · ${plural(downloads, "download")}</span>`}</p>102<p class="release-sha mono"><span class="muted">sha256</span> ${file.sha256}</p>103${hint && html`<p class="release-hint muted">${hint}</p>`}104</div>`;105}106107// Kepr 2's install hints, with the wording of §21.9.108function installHint(fileName: string): string {109	const lower = fileName.toLowerCase();110	if (lower.endsWith(".app.zip")) return "unzip and drag to Applications";111	if (lower.endsWith(".apk")) {112		return "open on your device to install — allow installs from this source if prompted";113	}114	return "";115}