Skip to content
Myr

render.ts

This file

Path

packages/drafting/src/render.ts

75 lines · 2.4 KiB · TypeScript

Last changed by

Close the code browser and drafting wave: its decisions in two ADRs, every rule it added held, and its leftovers folded inoscabrielbefore adoption009640c

History of this file

Take it

Merge branch 'build/908-drafter-workers-ai'oscabrielbefore adoption004e40e

line 1why, one step back
1/**

Close the code browser and drafting wave: its decisions in two ADRs, every rule it added held, and its leftovers folded inoscabrielbefore adoption009640c

line 2why, one step back
2 * The ask file, written by code from a draft's fields (ADR 0018): the model

Merge branch 'build/908-drafter-workers-ai'oscabrielbefore adoption004e40e

lines 3–17why, one step back
3 * never writes the `.ask.ts` source, so it cannot put code there. Every
4 * string it gave enters the file through `JSON.stringify`, as a string5 * literal, and a field it left out (or gave empty) is left out. Only the6 * test file is the model's own text.7 */8import { Brand } from "@repo/brand";9import type { ConfigFile } from "@repo/protocol/domain/common";10import { RepoPath } from "@repo/protocol/ids";1112import type { DraftedAsk } from "./drafted.ts";1314const text = (value: string) => JSON.stringify(value);1516const list = (values: readonly string[]) => `[${values.map(text).join(", ")}]`;17

Close the code browser and drafting wave: its decisions in two ADRs, every rule it added held, and its leftovers folded inoscabrielbefore adoption009640c

line 18why, one step back
18/** Each drafted kind's handle in the rules package: drafts are `fix` asks only, for now. */

Merge branch 'build/908-drafter-workers-ai'oscabrielbefore adoption004e40e

lines 19–75why, one step back
19const kindHandle = { fix: "kinds.fix" } as const satisfies Record<
20 DraftedAsk["kind"],21 string22>;2324/**25 * The `.ask.ts` source for a draft: the rules import by the CLI package's26 * name, then `kinds.fix.ask({ … })` with its keys sorted.27 */28export const renderAskFile = (drafted: DraftedAsk): string => {29 const fields: readonly (readonly [string, string | undefined])[] = [30 [31 "context",32 drafted.context === undefined || drafted.context.length === 033 ? undefined34 : list(drafted.context),35 ],36 [37 "current",38 drafted.current === undefined ? undefined : text(drafted.current),39 ],40 [41 "outOfScope",42 drafted.outOfScope === undefined || drafted.outOfScope.length === 043 ? undefined44 : list(drafted.outOfScope),45 ],46 ["path", text(drafted.test.path)],47 ["territory", `territory.paths(${drafted.territory.map(text).join(", ")})`],48 ["want", text(drafted.want)],49 ];5051 return [52 `import { kinds, territory } from ${text(`${Brand.cliPackage}/rules`)};`,53 "",54 `export default ${kindHandle[drafted.kind]}.ask({`,55 ...fields.flatMap(([key, value]) =>56 value === undefined ? [] : [` ${key}: ${value},`]57 ),58 "});",59 "",60 ].join("\n");61};6263/**64 * A draft as a proposal's files: its ask file under the config folder's65 * `asks/`, named by its id (what `askIdOf` reads back), then its test.66 */67export const draftedFiles = (68 drafted: DraftedAsk69): readonly [ConfigFile, ConfigFile] => [70 {71 content: renderAskFile(drafted),72 path: RepoPath.make(`${Brand.configDir}/asks/${drafted.id}.ask.ts`),73 },74 { content: drafted.test.source, path: drafted.test.path },75];