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

A template's own page shows its first ask, so a client sees what the new forge will open with #15281

Merged opened this

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

Merged

scout/forge:scout/15263-a-template-s-own-page-shows-its-first-as → main 0 commits fork

  • Merged. scout/15263-a-template-s-own-page-shows-its-first-as was merged into main as 5013692.

Review

Sign in to review this change.
6 files changed +48 −3
docs/features/first-ask.md +1 −1
@@ -10,5 +10,5 @@ A template may start a new forge's ask box with a sentence, so the forge opens w
1010 - **Read once:** when a forge is made from a template repository (`template: "owner/name"`), `copyInto` reads `firstAsk` at the template's default branch and keeps it in the new forge's `parent.json` (`template.firstAsk`). Later edits to the template do not change a forge already made.
1111 - **Shown:** on the new forge's home before its first ask, the box holds the sentence, escaped and editable; leading newlines are trimmed. The terminal's "What do you want built?" prompt (`t` on the home) starts with the same text (`readyAsk`).
1212 - **Nothing declared:** the box is empty, as before.
13 - **Not done:** the template's own page does not show its first ask.
13 - **On the template's page:** `/templates/:owner/:name` shows the sentence, escaped, above the start button; nothing when none is declared or the stored value is invalid.
1414 - Code: `src/apps/first_ask.ts`, `src/spawn/template_first_ask.ts`, `readyAsk` in `src/spawn/template_record.ts`. Tests: `test/template-first-ask.test.ts`, `test/tui-first-ask.test.ts`.
src/core/template_view.ts +7 −1
@@ -5,6 +5,8 @@
55 */
66
77 import type { App } from "../app.ts";
8 import { readFileAt } from "../apps/checkout.ts";
9 import { firstAskOf } from "../apps/first_ask.ts";
810 import { blobForRender, listTree, refTip } from "../git/read.ts";
911 import { repoDir } from "../git/repos.ts";
1012 import { repoTemplateRef, TEMPLATES } from "../spawn/templates.ts";
@@ -20,6 +22,8 @@ export type TemplateView = {
2022 readonly about: string;
2123 /** Top-level names; a folder ends in `/`. Empty for a built-in template. */
2224 readonly files: readonly string[];
25 /** The valid `firstAsk` its forge.json declares, if any. */
26 readonly firstAsk?: string;
2327 };
2428
2529 /** A built-in template by id, or a marked repository by `owner/name`. */
@@ -33,6 +37,7 @@ export async function templateView(app: App, viewer: User | undefined, ref: stri
3337 const dir = repoDir(app.reposRoot, repo.ownerName, repo.name);
3438 let files: string[] = [];
3539 let about = "";
40 let firstAsk: string | undefined;
3641 try {
3742 const tip = await refTip(dir, `refs/heads/${repo.defaultBranch}`);
3843 if (tip !== undefined) {
@@ -40,9 +45,10 @@ export async function templateView(app: App, viewer: User | undefined, ref: stri
4045 if (tree.ok) files = tree.value.slice(0, FILES_MAX).map((e) => (e.type === "tree" ? `${e.name}/` : e.name));
4146 const blob = await blobForRender(dir, tip, "README.md", README_BYTES);
4247 if (blob.kind === "text") about = readmeParagraph(new TextDecoder().decode(blob.body));
48 firstAsk = firstAskOf(await readFileAt(dir, tip, "forge.json"));
4349 }
4450 } catch {
4551 // an unreadable repository shows what it can: the name and nothing else
4652 }
47 return { title: `${repo.ownerName}/${repo.name}`, about, files };
53 return { title: `${repo.ownerName}/${repo.name}`, about, files, ...(firstAsk === undefined ? {} : { firstAsk }) };
4854 }
src/tui/screens/template.ts +6
@@ -10,6 +10,7 @@ import { updateRepoSettings } from "../../actions/repos.ts";
1010 import { templateTimingLine } from "../../actions/template_timing.ts";
1111 import { templateProblem } from "../../apps/template_problem.ts";
1212 import { canAdmin, getRepoById, type Repo } from "../../core/repos.ts";
13 import { templateFirstAsk } from "../../spawn/template_first_ask.ts";
1314 import { templateSaid } from "../../core/template_words.ts";
1415 import type { Ctx, Screen } from "../app/app.ts";
1516 import { binding } from "../app/keys.ts";
@@ -25,10 +26,13 @@ export const mayMarkTemplate = (f: Session, repo: Repo): boolean => f.user !== u
2526 export function templateScreen(f: Session, ctx: Ctx, repo: Repo): Screen {
2627 const now = (): Repo => getRepoById(f.db, repo.id) ?? repo;
2728 let flag: string | undefined;
29 let ask: string | undefined;
2830 const look = async (signal: AbortSignal): Promise<void> => {
2931 const problem = now().template ? await templateProblem(f.app, now()) : undefined;
32 const declared = now().template ? await templateFirstAsk(f.app, `${repo.ownerName}/${repo.name}`).catch(() => undefined) : undefined;
3033 if (signal.aborted) return;
3134 flag = problem;
35 ask = declared;
3236 ctx.invalidate();
3337 };
3438 return {
@@ -50,6 +54,8 @@ export function templateScreen(f: Session, ctx: Ctx, repo: Repo): Screen {
5054 let row = area.y + 1;
5155 if (on && flag !== undefined && row < area.y + area.h)
5256 s.text(area.x, row++, truncate(`Flagged: ${flag}`, area.w), style(c.mutedFg, c.bg), area.x + area.w);
57 if (on && ask !== undefined && row < area.y + area.h)
58 s.text(area.x, row++, truncate(`A new forge opens with this ask: ${ask.replace(/\s+/g, " ")}`, area.w), style(c.mutedFg, c.bg), area.x + area.w);
5359 const took = templateTimingLine(f.db, `${repo.ownerName}/${repo.name}`);
5460 if (took !== null && row < area.y + area.h) s.text(area.x, row, truncate(took, area.w), style(c.mutedFg, c.bg), area.x + area.w);
5561 },
src/web/pages/template.ts +1
@@ -42,6 +42,7 @@ export async function templatePage(ctx: Ctx): Promise<Response> {
4242 ...(shared !== undefined ? { ogImage: `/og/template/${templateCardRef(ref)}.png` } : {}),
4343 body: html`${pageHead(view.title, { sub: view.about !== "" ? view.about : "The files a forge starts with." })}
4444 <section>
45 ${view.firstAsk === undefined ? null : html`<p id="template-first-ask"><span class="muted">A new forge opens with this ask:</span> <q>${view.firstAsk}</q></p>`}
4546 ${row ? html`<p>${btn("Start a forge", { variant: "primary", href: `/forges/new?template=${encodeURIComponent(ref)}` })}</p>` : null}
4647 ${built ? null : html`<p>A forge started from this template begins with these files.</p>`}
4748 ${built
test/template-forge-json.test.ts +13
@@ -197,6 +197,19 @@ describe("every door names the field", () => {
197197 flagged.stop();
198198 });
199199
200 test("the terminal's template screen shows the declared first ask, and nothing when none", async () => {
201 const m = await make("tuiask", '{"app":{"run":"bun main.ts"},"firstAsk":"Add a weekly menu page"}');
202 expect((await updateRepoSettings(app, admin, m, { template: true })).ok).toBe(true);
203 const shown = await openTui(fresh(m));
204 expect(shown.text()).toContain("A new forge opens with this ask: Add a weekly menu page");
205 shown.stop();
206 const n = await make("tuinoask", '{"app":{"run":"bun main.ts"}}');
207 expect((await updateRepoSettings(app, admin, n, { template: true })).ok).toBe(true);
208 const none = await openTui(fresh(n));
209 expect(none.text()).not.toContain("opens with this ask");
210 none.stop();
211 });
212
200213 test("a screen that is gone ignores a late look", async () => {
201214 const m = await make("tuigone", '{"app":{"run":"bun main.ts"}}');
202215 expect((await updateRepoSettings(app, admin, m, { template: true })).ok).toBe(true);
test/template-page.test.ts +20 −1
@@ -39,6 +39,9 @@ beforeAll(async () => {
3939 };
4040 const owner = await mk("giver", true);
4141 await mk("stranger");
42 const forgeJsons: Record<string, string> = {
43 asky: JSON.stringify({ app: { run: "bun main.ts" }, firstAsk: "Add a <b>weekly</b> menu page" }),
44 };
4245 const make = async (name: string, visibility: "public" | "private", template: boolean) => {
4346 const repo = await createRepo(app, owner, { name, visibility, description: "" });
4447 if (!repo.ok) throw new Error(repo.error);
@@ -46,7 +49,7 @@ beforeAll(async () => {
4649 message: "t",
4750 files: [
4851 { path: "README.md", content: `# ${name}\n\nA <script>alert(1)</script> starter.\nSecond line.\n\nNot this.\n` },
49 { path: "forge.json", content: '{"app":{"run":"bun main.ts"}}' },
52 { path: "forge.json", content: forgeJsons[name] ?? '{"app":{"run":"bun main.ts"}}' },
5053 { path: "server.ts", content: "x" },
5154 { path: "src/a.ts", content: "x" },
5255 ],
@@ -58,6 +61,13 @@ beforeAll(async () => {
5861 }
5962 };
6063 await make("kit", "public", true);
64 await make("asky", "public", true);
65 await make("badask", "public", true);
66 // marked while valid, then changed to a value the check would refuse: the page ignores it
67 const bad = await commitToBranch(app, owner, getRepo(app.db, "giver", "badask")!, {
68 message: "bad", files: [{ path: "forge.json", content: JSON.stringify({ app: { run: "bun main.ts" }, firstAsk: "x".repeat(281) }) }],
69 });
70 if (!bad.ok) throw new Error(String(bad.error));
6171 await make("secret", "private", true);
6272 await make("plain", "public", false);
6373 for (const who of ["stranger", "giver"]) {
@@ -119,3 +129,12 @@ test("readmeParagraph skips the heading and fences and stops at the blank line",
119129 expect(readmeParagraph("# T\n\n```\ncode\n```\n\nOne *two*\nthree.\n\nfour")).toBe("One two three.");
120130 expect(readmeParagraph("")).toBe("");
121131 });
132
133 test("a template that declares a first ask shows it, escaped; none declared or invalid shows nothing", async () => {
134 const shown = await (await get("/templates/giver/asky")).text();
135 expect(shown).toContain('id="template-first-ask"');
136 expect(shown).toContain("Add a &lt;b&gt;weekly&lt;/b&gt; menu page");
137 expect(shown).not.toContain("<b>weekly");
138 expect(await (await get("/templates/giver/kit")).text()).not.toContain("template-first-ask");
139 expect(await (await get("/templates/giver/badask")).text()).not.toContain("template-first-ask");
140 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs