ReviewOS

also looking at this

stacks/bench-review

Adopt the hq-apps app conventions

#76
Merged glennmichael123 wants to merge chore/hq-apps-conventions into main
31 files +1656 -395
scripts/validate-release.tsadded+214-0
Changes to scripts/validate-release.ts
@@ -0,0 +1,214 @@
1import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
2import { extname, join, resolve } from 'node:path'
3
4interface AppContract {
5 domain: string
6 minPages: number
7 routes: Record<string, string[]>
8}
9
10const CONTRACTS: Record<string, AppContract> = {
11 // bench-review. Markers are chosen to survive the coming-soon -> launched
12 // transition: `/` is served by the coming-soon gate today (title "Coming
13 // Soon - Bench Review") and by home.stx once that gate lifts, so the brand
14 // name is the only stable assertion for it. The other three assert their
15 // <h1>, which is real page content rather than chrome.
16 'bench-review': {
17 domain: 'benchreview.org',
18 minPages: 35,
19 routes: {
20 '/': ['Bench Review'],
21 '/judges': ['Judicial Directory'],
22 '/login': ['Welcome back'],
23 '/register': ['Create your account'],
24 },
25 },
26 analyticshq: {
27 domain: 'analyticshq.org',
28 minPages: 30,
29 routes: {
30 '/': ['analyticshq'],
31 '/compare/matomo': ['analyticshq vs Matomo', 'Point for point.'],
32 '/login': ['analyticshq'],
33 },
34 },
35 bughq: {
36 domain: 'bughq.org',
37 minPages: 25,
38 routes: {
39 '/': ['Error tracking for people who ship.', 'From throw to triaged'],
40 '/login': ['bughq'],
41 '/dashboard': ['bughq'],
42 },
43 },
44 commshq: {
45 domain: 'commshq.org',
46 minPages: 45,
47 routes: {
48 '/': ['Grow an audience worth knowing.', 'From first hello to favorite customer.'],
49 '/login': ['CommsHQ'],
50 '/dashboard/commshq': ['CommsHQ'],
51 },
52 },
53 loghq: {
54 domain: 'loghq.org',
55 minPages: 30,
56 routes: {
57 '/': ['Your logs, finally worth reading.', 'From scattered log files to one live stream.'],
58 '/login': ['loghq'],
59 '/compare/sentry': ['loghq', 'Sentry'],
60 },
61 },
62 reportshq: {
63 domain: 'reportshq.org',
64 minPages: 20,
65 routes: {
66 '/': ['Reports that build themselves', 'Three steps, and you write none of the reporting'],
67 '/login': ['ReportsHQ'],
68 '/pricing': ['ReportsHQ'],
69 },
70 },
71 status: {
72 domain: 'statushq.org',
73 minPages: 35,
74 routes: {
75 '/': ['Know the moment', 'One dashboard for uptime, SSL, DNS, and status.'],
76 '/login': ['Welcome back'],
77 '/register': ['Create your account'],
78 },
79 },
80}
81
82const projectRoot = resolve(import.meta.dir, '..')
83const distRoot = resolve(projectRoot, 'dist')
84const ssrRoot = resolve(projectRoot, '.output')
85const ssrPages = resolve(ssrRoot, 'server/pages')
86const packageJson = JSON.parse(readFileSync(resolve(projectRoot, 'package.json'), 'utf8')) as { name?: string }
87const packageName = String(packageJson.name || '')
88const directoryName = projectRoot.split('/').pop() || ''
89const appName = CONTRACTS[packageName] ? packageName : directoryName
90const contract = CONTRACTS[appName]
91
92if (!contract)
93 throw new Error(`No release contract is configured for package ${appName || '(unnamed)'}.`)
94
95const isSsr = existsSync(ssrPages)
96if (!existsSync(distRoot) && !isSsr)
97 throw new Error('No production artifact found. Run bun run build first.')
98
99function walkFiles(directory: string): string[] {
100 if (!existsSync(directory)) return []
101 const files: string[] = []
102 for (const entry of readdirSync(directory)) {
103 const path = join(directory, entry)
104 if (statSync(path).isDirectory()) files.push(...walkFiles(path))
105 else files.push(path)
106 }
107 return files
108}
109
110function routeArtifact(route: string): { html: string, source: string } | undefined {
111 if (!isSsr) {
112 const direct = route === '/' ? join(distRoot, 'index.html') : join(distRoot, `${route.slice(1)}.html`)
113 const nested = join(distRoot, route.slice(1), 'index.html')
114 const source = existsSync(direct) ? direct : existsSync(nested) ? nested : undefined
115 return source ? { html: readFileSync(source, 'utf8'), source } : undefined
116 }
117
118 const name = route === '/' ? 'index' : route.slice(1).replaceAll('/', '-')
119 const source = join(ssrPages, `${name}.compiled.json`)
120 if (!existsSync(source)) return undefined
121 const compiled = JSON.parse(readFileSync(source, 'utf8')) as { html?: string }
122 return { html: compiled.html || '', source }
123}
124
125const pageFiles = isSsr
126 ? walkFiles(ssrPages).filter(path => path.endsWith('.compiled.json'))
127 : walkFiles(distRoot).filter(path => path.endsWith('.html'))
128
129const failures: string[] = []
130if (pageFiles.length < contract.minPages)
131 failures.push(`artifact contains ${pageFiles.length} pages; expected at least ${contract.minPages}`)
132
133const publicRoot = isSsr ? join(ssrRoot, 'public') : distRoot
134for (const [route, expectedText] of Object.entries(contract.routes)) {
135 const artifact = routeArtifact(route)
136 if (!artifact) {
137 failures.push(`${route}: production artifact is missing`)
138 continue
139 }
140
141 const withoutCode = artifact.html
142 .replace(/<script\b[^>]*>[\s\S]*?<\/script>/gi, ' ')
143 .replace(/<style\b[^>]*>[\s\S]*?<\/style>/gi, ' ')
144 .replace(/<svg\b[^>]*>[\s\S]*?<\/svg>/gi, ' ')
145 const visibleText = withoutCode.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim()
146
147 for (const fragment of expectedText) {
148 if (!visibleText.includes(fragment))
149 failures.push(`${route}: expected text is missing: ${fragment}`)
150 }
151 if (/@(if|foreach|forelse)\b/.test(visibleText))
152 failures.push(`${route}: unresolved template syntax is visible`)
153 if (/<h[12]\b[^>]*>\s*<\/h[12]>/i.test(withoutCode))
154 failures.push(`${route}: an empty primary heading was emitted`)
155 if (/\b(?:AWS_SECRET_ACCESS_KEY|DB_PASSWORD|STRIPE_SECRET_KEY)\s*[:=]\s*["']?[^\s"'<]{4,}/i.test(artifact.html))
156 failures.push(`${route}: a secret-shaped assignment was emitted`)
157
158 const assetPattern = /(?:src|href)=["'](\/[^"'?#]+)["']/gi
159 let match: RegExpExecArray | null
160 while ((match = assetPattern.exec(artifact.html))) {
161 const assetPath = match[1]
162 const looksLikeAsset = extname(assetPath) !== '' || assetPath.startsWith('/_stx/') || assetPath.startsWith('/__stx/')
163 if (!looksLikeAsset) continue
164 const local = resolve(publicRoot, `.${assetPath}`)
165 if (!local.startsWith(`${publicRoot}/`) || !existsSync(local))
166 failures.push(`${route}: referenced asset is missing: ${assetPath}`)
167 }
168}
169
170for (const metadataFile of ['sitemap.xml', 'robots.txt']) {
171 const path = join(publicRoot, metadataFile)
172 if (!existsSync(path)) {
173 failures.push(`${metadataFile}: missing from production artifact`)
174 continue
175 }
176 const contents = readFileSync(path, 'utf8')
177 if (contents.includes('http://localhost')) failures.push(`${metadataFile}: contains a localhost production URL`)
178 if (!contents.includes(contract.domain)) failures.push(`${metadataFile}: does not name ${contract.domain}`)
179}
180
181const migrationsDir = resolve(projectRoot, 'database/migrations')
182if (existsSync(migrationsDir)) {
183 for (const file of readdirSync(migrationsDir).sort()) {
184 const path = join(migrationsDir, file)
185 if (!statSync(path).isFile()) continue
186 if (readFileSync(path, 'utf8').trim().length === 0) failures.push(`migration is empty: ${file}`)
187 const sql = readFileSync(path, 'utf8')
188 if (/^(<{7}|={7}|>{7})/m.test(sql)) failures.push(`migration contains a merge marker: ${file}`)
189 }
190}
191
192if (isSsr && failures.length === 0) {
193 const { startProductionServer } = await import('@stacksjs/stx')
194 const port = 42_000 + Math.floor(Math.random() * 8_000)
195 const server = await startProductionServer({ outputDir: ssrRoot, port })
196 try {
197 for (const route of Object.keys(contract.routes)) {
198 const response = await fetch(`http://127.0.0.1:${server.port}${route}`)
199 const body = await response.text()
200 if (!response.ok) failures.push(`${route}: production server returned ${response.status}`)
201 if (!body.toLowerCase().includes(appName.toLowerCase())) failures.push(`${route}: production server response is not the expected application`)
202 }
203 }
204 finally {
205 server.stop()
206 }
207}
208
209if (failures.length) {
210 for (const failure of failures) console.error(`- ${failure}`)
211 process.exit(1)
212}
213
214console.log(`Validated ${pageFiles.length} production pages, ${Object.keys(contract.routes).length} smoke routes, metadata, assets, migrations, and ${isSsr ? 'the SSR server' : 'the SSG artifact'}.`)