@brimsites/brimjs 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (238) hide show
  1. package/LICENSE +110 -0
  2. package/README.md +22 -2
  3. package/bin/brim.js +5 -0
  4. package/dist/cli.js +3802 -0
  5. package/editor-dist/editor.css +1 -0
  6. package/editor-dist/editor.js +91 -0
  7. package/package.json +71 -3
  8. package/skills/brim-convert/SKILL.md +256 -0
  9. package/skills/brim-hosting/SKILL.md +221 -0
  10. package/src/astro-shim.d.ts +5 -0
  11. package/src/auth/accounts.ts +341 -0
  12. package/src/auth/password.ts +34 -0
  13. package/src/auth/permissions.ts +44 -0
  14. package/src/blocks/accordion/Accordion.astro +48 -0
  15. package/src/blocks/accordion/accordion.css +63 -0
  16. package/src/blocks/analytics/Analytics.astro +45 -0
  17. package/src/blocks/analytics/ConsentBanner.astro +41 -0
  18. package/src/blocks/analytics/analytics.client.ts +125 -0
  19. package/src/blocks/analytics/analytics.css +51 -0
  20. package/src/blocks/analytics/consent.ts +51 -0
  21. package/src/blocks/button/Button.astro +64 -0
  22. package/src/blocks/button/button.css +100 -0
  23. package/src/blocks/carousel/Carousel.astro +140 -0
  24. package/src/blocks/carousel/carousel.client.ts +153 -0
  25. package/src/blocks/carousel/carousel.css +181 -0
  26. package/src/blocks/data/icons.ts +285 -0
  27. package/src/blocks/data/image-shapes.ts +91 -0
  28. package/src/blocks/data/social-icons.ts +41 -0
  29. package/src/blocks/data/social-networks.ts +256 -0
  30. package/src/blocks/divider/Divider.astro +27 -0
  31. package/src/blocks/divider/divider.css +17 -0
  32. package/src/blocks/form/Captcha.astro +27 -0
  33. package/src/blocks/form/Form.astro +250 -0
  34. package/src/blocks/form/FormField.astro +138 -0
  35. package/src/blocks/form/Newsletter.astro +80 -0
  36. package/src/blocks/form/form.client.ts +339 -0
  37. package/src/blocks/form/form.css +271 -0
  38. package/src/blocks/gallery/ImageGallery.astro +126 -0
  39. package/src/blocks/gallery/gallery.css +43 -0
  40. package/src/blocks/gallery/lightbox.client.ts +178 -0
  41. package/src/blocks/gallery/lightbox.css +134 -0
  42. package/src/blocks/icon/Icon.astro +41 -0
  43. package/src/blocks/icon/icon.css +16 -0
  44. package/src/blocks/image/Image.astro +158 -0
  45. package/src/blocks/image/image.css +42 -0
  46. package/src/blocks/images.ts +64 -0
  47. package/src/blocks/in-editor.client.ts +8 -0
  48. package/src/blocks/listing/Listing.astro +288 -0
  49. package/src/blocks/listing/ListingCards.astro +48 -0
  50. package/src/blocks/listing/listing.css +136 -0
  51. package/src/blocks/listing/query.ts +363 -0
  52. package/src/blocks/map/Map.astro +75 -0
  53. package/src/blocks/map/map.css +32 -0
  54. package/src/blocks/schemas/accordion.ts +118 -0
  55. package/src/blocks/schemas/button.ts +78 -0
  56. package/src/blocks/schemas/carousel.ts +133 -0
  57. package/src/blocks/schemas/divider.ts +29 -0
  58. package/src/blocks/schemas/form-field-types.ts +29 -0
  59. package/src/blocks/schemas/form.ts +350 -0
  60. package/src/blocks/schemas/icon.ts +24 -0
  61. package/src/blocks/schemas/image-gallery.ts +54 -0
  62. package/src/blocks/schemas/image.ts +87 -0
  63. package/src/blocks/schemas/index.ts +33 -0
  64. package/src/blocks/schemas/listing.ts +14 -0
  65. package/src/blocks/schemas/map.ts +81 -0
  66. package/src/blocks/schemas/newsletter.ts +324 -0
  67. package/src/blocks/schemas/social-media.ts +348 -0
  68. package/src/blocks/schemas/text.ts +151 -0
  69. package/src/blocks/schemas/video.ts +57 -0
  70. package/src/blocks/search/Search.astro +147 -0
  71. package/src/blocks/search/search.css +69 -0
  72. package/src/blocks/search/state.ts +28 -0
  73. package/src/blocks/settings.ts +94 -0
  74. package/src/blocks/social/SocialMedia.astro +102 -0
  75. package/src/blocks/social/social-links.ts +16 -0
  76. package/src/blocks/social/social.css +31 -0
  77. package/src/blocks/theme.ts +35 -0
  78. package/src/blocks/types.ts +52 -0
  79. package/src/blocks/video/Video.astro +90 -0
  80. package/src/blocks/video/video-source.ts +77 -0
  81. package/src/blocks/video/video.client.ts +43 -0
  82. package/src/blocks/video/video.css +29 -0
  83. package/src/check.ts +1119 -0
  84. package/src/cli.ts +493 -0
  85. package/src/components/Blocks.astro +68 -0
  86. package/src/components/Field.astro +61 -0
  87. package/src/components/Footer.astro +36 -0
  88. package/src/components/Link.astro +18 -0
  89. package/src/components/MobileMenu.astro +77 -0
  90. package/src/components/Navbar.astro +42 -0
  91. package/src/components/RichText.astro +32 -0
  92. package/src/components/Seo.astro +202 -0
  93. package/src/components/Setting.astro +46 -0
  94. package/src/components/Shared.astro +17 -0
  95. package/src/components/Text.astro +91 -0
  96. package/src/components/blocks.css +86 -0
  97. package/src/components/index.ts +27 -0
  98. package/src/components/mobile-menu.client.ts +21 -0
  99. package/src/components/mobile-menu.css +52 -0
  100. package/src/components/text.css +48 -0
  101. package/src/config.ts +413 -0
  102. package/src/content/block-layout.ts +63 -0
  103. package/src/content/block-values.ts +48 -0
  104. package/src/content/content-check.ts +174 -0
  105. package/src/content/content-types.ts +408 -0
  106. package/src/content/drafts.ts +115 -0
  107. package/src/content/feed.ts +237 -0
  108. package/src/content/field-types.ts +107 -0
  109. package/src/content/history.ts +140 -0
  110. package/src/content/ids.ts +50 -0
  111. package/src/content/image-size.ts +212 -0
  112. package/src/content/image-sizes.ts +126 -0
  113. package/src/content/item-fields.ts +110 -0
  114. package/src/content/item-head.ts +126 -0
  115. package/src/content/item-image.ts +26 -0
  116. package/src/content/item-path.ts +122 -0
  117. package/src/content/items.ts +580 -0
  118. package/src/content/language-scopes.ts +40 -0
  119. package/src/content/media.ts +271 -0
  120. package/src/content/menus.ts +51 -0
  121. package/src/content/page-templates.ts +52 -0
  122. package/src/content/redirects.ts +112 -0
  123. package/src/content/references.ts +165 -0
  124. package/src/content/rich-text.ts +63 -0
  125. package/src/content/search.ts +167 -0
  126. package/src/content/seed.ts +229 -0
  127. package/src/content/site-settings.ts +111 -0
  128. package/src/content/sitemap.ts +143 -0
  129. package/src/content/store.ts +225 -0
  130. package/src/content/tags.ts +67 -0
  131. package/src/content/text-default.ts +139 -0
  132. package/src/content/types.ts +137 -0
  133. package/src/content/validate.ts +253 -0
  134. package/src/content/validation-error.ts +2 -0
  135. package/src/db/cached.ts +140 -0
  136. package/src/db/database.ts +15 -0
  137. package/src/db/libsql.ts +29 -0
  138. package/src/db/schema.ts +189 -0
  139. package/src/db/url.ts +44 -0
  140. package/src/editor/client/BlockSettings.tsx +1063 -0
  141. package/src/editor/client/Chrome.tsx +284 -0
  142. package/src/editor/client/ContentBrowser.tsx +1299 -0
  143. package/src/editor/client/Editor.tsx +1756 -0
  144. package/src/editor/client/HistoryPanel.tsx +114 -0
  145. package/src/editor/client/ImagesWidget.tsx +113 -0
  146. package/src/editor/client/Inspector.tsx +127 -0
  147. package/src/editor/client/ItemForm.tsx +535 -0
  148. package/src/editor/client/LinkWidget.tsx +440 -0
  149. package/src/editor/client/ListingPanel.tsx +99 -0
  150. package/src/editor/client/Login.tsx +338 -0
  151. package/src/editor/client/Menu.tsx +64 -0
  152. package/src/editor/client/PageSettings.tsx +270 -0
  153. package/src/editor/client/RedirectsSettings.tsx +130 -0
  154. package/src/editor/client/Resize.tsx +204 -0
  155. package/src/editor/client/Sidebar.tsx +51 -0
  156. package/src/editor/client/SiteMap.tsx +568 -0
  157. package/src/editor/client/SiteSettings.tsx +409 -0
  158. package/src/editor/client/TextTools.tsx +391 -0
  159. package/src/editor/client/Toolbar.tsx +449 -0
  160. package/src/editor/client/VideoUpload.tsx +251 -0
  161. package/src/editor/client/api.ts +417 -0
  162. package/src/editor/client/brand.ts +20 -0
  163. package/src/editor/client/connection.ts +98 -0
  164. package/src/editor/client/context.ts +19 -0
  165. package/src/editor/client/css.d.ts +2 -0
  166. package/src/editor/client/editor.css +4656 -0
  167. package/src/editor/client/fields.tsx +812 -0
  168. package/src/editor/client/icons.tsx +174 -0
  169. package/src/editor/client/keys.ts +15 -0
  170. package/src/editor/client/kinds.ts +289 -0
  171. package/src/editor/client/languages.ts +32 -0
  172. package/src/editor/client/main.tsx +100 -0
  173. package/src/editor/client/responses.ts +26 -0
  174. package/src/editor/client/rich-text.ts +326 -0
  175. package/src/editor/client/site-tree.ts +239 -0
  176. package/src/editor/client/theme.ts +55 -0
  177. package/src/editor/client/tsconfig.json +18 -0
  178. package/src/editor/client/tus.ts +137 -0
  179. package/src/editor/client/use-rect.ts +50 -0
  180. package/src/editor/client/widgets.tsx +1218 -0
  181. package/src/forms/captcha.ts +83 -0
  182. package/src/forms/define.ts +166 -0
  183. package/src/forms/fields.ts +197 -0
  184. package/src/forms/file-types.ts +108 -0
  185. package/src/forms/files.ts +216 -0
  186. package/src/forms/form-page.ts +9 -0
  187. package/src/forms/handler.ts +622 -0
  188. package/src/forms/spec.ts +89 -0
  189. package/src/forms/store.ts +96 -0
  190. package/src/forms/work.ts +50 -0
  191. package/src/icons/library.ts +144 -0
  192. package/src/icons/load.ts +63 -0
  193. package/src/index.ts +99 -0
  194. package/src/init.ts +257 -0
  195. package/src/integration.ts +469 -0
  196. package/src/mail/mailer.ts +197 -0
  197. package/src/mail/notify.ts +272 -0
  198. package/src/mail/settings.ts +173 -0
  199. package/src/mail/templates.ts +191 -0
  200. package/src/mcp/page-nodes.ts +209 -0
  201. package/src/mcp/server.ts +111 -0
  202. package/src/mcp/tools.ts +637 -0
  203. package/src/runtime/TemplatePage.astro +39 -0
  204. package/src/runtime/TemplatePageStatic.astro +13 -0
  205. package/src/runtime/admin-route.ts +80 -0
  206. package/src/runtime/connect.ts +53 -0
  207. package/src/runtime/content.ts +188 -0
  208. package/src/runtime/context.ts +131 -0
  209. package/src/runtime/form-files.ts +18 -0
  210. package/src/runtime/forms-route.ts +33 -0
  211. package/src/runtime/forms.ts +28 -0
  212. package/src/runtime/icons.ts +5 -0
  213. package/src/runtime/media.ts +175 -0
  214. package/src/runtime/middleware.ts +186 -0
  215. package/src/runtime/nav.ts +13 -0
  216. package/src/runtime/query-reads.ts +49 -0
  217. package/src/runtime/query-store.ts +16 -0
  218. package/src/runtime/request-state.ts +69 -0
  219. package/src/runtime/robots.ts +29 -0
  220. package/src/runtime/rss-static.ts +16 -0
  221. package/src/runtime/rss.ts +29 -0
  222. package/src/runtime/server.ts +29 -0
  223. package/src/runtime/sitemap.ts +34 -0
  224. package/src/runtime/template-paths.ts +34 -0
  225. package/src/runtime/uploads-route.ts +13 -0
  226. package/src/seed-cli.ts +253 -0
  227. package/src/server/auth.ts +31 -0
  228. package/src/server/fetch-image.ts +219 -0
  229. package/src/server/handler.ts +2106 -0
  230. package/src/server/http.ts +162 -0
  231. package/src/server/locales.ts +73 -0
  232. package/src/server/node.ts +57 -0
  233. package/src/server/query-params.ts +249 -0
  234. package/src/server/rate-limit.ts +72 -0
  235. package/src/server/video-host.ts +162 -0
  236. package/src/starter.ts +70 -0
  237. package/src/version.ts +4 -0
  238. package/src/virtual.d.ts +65 -0
package/src/init.ts ADDED
@@ -0,0 +1,257 @@
1
+ import { spawnSync } from "node:child_process";
2
+ import { existsSync } from "node:fs";
3
+ import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";
4
+ import { dirname, join, relative } from "node:path";
5
+ import { fileURLToPath } from "node:url";
6
+ import { insideGit } from "./check.ts";
7
+ import { VERSION } from "./version.ts";
8
+
9
+ /** The guide an AI coding agent follows to convert the site; shipped with the package. */
10
+ const SKILL = new URL("../skills/brim-convert/SKILL.md", import.meta.url);
11
+ const CONFIG_FILES = ["astro.config.mjs", "astro.config.ts", "astro.config.js", "astro.config.mts"];
12
+ const IGNORED = [
13
+ "# Brim.js: the local database (content, users, form responses) and images uploaded in the editor.",
14
+ ".brim",
15
+ "public/uploads",
16
+ ];
17
+ /** The name Brim.js is published and imported under. */
18
+ const PACKAGE = "@brimsites/brimjs";
19
+ const AGENTS_MARK = "<!-- brimjs -->";
20
+ const AGENTS_NOTE = `${AGENTS_MARK}
21
+ ## Brim.js
22
+
23
+ This Astro site is editable with Brim.js: texts, images, links and navigation wrapped in Brim's components carry a
24
+ stable \`nodeId\`, and the owner's edits (stored in SQLite) replace what the code says. Never rename or reuse a
25
+ \`nodeId\`. To make more of the site editable, follow \`.claude/skills/brim-convert/SKILL.md\`; the components and
26
+ their props are documented in \`node_modules/@brimsites/brimjs/README.md\`. While the dev server runs, the site's MCP is at
27
+ \`/admin/mcp\` (token: \`npx brim token add <email>\`): use it to read and change content instead of the database.
28
+ To show the person the editor, run \`npx brim open\` with the dev server running (or open \`/admin/edit\`).
29
+ Never write a password or a token into a file of the project; to let someone sign in, use \`npx brim user link <email>\`.
30
+ Facts about the whole site (email, phone, WhatsApp, social profiles, address) are site settings, declared in
31
+ \`src/site-settings.ts\` and read with \`getSiteSettings()\` or \`<Setting name="…" />\`: never constants in the code.
32
+ The body of an item with a page (article, project, service) is a \`blocks\` field drawn with \`<Blocks>\`, so the
33
+ owner adds images, galleries and buttons between paragraphs; \`richtext\` is for text that stays text (\`// text only\`).
34
+ The emails the site sends (a form message's) are written in \`src/emails.ts\` with \`defineEmails\` and \`html\` from
35
+ \`brimjs\`; see one at \`/admin/api/emails/form\`.
36
+ Pictures: the site's own (what the design starts with: hero, sections, a gallery it ships with, logo, icons) are
37
+ files in \`public/\`, at most 2560 px wide; Brim sends smaller copies of them and the owner replaces them in the editor.
38
+ Pictures of items (products, projects, articles) and anything filled in bulk are uploads (MCP \`brim_upload_image\`
39
+ or \`npx brim media add <files…>\`) saved in the item's image field; never write an \`/uploads/…\` address in the code.
40
+ What the site is filled from (photos, catalogues, data for an import) stays out of git: keep it in \`import/\` or
41
+ outside the project; the live site gets the uploads with the content (\`brimsites restore\`), never with a deploy.
42
+ To deploy, host or move the site (Brimsites first, or a server of its own), follow
43
+ \`node_modules/@brimsites/brimjs/skills/brim-hosting/SKILL.md\`; a deploy never carries the local \`.brim/data.db\`.
44
+ Working on the site once it is online, ask once whether to download its live content first (\`brimsites export\`,
45
+ local \`.brim\` moved outside the project first), so you work on the site as it is now.
46
+ After the first deploy, offer once to save the live site's MCP (\`brimsites tokens create <site>\`, then
47
+ \`claude mcp add\`), so its content can be changed without the editor; the token never goes into a file.
48
+ `;
49
+
50
+ export interface InitOptions {
51
+ /** The @brimsites/brimjs dependency to write: a version range, or anything else a package manager accepts. */
52
+ brim?: string | undefined;
53
+ /** `false` leaves a project that is not in git out of it. */
54
+ git?: boolean;
55
+ /** The site's language (`ro`, `en`…); by default the `lang` its layout writes, else `en`. */
56
+ lang?: string | undefined;
57
+ }
58
+
59
+ export interface InitResult {
60
+ /** What was done, in words, one line each. */
61
+ done: string[];
62
+ /** What could not be done automatically and is left to the person (or their agent). */
63
+ manual: string[];
64
+ }
65
+
66
+ /**
67
+ * Sets up Brim.js in an Astro project that already exists: the dependency, the integration, what git should
68
+ * ignore, and the conversion guide for AI coding agents. Running it again changes nothing that is already there.
69
+ */
70
+ export async function init(directory: string, options: InitOptions = {}): Promise<InitResult> {
71
+ const result: InitResult = { done: [], manual: [] };
72
+ const packagePath = join(directory, "package.json");
73
+ if (!existsSync(packagePath)) {
74
+ throw new Error("There is no package.json here. Run this in the folder of an Astro project.");
75
+ }
76
+ const packageSource = await readFile(packagePath, "utf8");
77
+ const manifest = JSON.parse(packageSource) as {
78
+ dependencies?: Record<string, string>;
79
+ devDependencies?: Record<string, string>;
80
+ scripts?: Record<string, string>;
81
+ };
82
+ if (!manifest.dependencies?.astro && !manifest.devDependencies?.astro) {
83
+ throw new Error(
84
+ "This project does not depend on astro. Brim.js makes Astro sites editable; start a new one with `npm create brim`.",
85
+ );
86
+ }
87
+
88
+ let changed = false;
89
+ if (!manifest.dependencies?.[PACKAGE] && !manifest.devDependencies?.[PACKAGE]) {
90
+ manifest.dependencies = { ...manifest.dependencies, [PACKAGE]: options.brim ?? `^${VERSION}` };
91
+ changed = true;
92
+ result.done.push(`Added ${PACKAGE} ${manifest.dependencies[PACKAGE]} to package.json.`);
93
+ }
94
+ for (const [name, command] of Object.entries({ start: "brim start", brim: "brim" })) {
95
+ if (manifest.scripts?.[name] === undefined) {
96
+ manifest.scripts = { ...manifest.scripts, [name]: command };
97
+ changed = true;
98
+ result.done.push(`Added the "${name}" script (${command}).`);
99
+ }
100
+ }
101
+ if (changed) {
102
+ const indent = /^(\s+)"/m.exec(packageSource)?.[1] ?? " ";
103
+ await writeFile(packagePath, `${JSON.stringify(manifest, null, indent)}\n`);
104
+ }
105
+
106
+ const lang = options.lang ?? (await layoutLanguage(directory)) ?? "en";
107
+ if (!LANGUAGE.test(lang)) throw new Error(`"${lang}" is not a language code, e.g. ro, en or pt-BR.`);
108
+ const configName = CONFIG_FILES.find((name) => existsSync(join(directory, name)));
109
+ if (!configName) {
110
+ await writeFile(
111
+ join(directory, "astro.config.mjs"),
112
+ `import { defineConfig } from "astro/config";\nimport brim from "@brimsites/brimjs";\n\nexport default defineConfig({\n ${languageLine(lang)}\n integrations: [brim()],\n});\n`,
113
+ );
114
+ result.done.push(`Created astro.config.mjs with the brim() integration and the site's language (${lang}).`);
115
+ } else {
116
+ const source = await readFile(join(directory, configName), "utf8");
117
+ const integrated = addIntegration(source);
118
+ const patched = integrated === undefined ? undefined : addLanguage(integrated, lang);
119
+ if (patched === undefined) {
120
+ result.manual.push(
121
+ `Add Brim to ${configName} yourself: \`import brim from "@brimsites/brimjs";\` and \`brim()\` in \`integrations\`.`,
122
+ );
123
+ } else if (patched !== source) {
124
+ await writeFile(join(directory, configName), patched);
125
+ result.done.push(`Added the brim() integration to ${configName}.`);
126
+ if (patched !== integrated) result.done.push(`Declared the site's language (${lang}) in ${configName}.`);
127
+ }
128
+ if (/\badapter\s*:/.test(source)) {
129
+ result.manual.push(
130
+ `${configName} sets an adapter. Brim keeps it, but its editor, forms and MCP need a Node server: see "Deploy" in the brimjs README.`,
131
+ );
132
+ }
133
+ }
134
+
135
+ const ignorePath = join(directory, ".gitignore");
136
+ const ignore = existsSync(ignorePath) ? await readFile(ignorePath, "utf8") : "";
137
+ const lines = ignore.split(/\r?\n/).map((line) => line.trim().replace(/^\/|\/$/g, ""));
138
+ const missing = IGNORED.filter((line) => !line.startsWith("#") && !lines.includes(line));
139
+ if (missing.length > 0) {
140
+ const block = [IGNORED[0], ...missing].join("\n");
141
+ await writeFile(ignorePath, `${ignore}${ignore === "" || ignore.endsWith("\n") ? "" : "\n"}${block}\n`);
142
+ result.done.push(`Added ${missing.join(" and ")} to .gitignore.`);
143
+ }
144
+
145
+ const skillPath = join(directory, ".claude", "skills", "brim-convert", "SKILL.md");
146
+ const skill = await readFile(fileURLToPath(SKILL), "utf8");
147
+ if (!existsSync(skillPath) || (await readFile(skillPath, "utf8")) !== skill) {
148
+ await mkdir(dirname(skillPath), { recursive: true });
149
+ await writeFile(skillPath, skill);
150
+ result.done.push(`Wrote the conversion guide for AI agents to ${relative(directory, skillPath)}.`);
151
+ }
152
+
153
+ const agentsPath = join(directory, "AGENTS.md");
154
+ const agents = existsSync(agentsPath) ? await readFile(agentsPath, "utf8") : "";
155
+ if (!agents.includes(AGENTS_MARK)) {
156
+ await writeFile(
157
+ agentsPath,
158
+ `${agents}${agents === "" || agents.endsWith("\n\n") ? "" : agents.endsWith("\n") ? "\n" : "\n\n"}${AGENTS_NOTE}`,
159
+ );
160
+ result.done.push(`${agents === "" ? "Created" : "Added a Brim.js section to"} AGENTS.md.`);
161
+ }
162
+ // Claude Code reads CLAUDE.md; one that does not exist yet simply points at AGENTS.md.
163
+ if (!existsSync(join(directory, "CLAUDE.md"))) {
164
+ await writeFile(join(directory, "CLAUDE.md"), "@AGENTS.md\n");
165
+ result.done.push("Created CLAUDE.md, pointing at AGENTS.md.");
166
+ }
167
+
168
+ // Every change an agent or the owner makes to the code should be seen and taken back: the site goes into git.
169
+ // Nothing is committed: the project may hold files that must not be, which only its owner knows.
170
+ if (options.git !== false && !insideGit(directory)) {
171
+ const made = spawnSync("git", ["init", "--quiet"], { cwd: directory, stdio: "ignore" });
172
+ if (made.status === 0) {
173
+ result.done.push("Put the project in git (git init). Commit once you have checked what it holds.");
174
+ } else {
175
+ result.manual.push("Put the project in git (git init), so every change can be seen and taken back.");
176
+ }
177
+ }
178
+ return result;
179
+ }
180
+
181
+ /**
182
+ * An Astro config with Brim's integration added. The same text when it is already there; `undefined` when the
183
+ * config is written in a way this cannot safely change.
184
+ */
185
+ export function addIntegration(source: string): string | undefined {
186
+ if (/from\s+["']@brimsites\/brimjs["']/.test(source)) return source;
187
+ const list = /\bintegrations\s*:\s*\[/.exec(source);
188
+ const config = /\bdefineConfig\s*\(\s*\{|export\s+default\s*\{/.exec(source);
189
+ let patched: string;
190
+ if (list) {
191
+ const at = list.index + list[0].length;
192
+ const rest = source.slice(at);
193
+ const space = /^\s*/.exec(rest)?.[0] ?? "";
194
+ // An empty list, a list over several lines, or one written on a single line.
195
+ const added = rest.trimStart().startsWith("]") ? "brim()" : space.includes("\n") ? `${space}brim(),` : "brim(), ";
196
+ patched = source.slice(0, at) + added + rest;
197
+ } else if (config) {
198
+ const at = config.index + config[0].length;
199
+ patched = `${source.slice(0, at)}\n integrations: [brim()],${source.slice(at)}`;
200
+ } else {
201
+ return undefined;
202
+ }
203
+ return `import brim from "@brimsites/brimjs";\n${patched}`;
204
+ }
205
+
206
+ /** A language code as Astro takes it: `ro`, `en`, `pt-BR`. */
207
+ const LANGUAGE = /^[a-z]{2,3}(-[A-Za-z]{2,4})?$/;
208
+
209
+ /**
210
+ * The line that declares a site's one language, its default. Declared from the start, the site keeps what its owner
211
+ * saves (navbar, footer, settings, templates) under that language, so adding a second one later moves nothing.
212
+ */
213
+ export function languageLine(lang: string): string {
214
+ return `i18n: { defaultLocale: ${JSON.stringify(lang)}, locales: [${JSON.stringify(lang)}] },`;
215
+ }
216
+
217
+ /** An Astro config with the site's language declared; the same text when it declares languages already. */
218
+ export function addLanguage(source: string, lang: string): string {
219
+ if (/\bi18n\s*:/.test(source)) return source;
220
+ const config = /\bdefineConfig\s*\(\s*\{|export\s+default\s*\{/.exec(source);
221
+ if (!config) return source;
222
+ const at = config.index + config[0].length;
223
+ return `${source.slice(0, at)}\n ${languageLine(lang)}${source.slice(at)}`;
224
+ }
225
+
226
+ /** The language a site's layout already writes in `<html lang="…">`, if one does. */
227
+ async function layoutLanguage(directory: string): Promise<string | undefined> {
228
+ const folder = join(directory, "src", "layouts");
229
+ if (!existsSync(folder)) return undefined;
230
+ for (const name of await readdir(folder)) {
231
+ if (!name.endsWith(".astro")) continue;
232
+ const match = /<html[^>]*\blang=["']([A-Za-z-]+)["']/.exec(await readFile(join(folder, name), "utf8"));
233
+ if (match?.[1] && LANGUAGE.test(match[1])) return match[1];
234
+ }
235
+ return undefined;
236
+ }
237
+
238
+ export function initReport(result: InitResult, runner: string): string {
239
+ const exec = runner === "npm" ? "npx" : runner;
240
+ return [
241
+ ...(result.done.length > 0 ? result.done : ["Brim.js is already set up here; nothing to change."]),
242
+ ...(result.manual.length > 0 ? ["", "Left to you:", ...result.manual.map((line) => ` - ${line}`)] : []),
243
+ "",
244
+ "Next:",
245
+ ` ${runner} install`,
246
+ ` ${exec} brim user add you@example.com # the account that edits the site`,
247
+ ` ${runner} run dev # the editor is at /admin/edit`,
248
+ "",
249
+ "Nothing on the site is editable yet. Ask your AI coding agent to make it so:",
250
+ ' "Convert this site to Brim.js, following the brim-convert skill."',
251
+ "or wrap elements yourself, one at a time:",
252
+ ' <Text nodeId="hero-title" as="h1">Your heading</Text>',
253
+ "",
254
+ "When it is ready, deploy it in one click on Brimsites, the managed hosting for Brim.js:",
255
+ " https://dashboard.brimsites.com",
256
+ ].join("\n");
257
+ }