@react-consulting/rc-kit 0.0.0-stage → 0.13.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 (337) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +94 -2
  3. package/devkit/README.md +301 -0
  4. package/devkit/cli.test.ts +211 -0
  5. package/devkit/cli.ts +120 -0
  6. package/devkit/commands/build.ts +127 -0
  7. package/devkit/commands/checks.ts +123 -0
  8. package/devkit/commands/context.ts +47 -0
  9. package/devkit/commands/db.ts +154 -0
  10. package/devkit/commands/deploy.ts +46 -0
  11. package/devkit/commands/dev.ts +191 -0
  12. package/devkit/commands/doctor.ts +25 -0
  13. package/devkit/commands/kit.ts +45 -0
  14. package/devkit/commands/new.ts +81 -0
  15. package/devkit/commands/registry.ts +20 -0
  16. package/devkit/commands/secrets.ts +254 -0
  17. package/devkit/lib/args.ts +65 -0
  18. package/devkit/lib/audit.test.ts +405 -0
  19. package/devkit/lib/audit.ts +813 -0
  20. package/devkit/lib/config.test.ts +203 -0
  21. package/devkit/lib/config.ts +417 -0
  22. package/devkit/lib/database/index.test.ts +223 -0
  23. package/devkit/lib/database/index.ts +314 -0
  24. package/devkit/lib/database/neon.test.ts +233 -0
  25. package/devkit/lib/database/neon.ts +357 -0
  26. package/devkit/lib/database/prepare.ts +54 -0
  27. package/devkit/lib/deploy-config.test.ts +217 -0
  28. package/devkit/lib/deploy-config.ts +133 -0
  29. package/devkit/lib/deploy.test.ts +275 -0
  30. package/devkit/lib/deploy.ts +177 -0
  31. package/devkit/lib/dev-gateway.test.ts +128 -0
  32. package/devkit/lib/dev-gateway.ts +162 -0
  33. package/devkit/lib/dev-menu.test.ts +114 -0
  34. package/devkit/lib/dev-menu.ts +332 -0
  35. package/devkit/lib/dev-secrets.test.ts +156 -0
  36. package/devkit/lib/dev-secrets.ts +97 -0
  37. package/devkit/lib/dev.test.ts +194 -0
  38. package/devkit/lib/dev.ts +119 -0
  39. package/devkit/lib/doctor.test.ts +144 -0
  40. package/devkit/lib/doctor.ts +264 -0
  41. package/devkit/lib/env-file.test.ts +102 -0
  42. package/devkit/lib/env-file.ts +96 -0
  43. package/devkit/lib/glob.ts +31 -0
  44. package/devkit/lib/infisical.test.ts +208 -0
  45. package/devkit/lib/infisical.ts +139 -0
  46. package/devkit/lib/json-schema.ts +220 -0
  47. package/devkit/lib/jsonc.test.ts +53 -0
  48. package/devkit/lib/jsonc.ts +93 -0
  49. package/devkit/lib/kit-migrate.test.ts +212 -0
  50. package/devkit/lib/kit-migrate.ts +617 -0
  51. package/devkit/lib/kit-package.test.ts +83 -0
  52. package/devkit/lib/kit-package.ts +57 -0
  53. package/devkit/lib/kit-version.ts +22 -0
  54. package/devkit/lib/naming.test.ts +547 -0
  55. package/devkit/lib/naming.ts +796 -0
  56. package/devkit/lib/paths.ts +7 -0
  57. package/devkit/lib/process.ts +78 -0
  58. package/devkit/lib/project-checks.ts +86 -0
  59. package/devkit/lib/scaffold.test.ts +302 -0
  60. package/devkit/lib/scaffold.ts +315 -0
  61. package/devkit/lib/secrets.test.ts +101 -0
  62. package/devkit/lib/secrets.ts +125 -0
  63. package/devkit/lib/sourcemaps.test.ts +86 -0
  64. package/devkit/lib/sourcemaps.ts +98 -0
  65. package/devkit/lib/test-utils.ts +68 -0
  66. package/devkit/lib/workspace.ts +87 -0
  67. package/devkit/lib/wrangler.ts +231 -0
  68. package/docs/rc-config.md +93 -0
  69. package/package.json +86 -4
  70. package/presets/build.d.ts +15 -0
  71. package/presets/build.js +53 -0
  72. package/presets/vite-plus.d.ts +28 -0
  73. package/presets/vite-plus.js +153 -0
  74. package/registry/README.md +294 -0
  75. package/registry/public/r/avatar-stack.json +18 -0
  76. package/registry/public/r/avatar.json +20 -0
  77. package/registry/public/r/back-link.json +21 -0
  78. package/registry/public/r/badge.json +17 -0
  79. package/registry/public/r/button.json +18 -0
  80. package/registry/public/r/calendar-days.json +14 -0
  81. package/registry/public/r/calendar-popover.json +24 -0
  82. package/registry/public/r/calendar.json +23 -0
  83. package/registry/public/r/callout.json +20 -0
  84. package/registry/public/r/card.json +18 -0
  85. package/registry/public/r/character-counter.json +17 -0
  86. package/registry/public/r/checkbox.json +22 -0
  87. package/registry/public/r/choice-cards.json +22 -0
  88. package/registry/public/r/color-tag.json +17 -0
  89. package/registry/public/r/combobox.json +24 -0
  90. package/registry/public/r/confirm-button.json +24 -0
  91. package/registry/public/r/confirm-dialog.json +23 -0
  92. package/registry/public/r/copy-button.json +24 -0
  93. package/registry/public/r/date-picker.json +24 -0
  94. package/registry/public/r/date-range-picker.json +28 -0
  95. package/registry/public/r/detail-header.json +17 -0
  96. package/registry/public/r/detail-row.json +17 -0
  97. package/registry/public/r/dev-inbox-api.json +33 -0
  98. package/registry/public/r/dev-toolbar.json +109 -0
  99. package/registry/public/r/dev-tools-contract.json +15 -0
  100. package/registry/public/r/dialog.json +23 -0
  101. package/registry/public/r/empty-state.json +20 -0
  102. package/registry/public/r/error-state.json +23 -0
  103. package/registry/public/r/eyebrow.json +17 -0
  104. package/registry/public/r/fab.json +20 -0
  105. package/registry/public/r/field-error.json +17 -0
  106. package/registry/public/r/field-grid.json +17 -0
  107. package/registry/public/r/field.json +19 -0
  108. package/registry/public/r/file-picker.json +22 -0
  109. package/registry/public/r/filter-chips.json +17 -0
  110. package/registry/public/r/form-section.json +17 -0
  111. package/registry/public/r/form-sheet.json +22 -0
  112. package/registry/public/r/icon-button.json +23 -0
  113. package/registry/public/r/input.json +18 -0
  114. package/registry/public/r/label.json +18 -0
  115. package/registry/public/r/list-row.json +17 -0
  116. package/registry/public/r/loading-state.json +18 -0
  117. package/registry/public/r/localized-field.json +23 -0
  118. package/registry/public/r/markdown-text.json +17 -0
  119. package/registry/public/r/menu.json +21 -0
  120. package/registry/public/r/money-input.json +17 -0
  121. package/registry/public/r/month-day-select.json +18 -0
  122. package/registry/public/r/numbered-step.json +20 -0
  123. package/registry/public/r/page-actions.json +14 -0
  124. package/registry/public/r/page-header.json +17 -0
  125. package/registry/public/r/pending.json +17 -0
  126. package/registry/public/r/person-option.json +21 -0
  127. package/registry/public/r/popover.json +20 -0
  128. package/registry/public/r/progress-bar.json +17 -0
  129. package/registry/public/r/query-view.json +22 -0
  130. package/registry/public/r/registry.json +1352 -0
  131. package/registry/public/r/retry-button.json +21 -0
  132. package/registry/public/r/rich-text-editor.json +32 -0
  133. package/registry/public/r/save-bar.json +22 -0
  134. package/registry/public/r/search-input.json +23 -0
  135. package/registry/public/r/search-select.json +24 -0
  136. package/registry/public/r/section-tabs.json +17 -0
  137. package/registry/public/r/segmented.json +18 -0
  138. package/registry/public/r/select.json +23 -0
  139. package/registry/public/r/sheet.json +24 -0
  140. package/registry/public/r/skeleton.json +17 -0
  141. package/registry/public/r/spinner.json +20 -0
  142. package/registry/public/r/stale-notice.json +23 -0
  143. package/registry/public/r/stat-tile.json +23 -0
  144. package/registry/public/r/stepper.json +23 -0
  145. package/registry/public/r/suggestion-strip.json +21 -0
  146. package/registry/public/r/swatch-picker.json +22 -0
  147. package/registry/public/r/switch.json +21 -0
  148. package/registry/public/r/tabs.json +20 -0
  149. package/registry/public/r/textarea.json +18 -0
  150. package/registry/public/r/theme.json +15 -0
  151. package/registry/public/r/timezone-select.json +19 -0
  152. package/registry/public/r/toaster.json +21 -0
  153. package/registry/public/r/tooltip.json +17 -0
  154. package/registry/public/r/ui-labels.json +14 -0
  155. package/registry/public/r/use-copy-to-clipboard.json +20 -0
  156. package/registry/public/r/use-online.json +14 -0
  157. package/registry/public/r/use-stale-queries.json +17 -0
  158. package/registry/public/r/utils.json +18 -0
  159. package/registry/public/r/weekday-picker.json +19 -0
  160. package/registry/public/r/yes-no.json +20 -0
  161. package/registry/serve.ts +29 -0
  162. package/rules/architecture.md +47 -0
  163. package/rules/cloudflare.md +74 -0
  164. package/rules/code.md +12 -0
  165. package/rules/git.md +10 -0
  166. package/rules/index.md +14 -0
  167. package/rules/observability.md +13 -0
  168. package/rules/performance.md +39 -0
  169. package/rules/runtime.md +9 -0
  170. package/rules/secrets.md +11 -0
  171. package/rules/stack.md +33 -0
  172. package/rules/ui.md +23 -0
  173. package/runtime/ai.test.ts +106 -0
  174. package/runtime/ai.ts +125 -0
  175. package/runtime/observability/allowlists.ts +42 -0
  176. package/runtime/observability/anonymize.test.ts +111 -0
  177. package/runtime/observability/anonymize.ts +249 -0
  178. package/runtime/observability/browser.test.ts +46 -0
  179. package/runtime/observability/browser.ts +126 -0
  180. package/runtime/observability/server.test.ts +84 -0
  181. package/runtime/observability/server.ts +152 -0
  182. package/runtime/observability/tunnel.test.ts +276 -0
  183. package/runtime/observability/tunnel.ts +360 -0
  184. package/runtime/observability/user-agent.ts +51 -0
  185. package/schema/rc.config.schema.json +333 -0
  186. package/template/.gitattributes +1 -0
  187. package/template/.github/workflows/ci.yml +64 -0
  188. package/template/CLAUDE.md +48 -0
  189. package/template/README.md +50 -0
  190. package/template/apps/admin/.dev.vars.example +5 -0
  191. package/template/apps/admin/components.json +24 -0
  192. package/template/apps/admin/package.json +48 -0
  193. package/template/apps/admin/public/favicon.svg +1 -0
  194. package/template/apps/admin/src/components/app-shell.tsx +41 -0
  195. package/template/apps/admin/src/components/dev-sign-in-links.tsx +57 -0
  196. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-context.ts +13 -0
  197. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-environment-panel.tsx +105 -0
  198. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-inbox-list.tsx +54 -0
  199. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-inbox-message.tsx +104 -0
  200. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-inbox-panel.tsx +130 -0
  201. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-launcher.tsx +45 -0
  202. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-panel.tsx +90 -0
  203. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-types.ts +11 -0
  204. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar.tsx +61 -0
  205. package/template/apps/admin/src/components/dev-toolbar/development-devtools.tsx +32 -0
  206. package/template/apps/admin/src/components/login-form.tsx +115 -0
  207. package/template/apps/admin/src/components/login-sent.tsx +23 -0
  208. package/template/apps/admin/src/components/message-list.tsx +44 -0
  209. package/template/apps/admin/src/components/turnstile-challenge.tsx +140 -0
  210. package/template/apps/admin/src/components/ui/button.tsx +87 -0
  211. package/template/apps/admin/src/components/ui/callout.tsx +84 -0
  212. package/template/apps/admin/src/components/ui/card.tsx +48 -0
  213. package/template/apps/admin/src/components/ui/eyebrow.tsx +33 -0
  214. package/template/apps/admin/src/components/ui/field-error.tsx +24 -0
  215. package/template/apps/admin/src/components/ui/field.tsx +100 -0
  216. package/template/apps/admin/src/components/ui/input.tsx +73 -0
  217. package/template/apps/admin/src/components/ui/label.tsx +39 -0
  218. package/template/apps/admin/src/components/ui/skeleton.tsx +225 -0
  219. package/template/apps/admin/src/components/ui/spinner.tsx +12 -0
  220. package/template/apps/admin/src/components/ui/toaster.tsx +33 -0
  221. package/template/apps/admin/src/components/ui/tooltip.tsx +51 -0
  222. package/template/apps/admin/src/hooks/use-dev-inbox.ts +25 -0
  223. package/template/apps/admin/src/hooks/use-dev-toolbar-state.ts +48 -0
  224. package/template/apps/admin/src/lib/api.ts +39 -0
  225. package/template/apps/admin/src/lib/auth-client.ts +7 -0
  226. package/template/apps/admin/src/lib/dev-inbox-links.ts +66 -0
  227. package/template/apps/admin/src/lib/dev-tools-client.ts +54 -0
  228. package/template/apps/admin/src/lib/dev-tools-contract.ts +52 -0
  229. package/template/apps/admin/src/lib/messages.ts +5 -0
  230. package/template/apps/admin/src/lib/query-keys.ts +3 -0
  231. package/template/apps/admin/src/lib/security-headers.ts +18 -0
  232. package/template/apps/admin/src/lib/session.ts +27 -0
  233. package/template/apps/admin/src/lib/site-url.ts +7 -0
  234. package/template/apps/admin/src/lib/turnstile.ts +70 -0
  235. package/template/apps/admin/src/lib/ui-labels.tsx +182 -0
  236. package/template/apps/admin/src/lib/utils.ts +6 -0
  237. package/template/apps/admin/src/routeTree.gen.ts +109 -0
  238. package/template/apps/admin/src/router.tsx +29 -0
  239. package/template/apps/admin/src/routes/__root.tsx +56 -0
  240. package/template/apps/admin/src/routes/_app/index.tsx +38 -0
  241. package/template/apps/admin/src/routes/_app.tsx +26 -0
  242. package/template/apps/admin/src/routes/login.tsx +48 -0
  243. package/template/apps/admin/src/styles/app.css +11 -0
  244. package/template/apps/admin/src/worker.ts +88 -0
  245. package/template/apps/admin/tsconfig.json +19 -0
  246. package/template/apps/admin/vite.config.ts +69 -0
  247. package/template/apps/admin/wrangler.jsonc +92 -0
  248. package/template/apps/web/.env.example +1 -0
  249. package/template/apps/web/astro.config.ts +46 -0
  250. package/template/apps/web/package.json +33 -0
  251. package/template/apps/web/public/favicon.svg +1 -0
  252. package/template/apps/web/src/components/ContactForm.astro +78 -0
  253. package/template/apps/web/src/env.d.ts +11 -0
  254. package/template/apps/web/src/layouts/BaseLayout.astro +32 -0
  255. package/template/apps/web/src/lib/api.ts +8 -0
  256. package/template/apps/web/src/pages/api/messages.ts +32 -0
  257. package/template/apps/web/src/pages/index.astro +26 -0
  258. package/template/apps/web/src/pages/media/[...key].ts +4 -0
  259. package/template/apps/web/src/scripts/contact-form.ts +105 -0
  260. package/template/apps/web/src/scripts/error-reporting.ts +4 -0
  261. package/template/apps/web/src/scripts/turnstile.ts +177 -0
  262. package/template/apps/web/src/styles/global.css +11 -0
  263. package/template/apps/web/src/worker.ts +34 -0
  264. package/template/apps/web/tsconfig.json +11 -0
  265. package/template/apps/web/wrangler.jsonc +45 -0
  266. package/template/bun.lock +1925 -0
  267. package/template/bunfig.toml +2 -0
  268. package/template/gitignore +23 -0
  269. package/template/package.json +74 -0
  270. package/template/packages/api/components.json +21 -0
  271. package/template/packages/api/package.json +30 -0
  272. package/template/packages/api/src/client.ts +13 -0
  273. package/template/packages/api/src/cloudflare-env.d.ts +26 -0
  274. package/template/packages/api/src/emails/escape-html.ts +13 -0
  275. package/template/packages/api/src/emails/layout.ts +22 -0
  276. package/template/packages/api/src/emails/magic-link.ts +32 -0
  277. package/template/packages/api/src/emails/new-message.ts +32 -0
  278. package/template/packages/api/src/emails/send.ts +14 -0
  279. package/template/packages/api/src/lib/auth-guard.ts +16 -0
  280. package/template/packages/api/src/lib/auth.ts +108 -0
  281. package/template/packages/api/src/lib/context.ts +7 -0
  282. package/template/packages/api/src/lib/dev-inbox/routes.ts +77 -0
  283. package/template/packages/api/src/lib/dev-inbox/send.ts +57 -0
  284. package/template/packages/api/src/lib/dev-inbox/store.ts +185 -0
  285. package/template/packages/api/src/lib/dev-inbox.ts +6 -0
  286. package/template/packages/api/src/lib/dev-tools-contract.ts +52 -0
  287. package/template/packages/api/src/lib/environment.ts +17 -0
  288. package/template/packages/api/src/lib/turnstile.ts +33 -0
  289. package/template/packages/api/src/media.ts +45 -0
  290. package/template/packages/api/src/routes/admin-messages.ts +21 -0
  291. package/template/packages/api/src/routes/dev.ts +31 -0
  292. package/template/packages/api/src/routes/health.ts +7 -0
  293. package/template/packages/api/src/routes/messages.ts +58 -0
  294. package/template/packages/api/src/schemas.ts +18 -0
  295. package/template/packages/api/src/server.ts +29 -0
  296. package/template/packages/api/tsconfig.json +10 -0
  297. package/template/packages/db/migrations/0001_init.sql +79 -0
  298. package/template/packages/db/package.json +34 -0
  299. package/template/packages/db/prisma/schema.prisma +95 -0
  300. package/template/packages/db/prisma.config.ts +9 -0
  301. package/template/packages/db/scripts/migrate.ts +15 -0
  302. package/template/packages/db/scripts/new-migration.ts +185 -0
  303. package/template/packages/db/scripts/reset.ts +54 -0
  304. package/template/packages/db/scripts/seed.ts +44 -0
  305. package/template/packages/db/scripts/wrangler.ts +35 -0
  306. package/template/packages/db/src/d1.ts +7 -0
  307. package/template/packages/db/src/migration-guard.test.ts +43 -0
  308. package/template/packages/db/src/migration-guard.ts +51 -0
  309. package/template/packages/db/src/new-migration.test.ts +16 -0
  310. package/template/packages/db/src/scope.ts +38 -0
  311. package/template/packages/db/src/seed.ts +30 -0
  312. package/template/packages/db/src/sql.ts +42 -0
  313. package/template/packages/db/tsconfig.json +8 -0
  314. package/template/packages/shared/components.json +21 -0
  315. package/template/packages/shared/package.json +33 -0
  316. package/template/packages/shared/src/edge/origins.ts +20 -0
  317. package/template/packages/shared/src/edge/turnstile.ts +11 -0
  318. package/template/packages/shared/src/locales.ts +13 -0
  319. package/template/packages/shared/src/message-rules.ts +16 -0
  320. package/template/packages/shared/src/messages.test.ts +26 -0
  321. package/template/packages/shared/src/messages.ts +30 -0
  322. package/template/packages/shared/src/observability/build-release.ts +9 -0
  323. package/template/packages/shared/src/observability/error-reporting.ts +31 -0
  324. package/template/packages/shared/src/observability/error-tunnel.ts +20 -0
  325. package/template/packages/shared/src/observability/sentry-browser.ts +1 -0
  326. package/template/packages/shared/src/observability/server-errors.ts +74 -0
  327. package/template/packages/shared/src/project.ts +1 -0
  328. package/template/packages/shared/src/styles/rc-theme.css +138 -0
  329. package/template/packages/shared/src/styles/theme.css +9 -0
  330. package/template/packages/shared/tsconfig.json +10 -0
  331. package/template/rc.config.json +25 -0
  332. package/template/scripts/bootstrap-local.ts +42 -0
  333. package/template/tsconfig.base.json +20 -0
  334. package/template/tsconfig.json +8 -0
  335. package/template/tsconfig.worker.json +7 -0
  336. package/template/turbo.json +40 -0
  337. package/template/vite.config.ts +8 -0
@@ -0,0 +1,57 @@
1
+ import { lstatSync, readlinkSync } from "node:fs"
2
+ import { homedir } from "node:os"
3
+ import { isAbsolute, join, resolve } from "node:path"
4
+
5
+ export const KIT_PACKAGE = "@react-consulting/rc-kit"
6
+ export const LEGACY_KIT_PACKAGE = "@rc/kit"
7
+ export const KIT_RULES_PATH = `node_modules/${KIT_PACKAGE}/rules/index.md`
8
+ export const RULES_IMPORT = `@${KIT_RULES_PATH}`
9
+ export const KIT_SCHEMA_PATH = `./node_modules/${KIT_PACKAGE}/schema/rc.config.schema.json`
10
+ export const GITATTRIBUTES = "* text=auto eol=lf\n"
11
+
12
+ const RULES_IMPORT_RE = /^@(\S*rules\/index\.md)\s*$/gm
13
+ const LF_ATTRIBUTES_RE = /^\*\s.*\beol=lf\b/m
14
+ const LOCAL_SPEC_RE = /^(?:link|file|portal|workspace):/
15
+
16
+ export function rulesImportsOf(text: string): string[] {
17
+ return [...text.matchAll(RULES_IMPORT_RE)].map((match) => match[1] ?? "")
18
+ }
19
+
20
+ export function isPortableImport(spec: string): boolean {
21
+ return !(spec.startsWith("~") || isAbsolute(spec) || /^[A-Za-z]:/.test(spec))
22
+ }
23
+
24
+ export function resolveImport(spec: string, fromDir: string): string {
25
+ return spec.startsWith("~/")
26
+ ? join(homedir(), spec.slice(2))
27
+ : resolve(fromDir, spec)
28
+ }
29
+
30
+ export function hasLfAttributes(text: string | null): boolean {
31
+ return text !== null && LF_ATTRIBUTES_RE.test(text)
32
+ }
33
+
34
+ export function toPosixPath(path: string): string {
35
+ return path.replaceAll("\\", "/")
36
+ }
37
+
38
+ export function isLocalSpec(spec: string): boolean {
39
+ return LOCAL_SPEC_RE.test(spec)
40
+ }
41
+
42
+ export function installedKitDir(root: string): string {
43
+ return join(root, "node_modules", ...KIT_PACKAGE.split("/"))
44
+ }
45
+
46
+ // bun link replaces node_modules/<pkg> with a symlink to the developer's clone.
47
+ export function linkedKitTarget(root: string): string | undefined {
48
+ const dir = installedKitDir(root)
49
+
50
+ try {
51
+ return lstatSync(dir).isSymbolicLink()
52
+ ? resolve(join(dir, ".."), readlinkSync(dir))
53
+ : undefined
54
+ } catch {
55
+ return undefined
56
+ }
57
+ }
@@ -0,0 +1,22 @@
1
+ import { readFileSync } from "node:fs"
2
+ import { join } from "node:path"
3
+
4
+ import { KIT_PACKAGE } from "./kit-package"
5
+ import { KIT_ROOT } from "./paths"
6
+
7
+ export const KIT_VERSION = (
8
+ JSON.parse(readFileSync(join(KIT_ROOT, "package.json"), "utf8")) as {
9
+ version: string
10
+ }
11
+ ).version
12
+
13
+ export function kitVersionError(
14
+ range: string | undefined,
15
+ version = KIT_VERSION
16
+ ): string | undefined {
17
+ if (!range || Bun.semver.satisfies(version, range)) {
18
+ return undefined
19
+ }
20
+
21
+ return `rc.config.json demande ${KIT_PACKAGE} ${range}, la version installée est ${version} : lance « bun add -d ${KIT_PACKAGE}@latest » (ou une version qui satisfait ${range}).`
22
+ }
@@ -0,0 +1,547 @@
1
+ import { describe, expect, test } from "bun:test"
2
+
3
+ import {
4
+ checkWranglerNaming,
5
+ environmentOfKey,
6
+ formatNamingReport,
7
+ type NamingOptions,
8
+ resourcePattern,
9
+ suggestResourceName,
10
+ } from "./naming"
11
+ import type { WranglerConfig } from "./wrangler"
12
+
13
+ const RC = "0123456789abcdef0123456789abcdef"
14
+
15
+ const OTHER_ACCOUNT = "fedcba9876543210fedcba9876543210"
16
+
17
+ function check(config: WranglerConfig, options: Partial<NamingOptions> = {}) {
18
+ return checkWranglerNaming(config, {
19
+ prefix: "adv",
20
+ accountId: RC,
21
+ role: "admin",
22
+ project: "fabrikam-motors",
23
+ file: "apps/admin/wrangler.jsonc",
24
+ ...options,
25
+ })
26
+ }
27
+
28
+ function values(config: WranglerConfig, options: Partial<NamingOptions> = {}) {
29
+ return check(config, options).violations.map((violation) => violation.value)
30
+ }
31
+
32
+ function suggestions(
33
+ config: WranglerConfig,
34
+ options: Partial<NamingOptions> = {}
35
+ ) {
36
+ return Object.fromEntries(
37
+ check(config, options).violations.map((violation) => [
38
+ violation.value,
39
+ violation.suggestion,
40
+ ])
41
+ )
42
+ }
43
+
44
+ const CONFORMING: WranglerConfig = {
45
+ name: "gio-admin-local",
46
+ account_id: RC,
47
+ d1_databases: [
48
+ {
49
+ binding: "DB",
50
+ database_name: "gio-d1-local",
51
+ database_id: "gio-d1-local",
52
+ },
53
+ ],
54
+ r2_buckets: [{ binding: "MEDIA", bucket_name: "gio-media-local" }],
55
+ kv_namespaces: [{ binding: "AUTH_KV", id: "gio-kv-auth-local" }],
56
+ ratelimits: [{ name: "LEADS_RATE_LIMITER", namespace_id: "1001" }],
57
+ send_email: [{ name: "EMAIL" }],
58
+ ai: { binding: "AI" },
59
+ vars: { ENVIRONMENT: "local", AI_GATEWAY_ID: "gio-ai-gateway-prod" },
60
+ secrets: { required: ["BETTER_AUTH_SECRET"] },
61
+ env: {
62
+ production: {
63
+ name: "gio-admin-prod",
64
+ d1_databases: [
65
+ {
66
+ binding: "DB",
67
+ database_name: "gio-d1-prod",
68
+ database_id: "4f0c2a7e-1111-2222-3333-444455556666",
69
+ },
70
+ ],
71
+ r2_buckets: [{ binding: "MEDIA", bucket_name: "gio-media-prod" }],
72
+ kv_namespaces: [
73
+ { binding: "AUTH_KV", id: "5555555555555555aaaaaaaaaaaaaaaa" },
74
+ ],
75
+ ratelimits: [{ name: "LEADS_RATE_LIMITER", namespace_id: "1001" }],
76
+ queues: {
77
+ producers: [
78
+ {
79
+ binding: "STORAGE_QUEUE",
80
+ queue: "gio-queue-storage-optimization-prod",
81
+ },
82
+ ],
83
+ consumers: [
84
+ {
85
+ queue: "gio-queue-storage-optimization-prod",
86
+ dead_letter_queue: "gio-queue-storage-optimization-dlq-prod",
87
+ },
88
+ ],
89
+ },
90
+ workflows: [
91
+ {
92
+ binding: "PURCHASE_OCR_WORKFLOW",
93
+ name: "gio-wf-purchase-ocr-prod",
94
+ class_name: "PurchaseOcr",
95
+ },
96
+ ],
97
+ vectorize: [{ binding: "DOCS_INDEX", index_name: "gio-vec-docs-prod" }],
98
+ services: [{ binding: "API", service: "gio-api-prod" }],
99
+ vars: { ENVIRONMENT: "production", AI_GATEWAY_ID: "gio-ai-gateway-prod" },
100
+ },
101
+ },
102
+ }
103
+
104
+ describe("checkWranglerNaming", () => {
105
+ test("accepts a config that follows rules/cloudflare.md", () => {
106
+ expect(
107
+ check(CONFORMING, { prefix: "gio", project: "contoso-immo" }).violations
108
+ ).toEqual([])
109
+ })
110
+
111
+ test("names each Worker <pfx>-<role>-<env>, top level local", () => {
112
+ expect(
113
+ suggestions({
114
+ name: "fabrikam-motors-admin",
115
+ account_id: RC,
116
+ env: {
117
+ production: { name: "adv-admin" },
118
+ staging: { name: "adv-admin-staging" },
119
+ },
120
+ })
121
+ ).toEqual({
122
+ "fabrikam-motors-admin": "adv-admin-local",
123
+ "adv-admin": "adv-admin-prod",
124
+ })
125
+ })
126
+
127
+ test("reads a config without env as a single deployed level", () => {
128
+ expect(
129
+ values({ name: "adv-web-prod", account_id: RC }, { role: "web" })
130
+ ).toEqual([])
131
+ expect(
132
+ suggestions(
133
+ { name: "ppt-web", account_id: RC },
134
+ { prefix: "ppt", role: "web" }
135
+ )
136
+ ).toEqual({
137
+ "ppt-web": "ppt-web-prod",
138
+ })
139
+ })
140
+
141
+ test("flags a name that does not use the project prefix", () => {
142
+ expect(
143
+ suggestions(
144
+ { name: "contoso-mode", account_id: RC },
145
+ { prefix: "gfa", role: "web" }
146
+ )
147
+ ).toEqual({
148
+ "contoso-mode": "gfa-web-prod",
149
+ })
150
+ })
151
+
152
+ test("refuses an env without name and a role outside the list", () => {
153
+ const result = check(
154
+ { name: "adv-cms-local", account_id: RC, env: { production: {} } },
155
+ { role: "cms" }
156
+ )
157
+
158
+ expect(result.violations.map((violation) => violation.subject)).toEqual([
159
+ "rôle",
160
+ "Worker",
161
+ ])
162
+ expect(result.violations[1]?.suggestion).toBe("adv-cms-prod")
163
+ })
164
+
165
+ test("fixes the legacy Fabrikam Motors local resources", () => {
166
+ expect(
167
+ suggestions({
168
+ name: "adv-admin-local",
169
+ account_id: RC,
170
+ d1_databases: [
171
+ {
172
+ binding: "DB",
173
+ database_name: "adv-d1-local",
174
+ database_id: "fabrikam-motors-admin-local",
175
+ },
176
+ ],
177
+ r2_buckets: [
178
+ {
179
+ binding: "FILES",
180
+ bucket_name: "fabrikam-motors-admin-files-local",
181
+ },
182
+ {
183
+ binding: "MEDIA",
184
+ bucket_name: "fabrikam-motors-admin-media-local",
185
+ },
186
+ ],
187
+ kv_namespaces: [{ binding: "KV", id: "fabrikam-motors-admin-local" }],
188
+ })
189
+ ).toEqual({
190
+ "fabrikam-motors-admin-local": "adv-kv-local",
191
+ "fabrikam-motors-admin-files-local": "adv-files-local",
192
+ "fabrikam-motors-admin-media-local": "adv-media-local",
193
+ })
194
+ })
195
+
196
+ test("checks the local D1 id but never a remote UUID or a hex KV id", () => {
197
+ const result = check({
198
+ name: "adv-admin-local",
199
+ account_id: RC,
200
+ d1_databases: [
201
+ {
202
+ binding: "DB",
203
+ database_name: "adv-d1-local",
204
+ database_id: "fabrikam-motors-admin-local",
205
+ },
206
+ ],
207
+ env: {
208
+ production: {
209
+ name: "adv-admin-prod",
210
+ d1_databases: [
211
+ {
212
+ binding: "DB",
213
+ database_name: "adv-d1-prod",
214
+ database_id: "44444444-4444-4444-8444-444444444444",
215
+ },
216
+ ],
217
+ kv_namespaces: [
218
+ { binding: "KV", id: "5555555555555555aaaaaaaaaaaaaaaa" },
219
+ ],
220
+ },
221
+ },
222
+ })
223
+
224
+ expect(
225
+ result.violations.map((violation) => [
226
+ violation.subject,
227
+ violation.suggestion,
228
+ ])
229
+ ).toEqual([["D1 database_id local", "adv-d1-local"]])
230
+ })
231
+
232
+ test("names D1 <pfx>-d1[-<usage>]-<env>", () => {
233
+ expect(
234
+ suggestions(
235
+ {
236
+ name: "ppt-api-prod",
237
+ account_id: RC,
238
+ d1_databases: [{ binding: "DB", database_name: "ppt-db-enam" }],
239
+ },
240
+ { prefix: "ppt", role: "api", project: "pupitre" }
241
+ )
242
+ ).toEqual({ "ppt-db-enam": "ppt-d1-enam-prod" })
243
+ })
244
+
245
+ test("names R2 buckets and buckets referenced in vars", () => {
246
+ expect(
247
+ suggestions(
248
+ {
249
+ name: "rac-app-prod",
250
+ account_id: RC,
251
+ r2_buckets: [
252
+ { binding: "ASSETS_BUCKET", bucket_name: "rac-assets-prod" },
253
+ ],
254
+ vars: {
255
+ PUBLIC_ASSETS_R2_BUCKET_NAME: "rbx-assets-prod",
256
+ R2_BUCKET_NAME: "rac-r2-docs-prod",
257
+ },
258
+ },
259
+ { prefix: "rac", role: "app", project: "react-accounting" }
260
+ )
261
+ ).toEqual({
262
+ "rac-r2-docs-prod": "rac-docs-prod",
263
+ "rbx-assets-prod": "rac-assets-prod",
264
+ })
265
+ })
266
+
267
+ test("names queues, dead letter queues and workflows", () => {
268
+ expect(
269
+ suggestions(
270
+ {
271
+ name: "rbx-jobs-prod",
272
+ account_id: OTHER_ACCOUNT,
273
+ queues: {
274
+ producers: [
275
+ {
276
+ binding: "STORAGE_OPT_QUEUE",
277
+ queue: "rbx-storage-optimization",
278
+ },
279
+ ],
280
+ consumers: [
281
+ {
282
+ queue: "rbx-storage-optimization",
283
+ dead_letter_queue: "rbx-storage-optimization-dlq",
284
+ },
285
+ ],
286
+ },
287
+ workflows: [
288
+ {
289
+ binding: "PURCHASE_OCR_WORKFLOW",
290
+ name: "rbx-purchase-ocr",
291
+ class_name: "PurchaseOcrWorkflow",
292
+ },
293
+ {
294
+ binding: "INBOUND_MAIL_WORKFLOW",
295
+ name: "rbx-inbound-mail",
296
+ class_name: "InboundMailWorkflow",
297
+ },
298
+ ],
299
+ },
300
+ {
301
+ prefix: "rbx",
302
+ role: "jobs",
303
+ project: "contoso-box",
304
+ accountId: OTHER_ACCOUNT,
305
+ }
306
+ )
307
+ ).toEqual({
308
+ "rbx-storage-optimization": "rbx-queue-storage-optimization-prod",
309
+ "rbx-storage-optimization-dlq": "rbx-queue-storage-optimization-dlq-prod",
310
+ "rbx-purchase-ocr": "rbx-wf-purchase-ocr-prod",
311
+ "rbx-inbound-mail": "rbx-wf-inbound-mail-prod",
312
+ })
313
+ })
314
+
315
+ test("names the AI Gateway of each deployment, any of the project's locally", () => {
316
+ const config: WranglerConfig = {
317
+ name: "gio-admin-local",
318
+ account_id: RC,
319
+ vars: { AI_GATEWAY_ID: "gio-ai-gateway-prod" },
320
+ env: {
321
+ production: {
322
+ name: "gio-admin-prod",
323
+ vars: { AI_GATEWAY_ID: "contoso-gateway" },
324
+ },
325
+ staging: {
326
+ name: "gio-admin-staging",
327
+ vars: { AI_GATEWAY_ID: "gio-ai-gateway-prod" },
328
+ },
329
+ },
330
+ }
331
+
332
+ expect(suggestions(config, { prefix: "gio" })).toEqual({
333
+ "contoso-gateway": "gio-ai-gateway-prod",
334
+ "gio-ai-gateway-prod": "gio-ai-gateway-staging",
335
+ })
336
+ })
337
+
338
+ test("points service bindings at a Worker of the same environment", () => {
339
+ const result = check(
340
+ {
341
+ name: "gio-web-local",
342
+ account_id: RC,
343
+ services: [{ binding: "API", service: "gio-admin-prod" }],
344
+ env: {
345
+ production: {
346
+ name: "gio-web-prod",
347
+ services: [{ binding: "API", service: "contoso-admin" }],
348
+ },
349
+ },
350
+ },
351
+ { prefix: "gio", role: "web" }
352
+ )
353
+
354
+ expect(
355
+ result.violations.map((violation) => [violation.level, violation.value])
356
+ ).toEqual([
357
+ ["niveau haut", "gio-admin-prod"],
358
+ ["env.production", "contoso-admin"],
359
+ ])
360
+ })
361
+
362
+ test("wants UPPER_SNAKE bindings and the usage suffixes", () => {
363
+ expect(
364
+ suggestions({
365
+ name: "adv-admin-local",
366
+ account_id: RC,
367
+ kv_namespaces: [{ binding: "authKv", id: "adv-kv-auth-local" }],
368
+ ratelimits: [
369
+ { name: "AUTH_RATE_LIMIT" },
370
+ { name: "PUBLIC_RATE_LIMITER" },
371
+ ],
372
+ unsafe: { bindings: [{ name: "STRICT_LIMIT", type: "ratelimit" }] },
373
+ workflows: [
374
+ {
375
+ binding: "EXPORT",
376
+ name: "adv-wf-export-local",
377
+ class_name: "ExportWorkflow",
378
+ },
379
+ ],
380
+ queues: {
381
+ producers: [{ binding: "MAILS", queue: "adv-queue-mails-local" }],
382
+ },
383
+ vars: { appName: "x" },
384
+ })
385
+ ).toEqual({
386
+ authKv: "AUTH_KV",
387
+ AUTH_RATE_LIMIT: "AUTH_RATE_LIMITER",
388
+ STRICT_LIMIT: "STRICT_RATE_LIMITER",
389
+ EXPORT: "EXPORT_WORKFLOW",
390
+ MAILS: "MAILS_QUEUE",
391
+ appName: "APP_NAME",
392
+ })
393
+ })
394
+
395
+ test("wants PascalCase Durable Object classes", () => {
396
+ expect(
397
+ values({
398
+ name: "adv-admin-local",
399
+ account_id: RC,
400
+ durable_objects: {
401
+ bindings: [{ name: "REALTIME", class_name: "console_hub" }],
402
+ },
403
+ })
404
+ ).toEqual(["console_hub"])
405
+ })
406
+
407
+ test("requires a well-formed account_id shared by the project", () => {
408
+ expect(check({ name: "adv-admin-local" }).violations[0]?.message).toContain(
409
+ "account_id absent"
410
+ )
411
+ expect(
412
+ values({ name: "adv-admin-local", account_id: OTHER_ACCOUNT })
413
+ ).toEqual([OTHER_ACCOUNT])
414
+ expect(
415
+ values(
416
+ { name: "rbx-api-prod", account_id: OTHER_ACCOUNT },
417
+ { prefix: "rbx", role: "api", accountId: OTHER_ACCOUNT }
418
+ )
419
+ ).toEqual([])
420
+ expect(values({ name: "adv-admin-local", account_id: "acme" }).at(0)).toBe(
421
+ "acme"
422
+ )
423
+ })
424
+
425
+ test("gives a client deployment its own prefix", () => {
426
+ const config: WranglerConfig = {
427
+ name: "adv-admin-local",
428
+ account_id: RC,
429
+ env: {
430
+ production: { name: "adv-admin-prod" },
431
+ "atlas-tours": {
432
+ name: "atl-admin-prod",
433
+ d1_databases: [{ binding: "DB", database_name: "atl-d1-prod" }],
434
+ },
435
+ "desert-trips": { name: "adv-admin-desert" },
436
+ },
437
+ }
438
+
439
+ const result = check(config)
440
+
441
+ expect(
442
+ result.violations.map((violation) => [violation.level, violation.subject])
443
+ ).toEqual([["env.desert-trips", "préfixe client"]])
444
+ expect(
445
+ check(config, { clients: { "desert-trips": "dst" } }).violations[0]
446
+ ?.suggestion
447
+ ).toBe("dst-admin-prod")
448
+ })
449
+
450
+ test("tolerates the names listed in naming.legacy", () => {
451
+ const result = check(
452
+ {
453
+ name: "adv-admin-local",
454
+ account_id: RC,
455
+ ratelimits: [{ name: "AUTH_RATE_LIMIT" }],
456
+ },
457
+ { legacy: ["AUTH_RATE_LIMIT"] }
458
+ )
459
+
460
+ expect(result.violations).toEqual([])
461
+ expect(result.tolerated.map((violation) => violation.value)).toEqual([
462
+ "AUTH_RATE_LIMIT",
463
+ ])
464
+ expect(formatNamingReport(result)).toContain("1 nom(s) hérité(s) toléré(s)")
465
+ })
466
+ })
467
+
468
+ describe("external Workers", () => {
469
+ test("a service binding may target a Worker listed in naming.externalWorkers", () => {
470
+ const result = check(
471
+ {
472
+ name: "adv-web-local",
473
+ account_id: RC,
474
+ services: [{ binding: "CATALOG", service: "ctl-admin-local" }],
475
+ },
476
+ { role: "web", externalWorkers: ["ctl-admin-local"] }
477
+ )
478
+
479
+ expect(result.violations).toEqual([])
480
+ })
481
+
482
+ test("any other foreign Worker is still refused", () => {
483
+ const result = check(
484
+ {
485
+ name: "adv-web-local",
486
+ account_id: RC,
487
+ services: [{ binding: "OTHER", service: "xyz-admin-local" }],
488
+ },
489
+ { role: "web", externalWorkers: ["ctl-admin-local"] }
490
+ )
491
+
492
+ expect(result.violations.map((violation) => violation.value)).toEqual([
493
+ "xyz-admin-local",
494
+ ])
495
+ })
496
+ })
497
+
498
+ describe("naming helpers", () => {
499
+ test("maps Wrangler env keys to environments", () => {
500
+ expect(environmentOfKey(null)).toBe("local")
501
+ expect(environmentOfKey("production")).toBe("prod")
502
+ expect(environmentOfKey("staging")).toBe("staging")
503
+ expect(environmentOfKey("dev")).toBe("dev")
504
+ expect(environmentOfKey("atlas-tours")).toBeNull()
505
+ })
506
+
507
+ test("builds the resource forms of rules/cloudflare.md", () => {
508
+ expect(
509
+ resourcePattern("kv", "rbx", ["prod"]).test("rbx-kv-auth-prod")
510
+ ).toBe(true)
511
+ expect(resourcePattern("kv", "rbx", ["prod"]).test("rbx-kv-prod")).toBe(
512
+ true
513
+ )
514
+ expect(resourcePattern("r2", "gio", ["prod"]).test("gio-r2-prod")).toBe(
515
+ false
516
+ )
517
+ expect(
518
+ resourcePattern("hyperdrive", "rbx", ["prod"]).test("rbx-hyperdrive-prod")
519
+ ).toBe(true)
520
+ expect(
521
+ resourcePattern("ai_gateway", "gio", ["prod"]).test("gio-ai-gateway-prod")
522
+ ).toBe(true)
523
+ expect(
524
+ resourcePattern("ai_gateway", "gio", ["prod"]).test(
525
+ "gio-ai-gateway-main-prod"
526
+ )
527
+ ).toBe(false)
528
+ expect(
529
+ resourcePattern("queue", "rac", ["prod"]).test(
530
+ "rac-queue-storage-optimization-prod"
531
+ )
532
+ ).toBe(true)
533
+ })
534
+
535
+ test("suggests a usage from the legacy name", () => {
536
+ expect(
537
+ suggestResourceName("r2", "bucket", { prefix: "adv", env: "prod" })
538
+ ).toBe("adv-<usage>-prod")
539
+ expect(
540
+ suggestResourceName("d1", "fabrikam-motors-admin-local", {
541
+ prefix: "adv",
542
+ env: "local",
543
+ project: "fabrikam-motors",
544
+ })
545
+ ).toBe("adv-d1-local")
546
+ })
547
+ })