Public: anyone can read this; the crew can change it.
A template card lists the files it will create before it is chosen #16822
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16317-a-template-card-lists-the-files-it-will → main 0 commits fork 3 threads · 3 unresolved
6 files
changed +44
−17
docs/design/areas/templates.md +2
| @@ -1,3 +1,5 @@ | ||
| 1 | 1 | # Templates |
| 2 | 2 | |
| 3 | 3 | - `.template-thumb` — a template's picture on /templates: its newest look in a fixed 64×40 box (lazy, alt naming the template), or the same box holding the template's initial when it has no look, so a row never shifts and never shows a broken image. |
| 4 | ||
| 5 | `.holds` is the Files, Run and Check rows under a template; `.files-list` is the paragraph of names inside its Files fold (a `details` whose `summary` shows "N files"). |
docs/features/template-holds.md +4 −3
| @@ -5,9 +5,10 @@ row: What a template holds: its files, run and check lines, on /templates | ||
| 5 | 5 | # What a template holds |
| 6 | 6 | |
| 7 | 7 | Before you start a forge from a template, `/templates` shows what it holds as three rows under its name: |
| 8 | **Files** (its top-level files, folders ending in `/`, at most 12, then "and N more"), **Run** (the `run` line of its | |
| 9 | `forge.json`) and **Check** (its `check` line). A row the `forge.json` does not say is left out. Blank says it holds no | |
| 10 | files. A template repository that has no commits, or cannot be read, says "Contents not available." | |
| 8 | **Files** (its top-level files, folders ending in `/`, at most 40, then "and N more"; on the page they sit in a | |
| 9 | fold, "N files", that you open), **Run** (the `run` line of its `forge.json`) and | |
| 10 | **Check** (its `check` line). A row the `forge.json` does not say is left out. Blank, and a repository with no files, | |
| 11 | say it holds no files. A template repository that has no commits, or cannot be read, says "Contents not available." | |
| 11 | 12 | |
| 12 | 13 | What was read is kept on the running forge, keyed by the template's default-branch tip: a page costs one ref read until |
| 13 | 14 | the template changes, then it is read again. |
src/spawn/template_holds.ts +15 −5
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * What a template holds, to check before starting from it (#13855): its top-level files (at most |
| 3 | * 12, then "and N more") and the `run` and `check` lines of its forge.json. The starter's are its | |
| 3 | * 40, then "and N more") and the `run` and `check` lines of its forge.json. The starter's are its | |
| 4 | 4 | * baked files; a template repository is read at its default branch. What was read is kept on the |
| 5 | 5 | * app, keyed by the branch tip, so a request costs one ref read until the template changes. A |
| 6 | 6 | * repository that cannot be read says so. |
| @@ -13,7 +13,7 @@ import { blobForRender, listTree, refTip } from "../git/read.ts"; | ||
| 13 | 13 | import { repoDir } from "../git/repos.ts"; |
| 14 | 14 | import { DEFAULT_TEMPLATE, repoTemplateRef } from "./templates.ts"; |
| 15 | 15 | |
| 16 | export const HOLDS_FILES_MAX = 12; | |
| 16 | export const HOLDS_FILES_MAX = 40; | |
| 17 | 17 | export const HOLDS_BLANK = "Holds no files."; |
| 18 | 18 | export const HOLDS_NONE = "Contents not available."; |
| 19 | 19 | |
| @@ -97,10 +97,20 @@ export async function templateHolds(app: App, id: string): Promise<TemplateHolds | ||
| 97 | 97 | return holds; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | /** The holds as label and value rows, in the order the page and the terminal show them. */ | |
| 101 | export function holdsRows(h: TemplateHolds): { label: string; value: string; code: boolean }[] { | |
| 102 | const rows = [{ label: "Files", value: `${h.files.join(", ")}${h.more > 0 ? ` and ${h.more} more` : ""}`, code: false }]; | |
| 100 | /** The Files row: the names and, past the limit, how many more. */ | |
| 101 | export function filesRow(h: TemplateHolds): { label: string; value: string; code: boolean } { | |
| 102 | return { label: "Files", value: `${h.files.join(", ")}${h.more > 0 ? ` and ${h.more} more` : ""}`, code: false }; | |
| 103 | } | |
| 104 | ||
| 105 | /** The Run and Check rows the forge.json says, in that order. */ | |
| 106 | export function runRows(h: TemplateHolds): { label: string; value: string; code: boolean }[] { | |
| 107 | const rows: { label: string; value: string; code: boolean }[] = []; | |
| 103 | 108 | if (h.run !== undefined) rows.push({ label: "Run", value: h.run, code: true }); |
| 104 | 109 | if (h.check !== undefined) rows.push({ label: "Check", value: h.check, code: true }); |
| 105 | 110 | return rows; |
| 106 | 111 | } |
| 112 | ||
| 113 | /** The holds as label and value rows, in the order the terminal shows them. */ | |
| 114 | export function holdsRows(h: TemplateHolds): { label: string; value: string; code: boolean }[] { | |
| 115 | return [filesRow(h), ...runRows(h)]; | |
| 116 | } |
src/web/pages/templates.ts +7 −2
| @@ -9,7 +9,7 @@ import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.t | ||
| 9 | 9 | import type { Ctx } from "../../http/context.ts"; |
| 10 | 10 | import { listChildren } from "../../actions/spawn.ts"; |
| 11 | 11 | import { DEFAULT_TEMPLATE } from "../../spawn/templates.ts"; |
| 12 | import { HOLDS_BLANK, HOLDS_NONE, holdsRows, templateHolds, type TemplateHolds } from "../../spawn/template_holds.ts"; | |
| 12 | import { HOLDS_BLANK, HOLDS_NONE, runRows, templateHolds, type TemplateHolds } from "../../spawn/template_holds.ts"; | |
| 13 | 13 | import { filterTemplates, TEMPLATES_EMPTY, noRepoTemplates, TEMPLATES_EMPTY_ADMIN, TEMPLATES_QUERY_MAX, templateList, type TemplateRow } from "../../spawn/template_list.ts"; |
| 14 | 14 | import { withReadmeSays } from "../../spawn/template_says.ts"; |
| 15 | 15 | import { templateUse, useWords, type TemplateUse } from "../../spawn/template_use.ts"; |
| @@ -34,8 +34,13 @@ function holdsView(id: string, holds: TemplateHolds | undefined, known: boolean) | ||
| 34 | 34 | if (id === DEFAULT_TEMPLATE) return html`<p class="small muted holds">${HOLDS_BLANK}</p>`; |
| 35 | 35 | if (!known) return null; |
| 36 | 36 | if (holds === undefined) return html`<p class="small muted holds">${HOLDS_NONE}</p>`; |
| 37 | const n = holds.files.length + holds.more; | |
| 38 | if (n === 0) return html`<p class="small muted holds">${HOLDS_BLANK}</p>`; | |
| 37 | 39 | return html`<dl class="status-rows small muted holds"> |
| 38 | ${holdsRows(holds).map((r) => html`<div class="status-row"><dt>${r.label}</dt><dd>${r.code ? html`<code>${r.value}</code>` : r.value}</dd></div>`)} | |
| 40 | <div class="status-row"><dt>Files</dt><dd><details data-files><summary>${n} ${n === 1 ? "file" : "files"}</summary> | |
| 41 | <p class="files-list">${holds.files.map((f) => html`<code>${f}</code> `)}${holds.more > 0 ? html`and ${holds.more} more` : null}</p> | |
| 42 | </details></dd></div> | |
| 43 | ${runRows(holds).map((r) => html`<div class="status-row"><dt>${r.label}</dt><dd>${r.code ? html`<code>${r.value}</code>` : r.value}</dd></div>`)} | |
| 39 | 44 | </dl>`; |
| 40 | 45 | } |
| 41 | 46 |
src/web/style/components.ts +3
| @@ -148,6 +148,9 @@ select.input { | ||
| 148 | 148 | .why[open] > summary { border-color: var(--border-strong); color: var(--fg); } |
| 149 | 149 | @media (hover: hover) and (pointer: fine) { .why > summary:hover { border-color: var(--border-strong); color: var(--fg); } } |
| 150 | 150 | .why > summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; } |
| 151 | .holds summary { cursor: pointer; min-height: 24px; } | |
| 152 | .holds summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; } | |
| 153 | .files-list { margin: 4px 0 0; overflow-wrap: anywhere; } | |
| 151 | 154 | .why > p { margin: 6px 0 0; font-size: 12.5px; color: var(--muted-fg); overflow-wrap: anywhere; } |
| 152 | 155 | @media (pointer: coarse) { .why > summary { width: 44px; height: 44px; } } |
| 153 | 156 | .field .error { display: block; font-size: 12.5px; color: var(--danger); margin-top: 6px; font-weight: 500; overflow-wrap: anywhere; } |
test/templates-page.test.ts +13 −7
| @@ -102,6 +102,8 @@ test("the starter's files and run lines are dl rows; blank says it holds none", | ||
| 102 | 102 | const text = await (await fetch(`${servers.url}/templates`)).text(); |
| 103 | 103 | const starter = text.slice(text.indexOf('data-template="starter"')); |
| 104 | 104 | expect(starter).toContain("<dt>Files</dt>"); |
| 105 | expect(starter).toContain("<details data-files>"); | |
| 106 | expect(starter).toContain("<code>forge.json</code>"); | |
| 105 | 107 | expect(starter).toContain("<dt>Run</dt><dd><code>bun run src/server.ts</code></dd>"); |
| 106 | 108 | expect(starter).toContain("<dt>Check</dt><dd><code>bun scripts/egress-check.ts && bun test</code></dd>"); |
| 107 | 109 | expect(text.slice(text.indexOf('data-template="blank"'), text.indexOf('data-template="starter"'))).toContain("Holds no files."); |
| @@ -130,26 +132,30 @@ const row = async (id: string): Promise<string> => { | ||
| 130 | 132 | return text.slice(text.indexOf(`data-template="${id}"`)); |
| 131 | 133 | }; |
| 132 | 134 | |
| 133 | test("a template repository lists at most 12 files, then how many more, with its run and check lines", async () => { | |
| 135 | test("a template repository lists at most 40 files, then how many more, with its run and check lines", async () => { | |
| 134 | 136 | const { topLevel, runLines } = await import("../src/spawn/template_holds.ts"); |
| 135 | const many = Array.from({ length: 15 }, (_, i) => `f${String(i).padStart(2, "0")}.txt`); | |
| 136 | expect(topLevel(many)).toEqual({ files: many.slice(0, 12), more: 3 }); | |
| 137 | const many = Array.from({ length: 45 }, (_, i) => `f${String(i).padStart(2, "0")}.txt`); | |
| 138 | expect(topLevel(many)).toEqual({ files: many.slice(0, 40), more: 5 }); | |
| 137 | 139 | expect(topLevel(["a/b/c", "a/d", "z"])).toEqual({ files: ["a/", "z"], more: 0 }); |
| 138 | 140 | expect(runLines('{"run":"bun x.ts","check":"bun test"}')).toEqual({ run: "bun x.ts", check: "bun test" }); |
| 139 | 141 | expect(runLines("nope")).toEqual({ run: undefined, check: undefined }); |
| 140 | 142 | |
| 141 | const files = Array.from({ length: 14 }, (_, i) => ({ path: `f${String(i).padStart(2, "0")}.txt`, content: "x" })); | |
| 143 | const files = Array.from({ length: 44 }, (_, i) => ({ path: `f${String(i).padStart(2, "0")}.txt`, content: "x" })); | |
| 142 | 144 | const id = await templateRepo("full-kit", [...files, { path: "forge.json", content: JSON.stringify({ app: { run: "bun serve.ts" }, check: { run: "bun test <x>" } }) }]); |
| 143 | 145 | const text = await row(id); |
| 144 | expect(text).toContain("and 3 more"); | |
| 146 | expect(text).toContain("and 5 more"); | |
| 147 | expect(text).toContain("<code>f39.txt</code>"); | |
| 148 | expect(text).not.toContain("f40.txt"); | |
| 149 | expect(text).toContain("45 files"); | |
| 145 | 150 | expect(text).toContain("<dt>Run</dt><dd><code>bun serve.ts</code></dd>"); |
| 146 | 151 | expect(text).toContain("<dt>Check</dt><dd><code>bun test <x></code></dd>"); |
| 147 | 152 | }); |
| 148 | 153 | |
| 149 | 154 | test("a template repository with no forge.json lists its files and no run or check row", async () => { |
| 150 | 155 | const id = await templateRepo("plain-kit", [{ path: "README.md", content: "hi" }]); |
| 151 | const text = (await row(id)).split("</li>")[0]!; | |
| 152 | expect(text).toContain("<dt>Files</dt><dd>README.md</dd>"); | |
| 156 | const text = (await row(id)).split("</dl>")[0]!; | |
| 157 | expect(text).toContain("<code>README.md</code>"); | |
| 158 | expect(text).toContain("1 file"); | |
| 153 | 159 | expect(text).not.toContain("<dt>Run</dt>"); |
| 154 | 160 | expect(text).not.toContain("<dt>Check</dt>"); |
| 155 | 161 | }); |
new above
offline
Builds the full holdsRows list, then drops Files. Principle 7 / one way: share the Files row instead of build-and-filter.