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
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15263-a-template-s-own-page-shows-its-first-as → main 0 commits fork
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 | ||
| 10 | 10 | - **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. |
| 11 | 11 | - **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`). |
| 12 | 12 | - **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. | |
| 14 | 14 | - 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 @@ | ||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | import type { App } from "../app.ts"; |
| 8 | import { readFileAt } from "../apps/checkout.ts"; | |
| 9 | import { firstAskOf } from "../apps/first_ask.ts"; | |
| 8 | 10 | import { blobForRender, listTree, refTip } from "../git/read.ts"; |
| 9 | 11 | import { repoDir } from "../git/repos.ts"; |
| 10 | 12 | import { repoTemplateRef, TEMPLATES } from "../spawn/templates.ts"; |
| @@ -20,6 +22,8 @@ export type TemplateView = { | ||
| 20 | 22 | readonly about: string; |
| 21 | 23 | /** Top-level names; a folder ends in `/`. Empty for a built-in template. */ |
| 22 | 24 | readonly files: readonly string[]; |
| 25 | /** The valid `firstAsk` its forge.json declares, if any. */ | |
| 26 | readonly firstAsk?: string; | |
| 23 | 27 | }; |
| 24 | 28 | |
| 25 | 29 | /** 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 | ||
| 33 | 37 | const dir = repoDir(app.reposRoot, repo.ownerName, repo.name); |
| 34 | 38 | let files: string[] = []; |
| 35 | 39 | let about = ""; |
| 40 | let firstAsk: string | undefined; | |
| 36 | 41 | try { |
| 37 | 42 | const tip = await refTip(dir, `refs/heads/${repo.defaultBranch}`); |
| 38 | 43 | if (tip !== undefined) { |
| @@ -40,9 +45,10 @@ export async function templateView(app: App, viewer: User | undefined, ref: stri | ||
| 40 | 45 | if (tree.ok) files = tree.value.slice(0, FILES_MAX).map((e) => (e.type === "tree" ? `${e.name}/` : e.name)); |
| 41 | 46 | const blob = await blobForRender(dir, tip, "README.md", README_BYTES); |
| 42 | 47 | if (blob.kind === "text") about = readmeParagraph(new TextDecoder().decode(blob.body)); |
| 48 | firstAsk = firstAskOf(await readFileAt(dir, tip, "forge.json")); | |
| 43 | 49 | } |
| 44 | 50 | } catch { |
| 45 | 51 | // an unreadable repository shows what it can: the name and nothing else |
| 46 | 52 | } |
| 47 | return { title: `${repo.ownerName}/${repo.name}`, about, files }; | |
| 53 | return { title: `${repo.ownerName}/${repo.name}`, about, files, ...(firstAsk === undefined ? {} : { firstAsk }) }; | |
| 48 | 54 | } |
src/tui/screens/template.ts +6
| @@ -10,6 +10,7 @@ import { updateRepoSettings } from "../../actions/repos.ts"; | ||
| 10 | 10 | import { templateTimingLine } from "../../actions/template_timing.ts"; |
| 11 | 11 | import { templateProblem } from "../../apps/template_problem.ts"; |
| 12 | 12 | import { canAdmin, getRepoById, type Repo } from "../../core/repos.ts"; |
| 13 | import { templateFirstAsk } from "../../spawn/template_first_ask.ts"; | |
| 13 | 14 | import { templateSaid } from "../../core/template_words.ts"; |
| 14 | 15 | import type { Ctx, Screen } from "../app/app.ts"; |
| 15 | 16 | import { binding } from "../app/keys.ts"; |
| @@ -25,10 +26,13 @@ export const mayMarkTemplate = (f: Session, repo: Repo): boolean => f.user !== u | ||
| 25 | 26 | export function templateScreen(f: Session, ctx: Ctx, repo: Repo): Screen { |
| 26 | 27 | const now = (): Repo => getRepoById(f.db, repo.id) ?? repo; |
| 27 | 28 | let flag: string | undefined; |
| 29 | let ask: string | undefined; | |
| 28 | 30 | const look = async (signal: AbortSignal): Promise<void> => { |
| 29 | 31 | 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; | |
| 30 | 33 | if (signal.aborted) return; |
| 31 | 34 | flag = problem; |
| 35 | ask = declared; | |
| 32 | 36 | ctx.invalidate(); |
| 33 | 37 | }; |
| 34 | 38 | return { |
| @@ -50,6 +54,8 @@ export function templateScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 50 | 54 | let row = area.y + 1; |
| 51 | 55 | if (on && flag !== undefined && row < area.y + area.h) |
| 52 | 56 | 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); | |
| 53 | 59 | const took = templateTimingLine(f.db, `${repo.ownerName}/${repo.name}`); |
| 54 | 60 | 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); |
| 55 | 61 | }, |
src/web/pages/template.ts +1
| @@ -42,6 +42,7 @@ export async function templatePage(ctx: Ctx): Promise<Response> { | ||
| 42 | 42 | ...(shared !== undefined ? { ogImage: `/og/template/${templateCardRef(ref)}.png` } : {}), |
| 43 | 43 | body: html`${pageHead(view.title, { sub: view.about !== "" ? view.about : "The files a forge starts with." })} |
| 44 | 44 | <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>`} | |
| 45 | 46 | ${row ? html`<p>${btn("Start a forge", { variant: "primary", href: `/forges/new?template=${encodeURIComponent(ref)}` })}</p>` : null} |
| 46 | 47 | ${built ? null : html`<p>A forge started from this template begins with these files.</p>`} |
| 47 | 48 | ${built |
test/template-forge-json.test.ts +13
| @@ -197,6 +197,19 @@ describe("every door names the field", () => { | ||
| 197 | 197 | flagged.stop(); |
| 198 | 198 | }); |
| 199 | 199 | |
| 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 | ||
| 200 | 213 | test("a screen that is gone ignores a late look", async () => { |
| 201 | 214 | const m = await make("tuigone", '{"app":{"run":"bun main.ts"}}'); |
| 202 | 215 | expect((await updateRepoSettings(app, admin, m, { template: true })).ok).toBe(true); |
test/template-page.test.ts +20 −1
| @@ -39,6 +39,9 @@ beforeAll(async () => { | ||
| 39 | 39 | }; |
| 40 | 40 | const owner = await mk("giver", true); |
| 41 | 41 | 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 | }; | |
| 42 | 45 | const make = async (name: string, visibility: "public" | "private", template: boolean) => { |
| 43 | 46 | const repo = await createRepo(app, owner, { name, visibility, description: "" }); |
| 44 | 47 | if (!repo.ok) throw new Error(repo.error); |
| @@ -46,7 +49,7 @@ beforeAll(async () => { | ||
| 46 | 49 | message: "t", |
| 47 | 50 | files: [ |
| 48 | 51 | { 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"}}' }, | |
| 50 | 53 | { path: "server.ts", content: "x" }, |
| 51 | 54 | { path: "src/a.ts", content: "x" }, |
| 52 | 55 | ], |
| @@ -58,6 +61,13 @@ beforeAll(async () => { | ||
| 58 | 61 | } |
| 59 | 62 | }; |
| 60 | 63 | 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)); | |
| 61 | 71 | await make("secret", "private", true); |
| 62 | 72 | await make("plain", "public", false); |
| 63 | 73 | for (const who of ["stranger", "giver"]) { |
| @@ -119,3 +129,12 @@ test("readmeParagraph skips the heading and fences and stops at the blank line", | ||
| 119 | 129 | expect(readmeParagraph("# T\n\n```\ncode\n```\n\nOne *two*\nthree.\n\nfour")).toBe("One two three."); |
| 120 | 130 | expect(readmeParagraph("")).toBe(""); |
| 121 | 131 | }); |
| 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 <b>weekly</b> 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 | }); |
new above
offline