Public: anyone can read this; the crew can change it.

A template card lists the files it will create before it is chosen #16822

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/16317-a-template-card-lists-the-files-it-will → main 0 commits fork 3 threads · 3 unresolved

  • Merged. scout/16317-a-template-card-lists-the-files-it-will was merged into main as 2da2af9.

Review

Sign in to review this change.
6 files changed +44 −17
docs/design/areas/templates.md +2
@@ -1,3 +1,5 @@
11 # Templates
22
33 - `.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
55 # What a template holds
66
77 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."
1112
1213 What was read is kept on the running forge, keyed by the template's default-branch tip: a page costs one ref read until
1314 the template changes, then it is read again.
src/spawn/template_holds.ts +15 −5
@@ -1,6 +1,6 @@
11 /**
22 * 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
44 * baked files; a template repository is read at its default branch. What was read is kept on the
55 * app, keyed by the branch tip, so a request costs one ref read until the template changes. A
66 * repository that cannot be read says so.
@@ -13,7 +13,7 @@ import { blobForRender, listTree, refTip } from "../git/read.ts";
1313 import { repoDir } from "../git/repos.ts";
1414 import { DEFAULT_TEMPLATE, repoTemplateRef } from "./templates.ts";
1515
16 export const HOLDS_FILES_MAX = 12;
16 export const HOLDS_FILES_MAX = 40;
1717 export const HOLDS_BLANK = "Holds no files.";
1818 export const HOLDS_NONE = "Contents not available.";
1919
@@ -97,10 +97,20 @@ export async function templateHolds(app: App, id: string): Promise<TemplateHolds
9797 return holds;
9898 }
9999
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 }[] = [];
103108 if (h.run !== undefined) rows.push({ label: "Run", value: h.run, code: true });
104109 if (h.check !== undefined) rows.push({ label: "Check", value: h.check, code: true });
105110 return rows;
106111 }
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
99 import type { Ctx } from "../../http/context.ts";
1010 import { listChildren } from "../../actions/spawn.ts";
1111 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";
1313 import { filterTemplates, TEMPLATES_EMPTY, noRepoTemplates, TEMPLATES_EMPTY_ADMIN, TEMPLATES_QUERY_MAX, templateList, type TemplateRow } from "../../spawn/template_list.ts";
1414 import { withReadmeSays } from "../../spawn/template_says.ts";
1515 import { templateUse, useWords, type TemplateUse } from "../../spawn/template_use.ts";
@@ -34,8 +34,13 @@ function holdsView(id: string, holds: TemplateHolds | undefined, known: boolean)
3434 if (id === DEFAULT_TEMPLATE) return html`<p class="small muted holds">${HOLDS_BLANK}</p>`;
3535 if (!known) return null;
3636 if (holds === undefined) return html`<p class="small muted holds">${HOLDS_NONE}</p>`;
37 const n = holds.files.length + holds.more;
outdated

Builds the full holdsRows list, then drops Files. Principle 7 / one way: share the Files row instead of build-and-filter.

38 if (n === 0) return html`<p class="small muted holds">${HOLDS_BLANK}</p>`;

Non-blocking: this zero-files branch for a template repository has no test. Add one with an empty repo that expects "Holds no files." and no <details>.

3739 return html`<dl class="status-rows small muted holds">
outdated

Zero files gives a '0 entries' fold with nothing in it. Show HOLDS_BLANK instead. Also principle 8: say 'files', not 'entries'.

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>`)}
3944 </dl>`;
4045 }
4146
src/web/style/components.ts +3
@@ -148,6 +148,9 @@ select.input {
148148 .why[open] > summary { border-color: var(--border-strong); color: var(--fg); }
149149 @media (hover: hover) and (pointer: fine) { .why > summary:hover { border-color: var(--border-strong); color: var(--fg); } }
150150 .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; }
151154 .why > p { margin: 6px 0 0; font-size: 12.5px; color: var(--muted-fg); overflow-wrap: anywhere; }
152155 @media (pointer: coarse) { .why > summary { width: 44px; height: 44px; } }
153156 .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",
102102 const text = await (await fetch(`${servers.url}/templates`)).text();
103103 const starter = text.slice(text.indexOf('data-template="starter"'));
104104 expect(starter).toContain("<dt>Files</dt>");
105 expect(starter).toContain("<details data-files>");
106 expect(starter).toContain("<code>forge.json</code>");
105107 expect(starter).toContain("<dt>Run</dt><dd><code>bun run src/server.ts</code></dd>");
106108 expect(starter).toContain("<dt>Check</dt><dd><code>bun scripts/egress-check.ts &amp;&amp; bun test</code></dd>");
107109 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> => {
130132 return text.slice(text.indexOf(`data-template="${id}"`));
131133 };
132134
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 () => {
134136 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 });
137139 expect(topLevel(["a/b/c", "a/d", "z"])).toEqual({ files: ["a/", "z"], more: 0 });
138140 expect(runLines('{"run":"bun x.ts","check":"bun test"}')).toEqual({ run: "bun x.ts", check: "bun test" });
139141 expect(runLines("nope")).toEqual({ run: undefined, check: undefined });
140142
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" }));
142144 const id = await templateRepo("full-kit", [...files, { path: "forge.json", content: JSON.stringify({ app: { run: "bun serve.ts" }, check: { run: "bun test <x>" } }) }]);
143145 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");
145150 expect(text).toContain("<dt>Run</dt><dd><code>bun serve.ts</code></dd>");
146151 expect(text).toContain("<dt>Check</dt><dd><code>bun test &lt;x&gt;</code></dd>");
147152 });
148153
149154 test("a template repository with no forge.json lists its files and no run or check row", async () => {
150155 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");
153159 expect(text).not.toContain("<dt>Run</dt>");
154160 expect(text).not.toContain("<dt>Check</dt>");
155161 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs