@react-consulting/rc-kit 0.0.0-stage → 0.13.1
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.
- package/LICENSE +21 -0
- package/README.md +93 -2
- package/devkit/README.md +301 -0
- package/devkit/cli.test.ts +211 -0
- package/devkit/cli.ts +120 -0
- package/devkit/commands/build.ts +127 -0
- package/devkit/commands/checks.ts +123 -0
- package/devkit/commands/context.ts +47 -0
- package/devkit/commands/db.ts +154 -0
- package/devkit/commands/deploy.ts +46 -0
- package/devkit/commands/dev.ts +191 -0
- package/devkit/commands/doctor.ts +25 -0
- package/devkit/commands/kit.ts +45 -0
- package/devkit/commands/new.ts +81 -0
- package/devkit/commands/registry.ts +20 -0
- package/devkit/commands/secrets.ts +254 -0
- package/devkit/lib/args.ts +65 -0
- package/devkit/lib/audit.test.ts +405 -0
- package/devkit/lib/audit.ts +813 -0
- package/devkit/lib/config.test.ts +203 -0
- package/devkit/lib/config.ts +417 -0
- package/devkit/lib/database/index.test.ts +223 -0
- package/devkit/lib/database/index.ts +314 -0
- package/devkit/lib/database/neon.test.ts +233 -0
- package/devkit/lib/database/neon.ts +357 -0
- package/devkit/lib/database/prepare.ts +54 -0
- package/devkit/lib/deploy-config.test.ts +217 -0
- package/devkit/lib/deploy-config.ts +133 -0
- package/devkit/lib/deploy.test.ts +275 -0
- package/devkit/lib/deploy.ts +177 -0
- package/devkit/lib/dev-gateway.test.ts +128 -0
- package/devkit/lib/dev-gateway.ts +162 -0
- package/devkit/lib/dev-menu.test.ts +114 -0
- package/devkit/lib/dev-menu.ts +332 -0
- package/devkit/lib/dev-secrets.test.ts +156 -0
- package/devkit/lib/dev-secrets.ts +97 -0
- package/devkit/lib/dev.test.ts +194 -0
- package/devkit/lib/dev.ts +119 -0
- package/devkit/lib/doctor.test.ts +144 -0
- package/devkit/lib/doctor.ts +264 -0
- package/devkit/lib/env-file.test.ts +102 -0
- package/devkit/lib/env-file.ts +96 -0
- package/devkit/lib/glob.ts +31 -0
- package/devkit/lib/infisical.test.ts +208 -0
- package/devkit/lib/infisical.ts +139 -0
- package/devkit/lib/json-schema.ts +220 -0
- package/devkit/lib/jsonc.test.ts +53 -0
- package/devkit/lib/jsonc.ts +93 -0
- package/devkit/lib/kit-migrate.test.ts +215 -0
- package/devkit/lib/kit-migrate.ts +624 -0
- package/devkit/lib/kit-package.test.ts +83 -0
- package/devkit/lib/kit-package.ts +57 -0
- package/devkit/lib/kit-version.ts +22 -0
- package/devkit/lib/naming.test.ts +547 -0
- package/devkit/lib/naming.ts +796 -0
- package/devkit/lib/paths.ts +7 -0
- package/devkit/lib/process.ts +78 -0
- package/devkit/lib/project-checks.ts +86 -0
- package/devkit/lib/scaffold.test.ts +302 -0
- package/devkit/lib/scaffold.ts +315 -0
- package/devkit/lib/secrets.test.ts +101 -0
- package/devkit/lib/secrets.ts +125 -0
- package/devkit/lib/sourcemaps.test.ts +86 -0
- package/devkit/lib/sourcemaps.ts +98 -0
- package/devkit/lib/test-utils.ts +68 -0
- package/devkit/lib/workspace.ts +87 -0
- package/devkit/lib/wrangler.ts +231 -0
- package/docs/rc-config.md +93 -0
- package/package.json +86 -4
- package/presets/build.d.ts +15 -0
- package/presets/build.js +53 -0
- package/presets/vite-plus.d.ts +28 -0
- package/presets/vite-plus.js +153 -0
- package/registry/README.md +294 -0
- package/registry/public/r/avatar-stack.json +18 -0
- package/registry/public/r/avatar.json +20 -0
- package/registry/public/r/back-link.json +21 -0
- package/registry/public/r/badge.json +17 -0
- package/registry/public/r/button.json +18 -0
- package/registry/public/r/calendar-days.json +14 -0
- package/registry/public/r/calendar-popover.json +24 -0
- package/registry/public/r/calendar.json +23 -0
- package/registry/public/r/callout.json +20 -0
- package/registry/public/r/card.json +18 -0
- package/registry/public/r/character-counter.json +17 -0
- package/registry/public/r/checkbox.json +22 -0
- package/registry/public/r/choice-cards.json +22 -0
- package/registry/public/r/color-tag.json +17 -0
- package/registry/public/r/combobox.json +24 -0
- package/registry/public/r/confirm-button.json +24 -0
- package/registry/public/r/confirm-dialog.json +23 -0
- package/registry/public/r/copy-button.json +24 -0
- package/registry/public/r/date-picker.json +24 -0
- package/registry/public/r/date-range-picker.json +28 -0
- package/registry/public/r/detail-header.json +17 -0
- package/registry/public/r/detail-row.json +17 -0
- package/registry/public/r/dev-inbox-api.json +33 -0
- package/registry/public/r/dev-toolbar.json +109 -0
- package/registry/public/r/dev-tools-contract.json +15 -0
- package/registry/public/r/dialog.json +23 -0
- package/registry/public/r/empty-state.json +20 -0
- package/registry/public/r/error-state.json +23 -0
- package/registry/public/r/eyebrow.json +17 -0
- package/registry/public/r/fab.json +20 -0
- package/registry/public/r/field-error.json +17 -0
- package/registry/public/r/field-grid.json +17 -0
- package/registry/public/r/field.json +19 -0
- package/registry/public/r/file-picker.json +22 -0
- package/registry/public/r/filter-chips.json +17 -0
- package/registry/public/r/form-section.json +17 -0
- package/registry/public/r/form-sheet.json +22 -0
- package/registry/public/r/icon-button.json +23 -0
- package/registry/public/r/input.json +18 -0
- package/registry/public/r/label.json +18 -0
- package/registry/public/r/list-row.json +17 -0
- package/registry/public/r/loading-state.json +18 -0
- package/registry/public/r/localized-field.json +23 -0
- package/registry/public/r/markdown-text.json +17 -0
- package/registry/public/r/menu.json +21 -0
- package/registry/public/r/money-input.json +17 -0
- package/registry/public/r/month-day-select.json +18 -0
- package/registry/public/r/numbered-step.json +20 -0
- package/registry/public/r/page-actions.json +14 -0
- package/registry/public/r/page-header.json +17 -0
- package/registry/public/r/pending.json +17 -0
- package/registry/public/r/person-option.json +21 -0
- package/registry/public/r/popover.json +20 -0
- package/registry/public/r/progress-bar.json +17 -0
- package/registry/public/r/query-view.json +22 -0
- package/registry/public/r/registry.json +1352 -0
- package/registry/public/r/retry-button.json +21 -0
- package/registry/public/r/rich-text-editor.json +32 -0
- package/registry/public/r/save-bar.json +22 -0
- package/registry/public/r/search-input.json +23 -0
- package/registry/public/r/search-select.json +24 -0
- package/registry/public/r/section-tabs.json +17 -0
- package/registry/public/r/segmented.json +18 -0
- package/registry/public/r/select.json +23 -0
- package/registry/public/r/sheet.json +24 -0
- package/registry/public/r/skeleton.json +17 -0
- package/registry/public/r/spinner.json +20 -0
- package/registry/public/r/stale-notice.json +23 -0
- package/registry/public/r/stat-tile.json +23 -0
- package/registry/public/r/stepper.json +23 -0
- package/registry/public/r/suggestion-strip.json +21 -0
- package/registry/public/r/swatch-picker.json +22 -0
- package/registry/public/r/switch.json +21 -0
- package/registry/public/r/tabs.json +20 -0
- package/registry/public/r/textarea.json +18 -0
- package/registry/public/r/theme.json +15 -0
- package/registry/public/r/timezone-select.json +19 -0
- package/registry/public/r/toaster.json +21 -0
- package/registry/public/r/tooltip.json +17 -0
- package/registry/public/r/ui-labels.json +14 -0
- package/registry/public/r/use-copy-to-clipboard.json +20 -0
- package/registry/public/r/use-online.json +14 -0
- package/registry/public/r/use-stale-queries.json +17 -0
- package/registry/public/r/utils.json +18 -0
- package/registry/public/r/weekday-picker.json +19 -0
- package/registry/public/r/yes-no.json +20 -0
- package/registry/serve.ts +29 -0
- package/rules/architecture.md +47 -0
- package/rules/cloudflare.md +74 -0
- package/rules/code.md +12 -0
- package/rules/git.md +10 -0
- package/rules/index.md +14 -0
- package/rules/observability.md +13 -0
- package/rules/performance.md +39 -0
- package/rules/runtime.md +9 -0
- package/rules/secrets.md +11 -0
- package/rules/stack.md +33 -0
- package/rules/ui.md +23 -0
- package/runtime/ai.test.ts +106 -0
- package/runtime/ai.ts +125 -0
- package/runtime/observability/allowlists.ts +42 -0
- package/runtime/observability/anonymize.test.ts +111 -0
- package/runtime/observability/anonymize.ts +249 -0
- package/runtime/observability/browser.test.ts +46 -0
- package/runtime/observability/browser.ts +126 -0
- package/runtime/observability/server.test.ts +84 -0
- package/runtime/observability/server.ts +152 -0
- package/runtime/observability/tunnel.test.ts +276 -0
- package/runtime/observability/tunnel.ts +360 -0
- package/runtime/observability/user-agent.ts +51 -0
- package/schema/rc.config.schema.json +333 -0
- package/template/.gitattributes +1 -0
- package/template/.github/workflows/ci.yml +64 -0
- package/template/CLAUDE.md +48 -0
- package/template/README.md +50 -0
- package/template/apps/admin/.dev.vars.example +5 -0
- package/template/apps/admin/components.json +24 -0
- package/template/apps/admin/package.json +48 -0
- package/template/apps/admin/public/favicon.svg +1 -0
- package/template/apps/admin/src/components/app-shell.tsx +41 -0
- package/template/apps/admin/src/components/dev-sign-in-links.tsx +57 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-context.ts +13 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-environment-panel.tsx +105 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-inbox-list.tsx +54 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-inbox-message.tsx +104 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-inbox-panel.tsx +130 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-launcher.tsx +45 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-panel.tsx +90 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-types.ts +11 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar.tsx +61 -0
- package/template/apps/admin/src/components/dev-toolbar/development-devtools.tsx +32 -0
- package/template/apps/admin/src/components/login-form.tsx +115 -0
- package/template/apps/admin/src/components/login-sent.tsx +23 -0
- package/template/apps/admin/src/components/message-list.tsx +44 -0
- package/template/apps/admin/src/components/turnstile-challenge.tsx +140 -0
- package/template/apps/admin/src/components/ui/button.tsx +87 -0
- package/template/apps/admin/src/components/ui/callout.tsx +84 -0
- package/template/apps/admin/src/components/ui/card.tsx +48 -0
- package/template/apps/admin/src/components/ui/eyebrow.tsx +33 -0
- package/template/apps/admin/src/components/ui/field-error.tsx +24 -0
- package/template/apps/admin/src/components/ui/field.tsx +100 -0
- package/template/apps/admin/src/components/ui/input.tsx +73 -0
- package/template/apps/admin/src/components/ui/label.tsx +39 -0
- package/template/apps/admin/src/components/ui/skeleton.tsx +225 -0
- package/template/apps/admin/src/components/ui/spinner.tsx +12 -0
- package/template/apps/admin/src/components/ui/toaster.tsx +33 -0
- package/template/apps/admin/src/components/ui/tooltip.tsx +51 -0
- package/template/apps/admin/src/hooks/use-dev-inbox.ts +25 -0
- package/template/apps/admin/src/hooks/use-dev-toolbar-state.ts +48 -0
- package/template/apps/admin/src/lib/api.ts +39 -0
- package/template/apps/admin/src/lib/auth-client.ts +7 -0
- package/template/apps/admin/src/lib/dev-inbox-links.ts +66 -0
- package/template/apps/admin/src/lib/dev-tools-client.ts +54 -0
- package/template/apps/admin/src/lib/dev-tools-contract.ts +52 -0
- package/template/apps/admin/src/lib/messages.ts +5 -0
- package/template/apps/admin/src/lib/query-keys.ts +3 -0
- package/template/apps/admin/src/lib/security-headers.ts +18 -0
- package/template/apps/admin/src/lib/session.ts +27 -0
- package/template/apps/admin/src/lib/site-url.ts +7 -0
- package/template/apps/admin/src/lib/turnstile.ts +70 -0
- package/template/apps/admin/src/lib/ui-labels.tsx +182 -0
- package/template/apps/admin/src/lib/utils.ts +6 -0
- package/template/apps/admin/src/routeTree.gen.ts +109 -0
- package/template/apps/admin/src/router.tsx +29 -0
- package/template/apps/admin/src/routes/__root.tsx +56 -0
- package/template/apps/admin/src/routes/_app/index.tsx +38 -0
- package/template/apps/admin/src/routes/_app.tsx +26 -0
- package/template/apps/admin/src/routes/login.tsx +48 -0
- package/template/apps/admin/src/styles/app.css +11 -0
- package/template/apps/admin/src/worker.ts +88 -0
- package/template/apps/admin/tsconfig.json +19 -0
- package/template/apps/admin/vite.config.ts +69 -0
- package/template/apps/admin/wrangler.jsonc +92 -0
- package/template/apps/web/.env.example +1 -0
- package/template/apps/web/astro.config.ts +46 -0
- package/template/apps/web/package.json +33 -0
- package/template/apps/web/public/favicon.svg +1 -0
- package/template/apps/web/src/components/ContactForm.astro +78 -0
- package/template/apps/web/src/env.d.ts +11 -0
- package/template/apps/web/src/layouts/BaseLayout.astro +32 -0
- package/template/apps/web/src/lib/api.ts +8 -0
- package/template/apps/web/src/pages/api/messages.ts +32 -0
- package/template/apps/web/src/pages/index.astro +26 -0
- package/template/apps/web/src/pages/media/[...key].ts +4 -0
- package/template/apps/web/src/scripts/contact-form.ts +105 -0
- package/template/apps/web/src/scripts/error-reporting.ts +4 -0
- package/template/apps/web/src/scripts/turnstile.ts +177 -0
- package/template/apps/web/src/styles/global.css +11 -0
- package/template/apps/web/src/worker.ts +34 -0
- package/template/apps/web/tsconfig.json +11 -0
- package/template/apps/web/wrangler.jsonc +45 -0
- package/template/bun.lock +1925 -0
- package/template/bunfig.toml +2 -0
- package/template/gitignore +23 -0
- package/template/package.json +74 -0
- package/template/packages/api/components.json +21 -0
- package/template/packages/api/package.json +30 -0
- package/template/packages/api/src/client.ts +13 -0
- package/template/packages/api/src/cloudflare-env.d.ts +26 -0
- package/template/packages/api/src/emails/escape-html.ts +13 -0
- package/template/packages/api/src/emails/layout.ts +22 -0
- package/template/packages/api/src/emails/magic-link.ts +32 -0
- package/template/packages/api/src/emails/new-message.ts +32 -0
- package/template/packages/api/src/emails/send.ts +14 -0
- package/template/packages/api/src/lib/auth-guard.ts +16 -0
- package/template/packages/api/src/lib/auth.ts +108 -0
- package/template/packages/api/src/lib/context.ts +7 -0
- package/template/packages/api/src/lib/dev-inbox/routes.ts +77 -0
- package/template/packages/api/src/lib/dev-inbox/send.ts +57 -0
- package/template/packages/api/src/lib/dev-inbox/store.ts +185 -0
- package/template/packages/api/src/lib/dev-inbox.ts +6 -0
- package/template/packages/api/src/lib/dev-tools-contract.ts +52 -0
- package/template/packages/api/src/lib/environment.ts +17 -0
- package/template/packages/api/src/lib/turnstile.ts +33 -0
- package/template/packages/api/src/media.ts +45 -0
- package/template/packages/api/src/routes/admin-messages.ts +21 -0
- package/template/packages/api/src/routes/dev.ts +31 -0
- package/template/packages/api/src/routes/health.ts +7 -0
- package/template/packages/api/src/routes/messages.ts +58 -0
- package/template/packages/api/src/schemas.ts +18 -0
- package/template/packages/api/src/server.ts +29 -0
- package/template/packages/api/tsconfig.json +10 -0
- package/template/packages/db/migrations/0001_init.sql +79 -0
- package/template/packages/db/package.json +34 -0
- package/template/packages/db/prisma/schema.prisma +95 -0
- package/template/packages/db/prisma.config.ts +9 -0
- package/template/packages/db/scripts/migrate.ts +15 -0
- package/template/packages/db/scripts/new-migration.ts +185 -0
- package/template/packages/db/scripts/reset.ts +54 -0
- package/template/packages/db/scripts/seed.ts +44 -0
- package/template/packages/db/scripts/wrangler.ts +35 -0
- package/template/packages/db/src/d1.ts +7 -0
- package/template/packages/db/src/migration-guard.test.ts +43 -0
- package/template/packages/db/src/migration-guard.ts +51 -0
- package/template/packages/db/src/new-migration.test.ts +16 -0
- package/template/packages/db/src/scope.ts +38 -0
- package/template/packages/db/src/seed.ts +30 -0
- package/template/packages/db/src/sql.ts +42 -0
- package/template/packages/db/tsconfig.json +8 -0
- package/template/packages/shared/components.json +21 -0
- package/template/packages/shared/package.json +33 -0
- package/template/packages/shared/src/edge/origins.ts +20 -0
- package/template/packages/shared/src/edge/turnstile.ts +11 -0
- package/template/packages/shared/src/locales.ts +13 -0
- package/template/packages/shared/src/message-rules.ts +16 -0
- package/template/packages/shared/src/messages.test.ts +26 -0
- package/template/packages/shared/src/messages.ts +30 -0
- package/template/packages/shared/src/observability/build-release.ts +9 -0
- package/template/packages/shared/src/observability/error-reporting.ts +31 -0
- package/template/packages/shared/src/observability/error-tunnel.ts +20 -0
- package/template/packages/shared/src/observability/sentry-browser.ts +1 -0
- package/template/packages/shared/src/observability/server-errors.ts +74 -0
- package/template/packages/shared/src/project.ts +1 -0
- package/template/packages/shared/src/styles/rc-theme.css +138 -0
- package/template/packages/shared/src/styles/theme.css +9 -0
- package/template/packages/shared/tsconfig.json +10 -0
- package/template/rc.config.json +25 -0
- package/template/scripts/bootstrap-local.ts +42 -0
- package/template/tsconfig.base.json +20 -0
- package/template/tsconfig.json +8 -0
- package/template/tsconfig.worker.json +7 -0
- package/template/turbo.json +40 -0
- package/template/vite.config.ts +8 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { afterAll, describe, expect, test } from "bun:test"
|
|
2
|
+
import { readFileSync } from "node:fs"
|
|
3
|
+
import { join } from "node:path"
|
|
4
|
+
|
|
5
|
+
import type { DatabaseConfig, LoadedConfig } from "../config"
|
|
6
|
+
import { cleanupTempDirs, fakeRunner, tempDir, writeTree } from "../test-utils"
|
|
7
|
+
import {
|
|
8
|
+
assertLocalTarget,
|
|
9
|
+
migrateCommand,
|
|
10
|
+
runDatabaseCommand,
|
|
11
|
+
targetEnv,
|
|
12
|
+
writeDevEnv,
|
|
13
|
+
} from "./index"
|
|
14
|
+
|
|
15
|
+
afterAll(cleanupTempDirs)
|
|
16
|
+
|
|
17
|
+
const NEON_DATABASE: DatabaseConfig = {
|
|
18
|
+
provider: "neon",
|
|
19
|
+
seed: { command: ["bun", "prisma/seed.ts"], cwd: "apps/api" },
|
|
20
|
+
neon: {
|
|
21
|
+
production: {
|
|
22
|
+
projectId: "little-river-1",
|
|
23
|
+
projectName: "fabrikam",
|
|
24
|
+
endpointId: "ep-prod-1",
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const DEV_POOLED = "postgresql://u:p@ep-dev-1-pooler.c-8.aws.neon.tech/neondb"
|
|
30
|
+
const DEV_DIRECT = "postgresql://u:p@ep-dev-1.c-8.aws.neon.tech/neondb"
|
|
31
|
+
|
|
32
|
+
function project(
|
|
33
|
+
database: DatabaseConfig,
|
|
34
|
+
files: Record<string, string> = {}
|
|
35
|
+
): LoadedConfig {
|
|
36
|
+
const root = writeTree(tempDir(), { "package.json": "{}", ...files })
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
root,
|
|
40
|
+
configPath: join(root, "rc.config.json"),
|
|
41
|
+
config: {
|
|
42
|
+
project: "fabrikam",
|
|
43
|
+
prefix: "fab",
|
|
44
|
+
account: "contoso",
|
|
45
|
+
apps: { api: { path: "apps/api" } },
|
|
46
|
+
infisical: { projectId: "22222222-2222-4222-8222-222222222222" },
|
|
47
|
+
database,
|
|
48
|
+
},
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function devNeonRunner() {
|
|
53
|
+
return fakeRunner((command) => {
|
|
54
|
+
const args = command.slice(1)
|
|
55
|
+
|
|
56
|
+
if (args[0] === "projects") {
|
|
57
|
+
return { stdout: JSON.stringify([{ id: "dev", name: "fabrikam-dev" }]) }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (args[0] === "branches") {
|
|
61
|
+
return {
|
|
62
|
+
stdout: JSON.stringify([
|
|
63
|
+
{ name: "main", current_state: "ready", default: true },
|
|
64
|
+
{ name: "development", current_state: "archived" },
|
|
65
|
+
]),
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (args[0] === "connection-string") {
|
|
70
|
+
return { stdout: args.includes("--pooled") ? DEV_POOLED : DEV_DIRECT }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return undefined
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
describe("writeDevEnv", () => {
|
|
78
|
+
test("writes the development strings and keeps the other keys", () => {
|
|
79
|
+
const loaded = project(NEON_DATABASE, {
|
|
80
|
+
".env.local": "BETTER_AUTH_SECRET=kept\nDATABASE_URL=stale\n",
|
|
81
|
+
})
|
|
82
|
+
const { runner } = devNeonRunner()
|
|
83
|
+
|
|
84
|
+
const branch = writeDevEnv(loaded, { gitBranch: "main", runner })
|
|
85
|
+
const env = readFileSync(join(loaded.root, ".env.local"), "utf8")
|
|
86
|
+
|
|
87
|
+
expect(branch).toBe("development")
|
|
88
|
+
expect(env).toContain("BETTER_AUTH_SECRET=kept")
|
|
89
|
+
expect(env).toContain(`DATABASE_URL="${DEV_POOLED}"`)
|
|
90
|
+
expect(env).toContain(`DATABASE_URL_UNPOOLED="${DEV_DIRECT}"`)
|
|
91
|
+
expect(env).toContain('NEON_BRANCH="development"')
|
|
92
|
+
expect(env).toContain('APP_ENV="local"')
|
|
93
|
+
})
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
describe("writeDevEnv without an env file", () => {
|
|
97
|
+
test("starts from .env.example", () => {
|
|
98
|
+
const loaded = project(NEON_DATABASE, { ".env.example": "PORT=3000\n" })
|
|
99
|
+
const { runner } = devNeonRunner()
|
|
100
|
+
|
|
101
|
+
writeDevEnv(loaded, { gitBranch: "main", runner })
|
|
102
|
+
|
|
103
|
+
expect(readFileSync(join(loaded.root, ".env.local"), "utf8")).toContain(
|
|
104
|
+
"PORT=3000"
|
|
105
|
+
)
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
describe("targetEnv", () => {
|
|
110
|
+
test("local reads the env file after the guard", () => {
|
|
111
|
+
const loaded = project(NEON_DATABASE, {
|
|
112
|
+
".env.local": `APP_ENV=local\nDATABASE_URL=${DEV_POOLED}\nNEON_BRANCH=development\n`,
|
|
113
|
+
})
|
|
114
|
+
const { runner } = fakeRunner(() => undefined)
|
|
115
|
+
|
|
116
|
+
expect(targetEnv(loaded, "local", runner)).toEqual({
|
|
117
|
+
RC_DATABASE_TARGET: "local",
|
|
118
|
+
APP_ENV: "local",
|
|
119
|
+
DATABASE_URL: DEV_POOLED,
|
|
120
|
+
DATABASE_URL_UNPOOLED: DEV_DIRECT,
|
|
121
|
+
})
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
test("local refuses an env file that points at production", () => {
|
|
125
|
+
const loaded = project(NEON_DATABASE, {
|
|
126
|
+
".env.local":
|
|
127
|
+
"APP_ENV=local\nDATABASE_URL=postgresql://u:p@ep-prod-1.c-8.aws.neon.tech/db\n",
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
expect(() => assertLocalTarget(loaded)).toThrow("PRODUCTION")
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
test("production asks neonctl, never the env file", () => {
|
|
134
|
+
const prod = "postgresql://u:p@ep-prod-1.c-8.aws.neon.tech/neondb"
|
|
135
|
+
const loaded = project(NEON_DATABASE, {
|
|
136
|
+
".env.local": `APP_ENV=local\nDATABASE_URL=${DEV_POOLED}\n`,
|
|
137
|
+
})
|
|
138
|
+
const { runner } = fakeRunner(() => ({ stdout: prod }))
|
|
139
|
+
|
|
140
|
+
expect(targetEnv(loaded, "production", runner)).toMatchObject({
|
|
141
|
+
APP_ENV: "production",
|
|
142
|
+
ENVIRONMENT: "production",
|
|
143
|
+
DATABASE_URL: prod,
|
|
144
|
+
RC_DATABASE_TARGET: "production",
|
|
145
|
+
})
|
|
146
|
+
})
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
describe("migrateCommand", () => {
|
|
150
|
+
test("defaults to prisma migrate deploy for neon", () => {
|
|
151
|
+
expect(migrateCommand(project(NEON_DATABASE), "local")).toEqual({
|
|
152
|
+
command: ["bun", "x", "prisma", "migrate", "deploy"],
|
|
153
|
+
cwd: "packages/db",
|
|
154
|
+
})
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
test("defaults to wrangler, local or remote, for d1", () => {
|
|
158
|
+
const loaded = project({
|
|
159
|
+
provider: "d1",
|
|
160
|
+
d1: { app: "api", binding: "DB" },
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
expect(migrateCommand(loaded, "local").command).toContain("--local")
|
|
164
|
+
expect(migrateCommand(loaded, "production")).toEqual({
|
|
165
|
+
command: [
|
|
166
|
+
"bun",
|
|
167
|
+
"x",
|
|
168
|
+
"wrangler",
|
|
169
|
+
"d1",
|
|
170
|
+
"migrations",
|
|
171
|
+
"apply",
|
|
172
|
+
"DB",
|
|
173
|
+
"--remote",
|
|
174
|
+
"--env",
|
|
175
|
+
"production",
|
|
176
|
+
],
|
|
177
|
+
cwd: "apps/api",
|
|
178
|
+
})
|
|
179
|
+
})
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
describe("runDatabaseCommand", () => {
|
|
183
|
+
test("retries while the database wakes up", () => {
|
|
184
|
+
const loaded = project(NEON_DATABASE)
|
|
185
|
+
let calls = 0
|
|
186
|
+
const { runner } = fakeRunner(() => {
|
|
187
|
+
calls += 1
|
|
188
|
+
|
|
189
|
+
return calls < 3
|
|
190
|
+
? { exitCode: 1, stderr: "Error: P1001: Can't reach database server" }
|
|
191
|
+
: { stdout: "All migrations have been successfully applied." }
|
|
192
|
+
})
|
|
193
|
+
const output: string[] = []
|
|
194
|
+
|
|
195
|
+
const exitCode = runDatabaseCommand(
|
|
196
|
+
loaded,
|
|
197
|
+
{ command: ["prisma"] },
|
|
198
|
+
{},
|
|
199
|
+
runner,
|
|
200
|
+
{ sleep: () => undefined, write: (text) => output.push(text) }
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
expect(exitCode).toBe(0)
|
|
204
|
+
expect(calls).toBe(3)
|
|
205
|
+
expect(output.at(-1)).toContain("successfully applied")
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
test("does not retry any other failure", () => {
|
|
209
|
+
const loaded = project(NEON_DATABASE)
|
|
210
|
+
const { runner, calls } = fakeRunner(() => ({
|
|
211
|
+
exitCode: 1,
|
|
212
|
+
stderr: "P3009 migrate found failed migrations",
|
|
213
|
+
}))
|
|
214
|
+
|
|
215
|
+
expect(
|
|
216
|
+
runDatabaseCommand(loaded, { command: ["prisma"] }, {}, runner, {
|
|
217
|
+
sleep: () => undefined,
|
|
218
|
+
write: () => undefined,
|
|
219
|
+
})
|
|
220
|
+
).toBe(1)
|
|
221
|
+
expect(calls).toHaveLength(1)
|
|
222
|
+
})
|
|
223
|
+
})
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs"
|
|
2
|
+
import { dirname, join, resolve } from "node:path"
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
DatabaseCommand,
|
|
6
|
+
DatabaseConfig,
|
|
7
|
+
LoadedConfig,
|
|
8
|
+
NeonDatabaseConfig,
|
|
9
|
+
} from "../config"
|
|
10
|
+
import { applyEnvValues, parseEnvFile } from "../env-file"
|
|
11
|
+
import type { Runner } from "../process"
|
|
12
|
+
import {
|
|
13
|
+
DEFAULT_FEATURE_TTL_DAYS,
|
|
14
|
+
devBranchForGitBranch,
|
|
15
|
+
directConnectionString,
|
|
16
|
+
ensureDevBranch,
|
|
17
|
+
localGuardErrors,
|
|
18
|
+
NeonCli,
|
|
19
|
+
productionConnection,
|
|
20
|
+
resolveDevProject,
|
|
21
|
+
waitForBranch,
|
|
22
|
+
type WaitOptions,
|
|
23
|
+
} from "./neon"
|
|
24
|
+
|
|
25
|
+
export type DatabaseTarget = "local" | "production"
|
|
26
|
+
|
|
27
|
+
export const DEFAULT_ENV_FILE = ".env.local"
|
|
28
|
+
|
|
29
|
+
const NEON_MIGRATE: DatabaseCommand = {
|
|
30
|
+
command: ["bun", "x", "prisma", "migrate", "deploy"],
|
|
31
|
+
cwd: "packages/db",
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const WAKING_DATABASE_RE = /P1001|Can't reach database server|ECONNREFUSED/i
|
|
35
|
+
|
|
36
|
+
export function databaseConfigOf(loaded: LoadedConfig): DatabaseConfig {
|
|
37
|
+
const database = loaded.config.database
|
|
38
|
+
|
|
39
|
+
if (!database) {
|
|
40
|
+
throw new Error(
|
|
41
|
+
`${loaded.configPath} n'a pas de clé database : voir docs/rc-config.md.`
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return database
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function neonConfigOf(database: DatabaseConfig): NeonDatabaseConfig {
|
|
49
|
+
if (database.provider !== "neon" || !database.neon) {
|
|
50
|
+
throw new Error("Cette commande ne concerne que provider neon.")
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return database.neon
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function envFilePath(loaded: LoadedConfig): string {
|
|
57
|
+
return resolve(
|
|
58
|
+
loaded.root,
|
|
59
|
+
databaseConfigOf(loaded).envFile ?? DEFAULT_ENV_FILE
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function readLocalEnv(loaded: LoadedConfig): Map<string, string> {
|
|
64
|
+
const path = envFilePath(loaded)
|
|
65
|
+
|
|
66
|
+
return existsSync(path) ? parseEnvFile(readFileSync(path, "utf8")) : new Map()
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function currentGitBranch(root: string, runner: Runner): string {
|
|
70
|
+
const branch = runner(["git", "branch", "--show-current"], {
|
|
71
|
+
cwd: root,
|
|
72
|
+
}).stdout.trim()
|
|
73
|
+
|
|
74
|
+
if (!branch) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
"Branche git indéterminée (HEAD détachée ?) : passe la branche en argument."
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return branch
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function readEnvOrExample(path: string): string {
|
|
84
|
+
if (existsSync(path)) {
|
|
85
|
+
return readFileSync(path, "utf8")
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const example = join(dirname(path), ".env.example")
|
|
89
|
+
|
|
90
|
+
return existsSync(example) ? readFileSync(example, "utf8") : ""
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface DevEnvOptions extends WaitOptions {
|
|
94
|
+
gitBranch: string
|
|
95
|
+
runner: Runner
|
|
96
|
+
log?: (line: string) => void
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function writeDevEnv(
|
|
100
|
+
loaded: LoadedConfig,
|
|
101
|
+
options: DevEnvOptions
|
|
102
|
+
): string {
|
|
103
|
+
const neon = neonConfigOf(databaseConfigOf(loaded))
|
|
104
|
+
const cli = new NeonCli(options.runner)
|
|
105
|
+
|
|
106
|
+
if (!cli.authenticated()) {
|
|
107
|
+
throw new Error(
|
|
108
|
+
"neonctl doit être installé et connecté : lance « neonctl auth »."
|
|
109
|
+
)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const project = resolveDevProject(cli, neon)
|
|
113
|
+
const target = devBranchForGitBranch(options.gitBranch)
|
|
114
|
+
|
|
115
|
+
ensureDevBranch(cli, project.id, target, {
|
|
116
|
+
ttlDays: neon.featureBranchTtlDays ?? DEFAULT_FEATURE_TTL_DAYS,
|
|
117
|
+
now: options.now,
|
|
118
|
+
log: options.log,
|
|
119
|
+
})
|
|
120
|
+
waitForBranch(cli, project.id, target.branch, options)
|
|
121
|
+
|
|
122
|
+
const connection = cli.connection(project.id, target.branch)
|
|
123
|
+
const path = envFilePath(loaded)
|
|
124
|
+
const current = readEnvOrExample(path)
|
|
125
|
+
|
|
126
|
+
writeFileSync(
|
|
127
|
+
path,
|
|
128
|
+
applyEnvValues(current, {
|
|
129
|
+
APP_ENV: "local",
|
|
130
|
+
DATABASE_URL: connection.pooled,
|
|
131
|
+
DATABASE_URL_UNPOOLED: connection.direct,
|
|
132
|
+
NEON_BRANCH: target.branch,
|
|
133
|
+
NEON_PROJECT_ID: project.id,
|
|
134
|
+
NEON_PROJECT_NAME: project.name,
|
|
135
|
+
})
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
return target.branch
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function assertLocalTarget(loaded: LoadedConfig): Map<string, string> {
|
|
142
|
+
const database = databaseConfigOf(loaded)
|
|
143
|
+
const env = readLocalEnv(loaded)
|
|
144
|
+
|
|
145
|
+
if (database.provider !== "neon" || !database.neon) {
|
|
146
|
+
return env
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const errors = localGuardErrors(env, database.neon)
|
|
150
|
+
|
|
151
|
+
if (errors.length > 0) {
|
|
152
|
+
throw new Error(
|
|
153
|
+
[
|
|
154
|
+
`Commande de base locale bloquée (${envFilePath(loaded)}) :`,
|
|
155
|
+
...errors.map((error) => ` - ${error}`),
|
|
156
|
+
" → Lance « rc db:env » pour réécrire les chaînes de connexion de dev.",
|
|
157
|
+
].join("\n")
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return env
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function migrateCommand(
|
|
165
|
+
loaded: LoadedConfig,
|
|
166
|
+
target: DatabaseTarget
|
|
167
|
+
): DatabaseCommand {
|
|
168
|
+
const database = databaseConfigOf(loaded)
|
|
169
|
+
|
|
170
|
+
if (database.migrate) {
|
|
171
|
+
return database.migrate
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (database.provider === "neon") {
|
|
175
|
+
return NEON_MIGRATE
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const d1 = database.d1 as { app: string; binding: string }
|
|
179
|
+
const location =
|
|
180
|
+
target === "production" ? ["--remote", "--env", "production"] : ["--local"]
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
command: [
|
|
184
|
+
"bun",
|
|
185
|
+
"x",
|
|
186
|
+
"wrangler",
|
|
187
|
+
"d1",
|
|
188
|
+
"migrations",
|
|
189
|
+
"apply",
|
|
190
|
+
d1.binding,
|
|
191
|
+
...location,
|
|
192
|
+
],
|
|
193
|
+
cwd: loaded.config.apps[d1.app]?.path,
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function seedCommand(loaded: LoadedConfig): DatabaseCommand {
|
|
198
|
+
const seed = databaseConfigOf(loaded).seed
|
|
199
|
+
|
|
200
|
+
if (!seed) {
|
|
201
|
+
throw new Error(
|
|
202
|
+
`${loaded.configPath} n'a pas de database.seed : ajoute la commande de seed du projet.`
|
|
203
|
+
)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return seed
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function productionLabel(loaded: LoadedConfig): string {
|
|
210
|
+
const database = databaseConfigOf(loaded)
|
|
211
|
+
|
|
212
|
+
return database.neon?.production.projectName ?? loaded.config.project
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function targetEnv(
|
|
216
|
+
loaded: LoadedConfig,
|
|
217
|
+
target: DatabaseTarget,
|
|
218
|
+
runner: Runner
|
|
219
|
+
): Record<string, string> {
|
|
220
|
+
const database = databaseConfigOf(loaded)
|
|
221
|
+
const base = { RC_DATABASE_TARGET: target }
|
|
222
|
+
|
|
223
|
+
if (database.provider !== "neon") {
|
|
224
|
+
return base
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (target === "local") {
|
|
228
|
+
const env = assertLocalTarget(loaded)
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
...base,
|
|
232
|
+
APP_ENV: "local",
|
|
233
|
+
DATABASE_URL: env.get("DATABASE_URL") ?? "",
|
|
234
|
+
DATABASE_URL_UNPOOLED:
|
|
235
|
+
env.get("DATABASE_URL_UNPOOLED") ??
|
|
236
|
+
directConnectionString(env.get("DATABASE_URL") ?? ""),
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const url = productionConnection(new NeonCli(runner), neonConfigOf(database))
|
|
241
|
+
|
|
242
|
+
return {
|
|
243
|
+
...base,
|
|
244
|
+
APP_ENV: "production",
|
|
245
|
+
ENVIRONMENT: "production",
|
|
246
|
+
DATABASE_URL: url,
|
|
247
|
+
DATABASE_URL_UNPOOLED: url,
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export interface RunDatabaseOptions {
|
|
252
|
+
attempts?: number
|
|
253
|
+
delayMs?: number
|
|
254
|
+
sleep?: (ms: number) => void
|
|
255
|
+
write?: (text: string) => void
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export function runDatabaseCommand(
|
|
259
|
+
loaded: LoadedConfig,
|
|
260
|
+
command: DatabaseCommand,
|
|
261
|
+
env: Record<string, string>,
|
|
262
|
+
runner: Runner,
|
|
263
|
+
options: RunDatabaseOptions = {}
|
|
264
|
+
): number {
|
|
265
|
+
const attempts = options.attempts ?? 6
|
|
266
|
+
const sleep = options.sleep ?? Bun.sleepSync
|
|
267
|
+
const write = options.write ?? ((text) => process.stdout.write(text))
|
|
268
|
+
const cwd = resolve(loaded.root, command.cwd ?? ".")
|
|
269
|
+
|
|
270
|
+
for (let attempt = 1; ; attempt += 1) {
|
|
271
|
+
const result = runner(command.command, { cwd, env })
|
|
272
|
+
const output = `${result.stdout}${result.stderr}`
|
|
273
|
+
const waking = result.exitCode !== 0 && WAKING_DATABASE_RE.test(output)
|
|
274
|
+
|
|
275
|
+
if (!waking || attempt >= attempts) {
|
|
276
|
+
write(output)
|
|
277
|
+
|
|
278
|
+
return result.exitCode
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
write(
|
|
282
|
+
` · base injoignable, sans doute en réveil (essai ${attempt}/${attempts})…\n`
|
|
283
|
+
)
|
|
284
|
+
sleep(options.delayMs ?? 10_000)
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export function deleteFeatureBranch(
|
|
289
|
+
loaded: LoadedConfig,
|
|
290
|
+
gitBranch: string,
|
|
291
|
+
runner: Runner
|
|
292
|
+
): string | null {
|
|
293
|
+
const neon = neonConfigOf(databaseConfigOf(loaded))
|
|
294
|
+
const target = devBranchForGitBranch(gitBranch)
|
|
295
|
+
|
|
296
|
+
if (!target.disposable) {
|
|
297
|
+
throw new Error(
|
|
298
|
+
`Suppression refusée : ${target.branch} n'est pas une branche feature/*.`
|
|
299
|
+
)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const cli = new NeonCli(runner)
|
|
303
|
+
const project = resolveDevProject(cli, neon)
|
|
304
|
+
|
|
305
|
+
if (
|
|
306
|
+
!cli.branches(project.id).some((branch) => branch.name === target.branch)
|
|
307
|
+
) {
|
|
308
|
+
return null
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
cli.deleteBranch(project.id, target.branch)
|
|
312
|
+
|
|
313
|
+
return target.branch
|
|
314
|
+
}
|