@prenta/cli 1.3.3 → 1.5.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 (33) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/.turbo/turbo-test.log +67 -53
  3. package/CHANGELOG.md +48 -0
  4. package/dist/commands/doctor.d.ts.map +1 -1
  5. package/dist/commands/doctor.js +86 -31
  6. package/dist/commands/doctor.js.map +1 -1
  7. package/dist/deployment/diagnostics.d.ts +67 -2
  8. package/dist/deployment/diagnostics.d.ts.map +1 -1
  9. package/dist/deployment/diagnostics.js +500 -90
  10. package/dist/deployment/diagnostics.js.map +1 -1
  11. package/dist/deployment/health-check.d.ts +15 -0
  12. package/dist/deployment/health-check.d.ts.map +1 -0
  13. package/dist/deployment/health-check.js +85 -0
  14. package/dist/deployment/health-check.js.map +1 -0
  15. package/dist/deployment/layout.d.ts +19 -0
  16. package/dist/deployment/layout.d.ts.map +1 -0
  17. package/dist/deployment/layout.js +67 -0
  18. package/dist/deployment/layout.js.map +1 -0
  19. package/dist/vercel/env-matrix.d.ts +11 -1
  20. package/dist/vercel/env-matrix.d.ts.map +1 -1
  21. package/dist/vercel/env-matrix.js +6 -1
  22. package/dist/vercel/env-matrix.js.map +1 -1
  23. package/package.json +3 -3
  24. package/src/__tests__/deployment-diagnostics.test.ts +1 -1
  25. package/src/__tests__/doctor-astro-split.test.ts +376 -0
  26. package/src/__tests__/doctor-env-shape-fallback.test.ts +44 -0
  27. package/src/__tests__/rate-limit-backend.test.ts +259 -0
  28. package/src/__tests__/vercel-cron-plan.test.ts +71 -0
  29. package/src/commands/doctor.ts +104 -29
  30. package/src/deployment/diagnostics.ts +569 -95
  31. package/src/deployment/health-check.ts +114 -0
  32. package/src/deployment/layout.ts +90 -0
  33. package/src/vercel/env-matrix.ts +22 -0
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Reads a deployed `/api/cms/health` response for `prenta verify`.
3
+ *
4
+ * Answering is not the same as working: a deploy without a rate-limit store
5
+ * answers `/health` with `status: "unhealthy"` while refusing every sign-in
6
+ * and save (#1261), so the verdict comes from the body, not only the status.
7
+ */
8
+
9
+ export type HealthVerdict = 'pass' | 'warn' | 'fail'
10
+
11
+ export interface HealthCheckResult {
12
+ verdict: HealthVerdict
13
+ /** One line per finding, the first being the summary. */
14
+ lines: string[]
15
+ }
16
+
17
+ interface HealthEnvCheck {
18
+ name?: string
19
+ status?: string
20
+ message?: string
21
+ }
22
+
23
+ interface HealthBody {
24
+ status?: string
25
+ rateLimitBackendReady?: boolean
26
+ trustedIpSource?: boolean
27
+ trustProxyHint?: string
28
+ databaseConnected?: boolean
29
+ env?: { checks?: HealthEnvCheck[] }
30
+ }
31
+
32
+ function readBody(json: unknown): HealthBody | null {
33
+ if (!json || typeof json !== 'object') return null
34
+ const data = (json as { data?: unknown }).data
35
+ if (!data || typeof data !== 'object') return null
36
+ return data as HealthBody
37
+ }
38
+
39
+ /** Why the deploy reports itself unhealthy or degraded, as far as the body says. */
40
+ function reasons(body: HealthBody): string[] {
41
+ const out: string[] = []
42
+ if (body.rateLimitBackendReady === false) {
43
+ out.push(
44
+ 'No rate-limit store: set UPSTASH_REDIS_REST_URL/TOKEN or add Upstash from the Vercel Marketplace (KV_REST_API_URL/TOKEN). Until then sign-in, setup and saves are refused with 503.',
45
+ )
46
+ }
47
+ if (body.trustedIpSource === false) {
48
+ out.push(body.trustProxyHint ?? 'No trusted client-IP source: set PRENTA_TRUST_PROXY=1.')
49
+ }
50
+ for (const check of body.env?.checks ?? []) {
51
+ if (check.status === 'error' || check.status === 'missing') {
52
+ out.push(`${check.name ?? 'env'}: ${check.message ?? check.status}`)
53
+ }
54
+ }
55
+ if (body.databaseConnected === false) out.push('The database is not reachable.')
56
+ return out
57
+ }
58
+
59
+ export async function checkHealthResponse(
60
+ response: Response,
61
+ url: string,
62
+ ): Promise<HealthCheckResult> {
63
+ // Deployment Protection or an auth gate in front of the site: the route is
64
+ // there, but its report cannot be read.
65
+ if (response.status === 401 || response.status === 302) {
66
+ return {
67
+ verdict: 'warn',
68
+ lines: [
69
+ `Health route at ${url} answered HTTP ${response.status}, so its status could not be read (Deployment Protection?).`,
70
+ ],
71
+ }
72
+ }
73
+
74
+ let body: HealthBody | null = null
75
+ try {
76
+ body = readBody(await response.json())
77
+ } catch {
78
+ body = null
79
+ }
80
+
81
+ if (!body?.status) {
82
+ return response.ok
83
+ ? {
84
+ verdict: 'warn',
85
+ lines: [
86
+ `Health route at ${url} answered HTTP ${response.status} without a health report.`,
87
+ ],
88
+ }
89
+ : { verdict: 'fail', lines: [`Health route returned HTTP ${response.status} at ${url}.`] }
90
+ }
91
+
92
+ if (body.status === 'unhealthy') {
93
+ return {
94
+ verdict: 'fail',
95
+ lines: [
96
+ `Health route at ${url} reports "unhealthy" (HTTP ${response.status}).`,
97
+ ...reasons(body),
98
+ ],
99
+ }
100
+ }
101
+ if (!response.ok) {
102
+ return {
103
+ verdict: 'fail',
104
+ lines: [`Health route returned HTTP ${response.status} at ${url} (status "${body.status}").`],
105
+ }
106
+ }
107
+ if (body.status === 'degraded') {
108
+ return {
109
+ verdict: 'warn',
110
+ lines: [`Health route at ${url} reports "degraded".`, ...reasons(body)],
111
+ }
112
+ }
113
+ return { verdict: 'pass', lines: [`Health route at ${url} reports "${body.status}".`] }
114
+ }
@@ -0,0 +1,90 @@
1
+ import { access } from 'node:fs/promises'
2
+ import { basename, dirname, relative, resolve } from 'node:path'
3
+
4
+ /**
5
+ * Where the CMS host and the Astro public site live, wherever `prenta doctor`
6
+ * is run from (#1263).
7
+ *
8
+ * - Next monolith: everything in the current folder.
9
+ * - astro-split workspace: the CMS in `apps/cms`, the site in `apps/site`. Run
10
+ * from the repo root, from `apps/cms`, or from `apps/site`.
11
+ * - Older split layout: the site in `site/` next to the CMS.
12
+ */
13
+ export interface ProjectLayout {
14
+ /** Folder holding prenta.config.ts, prisma/ and the CMS app. */
15
+ cmsRoot: string
16
+ /** Folder holding the Astro site, when there is one. */
17
+ siteRoot?: string
18
+ /** `siteRoot` relative to the folder doctor runs in, for messages (`apps/site`). */
19
+ siteLabel?: string
20
+ }
21
+
22
+ async function exists(path: string): Promise<boolean> {
23
+ try {
24
+ await access(path)
25
+ return true
26
+ } catch {
27
+ return false
28
+ }
29
+ }
30
+
31
+ const CMS_MARKERS = [
32
+ 'prenta.config.ts',
33
+ 'prenta.config.mjs',
34
+ 'prenta.config.js',
35
+ 'app/api/cms/[[...cms]]/route.ts',
36
+ ]
37
+
38
+ const SITE_MARKERS = ['astro.config.mjs', 'astro.config.ts', 'astro.config.js', 'src/lib/cms.ts']
39
+
40
+ async function hasAny(root: string, markers: readonly string[]): Promise<boolean> {
41
+ for (const marker of markers) {
42
+ if (await exists(resolve(root, marker))) return true
43
+ }
44
+ return false
45
+ }
46
+
47
+ /** True when `dir` is a workspace package folder: `<root>/apps/<name>`. */
48
+ function isUnderApps(dir: string): boolean {
49
+ return basename(dirname(dir)) === 'apps'
50
+ }
51
+
52
+ export async function resolveProjectLayout(cwd: string): Promise<ProjectLayout> {
53
+ const cwdIsCms = await hasAny(cwd, CMS_MARKERS)
54
+ let cmsRoot = cwd
55
+
56
+ if (!cwdIsCms) {
57
+ // Run from an astro-split repo root: the CMS is a workspace package.
58
+ if (await hasAny(resolve(cwd, 'apps/cms'), CMS_MARKERS)) {
59
+ cmsRoot = resolve(cwd, 'apps/cms')
60
+ } else if (
61
+ // Run from apps/site: the CMS is its sibling.
62
+ isUnderApps(cwd) &&
63
+ (await hasAny(cwd, SITE_MARKERS)) &&
64
+ (await hasAny(resolve(cwd, '../cms'), CMS_MARKERS))
65
+ ) {
66
+ return { cmsRoot: resolve(cwd, '../cms'), siteRoot: cwd, siteLabel: '.' }
67
+ }
68
+ }
69
+
70
+ // Only folders that belong to this project: the workspace layout from the
71
+ // root, the older `site/` folder inside the project, and the sibling of a
72
+ // CMS that is itself a workspace package (apps/cms -> apps/site). A sibling
73
+ // `site/` next to a monolith is someone else's project, not this one's.
74
+ const candidates = [
75
+ resolve(cwd, 'apps/site'),
76
+ resolve(cwd, 'site'),
77
+ resolve(cmsRoot, 'site'),
78
+ ...(isUnderApps(cmsRoot) ? [resolve(cmsRoot, '../site')] : []),
79
+ ]
80
+ for (const candidate of candidates) {
81
+ if (await hasAny(candidate, SITE_MARKERS)) {
82
+ return {
83
+ cmsRoot,
84
+ siteRoot: candidate,
85
+ siteLabel: relative(cwd, candidate).replace(/\\/g, '/') || '.',
86
+ }
87
+ }
88
+ }
89
+ return { cmsRoot }
90
+ }
@@ -40,10 +40,22 @@ export interface EnvMatrix {
40
40
  /** Production and preview are the deploy-blocking targets; development is local-only. */
41
41
  const DEPLOY_TARGETS: VercelTarget[] = ['production', 'preview']
42
42
 
43
+ /**
44
+ * A requirement any one of several sets of variables satisfies, such as the
45
+ * rate-limit store, which reads either the Upstash console names or the names
46
+ * Vercel Marketplace Upstash provisions.
47
+ */
48
+ export interface RequiredOneOf {
49
+ /** Shown in `missingCritical` when no alternative is complete on a target. */
50
+ label: string
51
+ alternatives: readonly (readonly string[])[]
52
+ }
53
+
43
54
  export function buildEnvMatrix(
44
55
  envVars: VercelEnvVar[],
45
56
  requiredKeys: readonly string[],
46
57
  optionalKeys: readonly string[] = [],
58
+ requiredOneOf: readonly RequiredOneOf[] = [],
47
59
  ): EnvMatrix {
48
60
  const rows: EnvMatrixRow[] = []
49
61
  const seen = new Set<string>()
@@ -63,6 +75,16 @@ export function buildEnvMatrix(
63
75
  rows.push({ key, required: false, presence: presenceForKey(envVars, key) })
64
76
  }
65
77
 
78
+ for (const requirement of requiredOneOf) {
79
+ const missing = DEPLOY_TARGETS.filter(
80
+ (target) =>
81
+ !requirement.alternatives.some((keys) =>
82
+ keys.every((key) => presenceForKey(envVars, key)[target]),
83
+ ),
84
+ )
85
+ if (missing.length > 0) missingCritical.push({ key: requirement.label, targets: missing })
86
+ }
87
+
66
88
  return { rows, missingCritical }
67
89
  }
68
90