@supatype/cli 0.1.0-alpha.7 → 0.1.0-alpha.9

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 (128) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/.turbo/turbo-test.log +66 -61
  3. package/.turbo/turbo-typecheck.log +1 -1
  4. package/dist/app/proxy-dev-app.d.ts +13 -0
  5. package/dist/app/proxy-dev-app.d.ts.map +1 -0
  6. package/dist/app/proxy-dev-app.js +53 -0
  7. package/dist/app/proxy-dev-app.js.map +1 -0
  8. package/dist/binary-cache.d.ts +5 -0
  9. package/dist/binary-cache.d.ts.map +1 -1
  10. package/dist/binary-cache.js +13 -0
  11. package/dist/binary-cache.js.map +1 -1
  12. package/dist/commands/cloud.d.ts +11 -3
  13. package/dist/commands/cloud.d.ts.map +1 -1
  14. package/dist/commands/cloud.js +33 -25
  15. package/dist/commands/cloud.js.map +1 -1
  16. package/dist/commands/deploy.d.ts.map +1 -1
  17. package/dist/commands/deploy.js +3 -17
  18. package/dist/commands/deploy.js.map +1 -1
  19. package/dist/commands/dev.d.ts +3 -3
  20. package/dist/commands/dev.d.ts.map +1 -1
  21. package/dist/commands/dev.js +66 -59
  22. package/dist/commands/dev.js.map +1 -1
  23. package/dist/commands/diff.d.ts.map +1 -1
  24. package/dist/commands/diff.js +11 -1
  25. package/dist/commands/diff.js.map +1 -1
  26. package/dist/commands/init.js +16 -3
  27. package/dist/commands/init.js.map +1 -1
  28. package/dist/commands/push.d.ts.map +1 -1
  29. package/dist/commands/push.js +42 -12
  30. package/dist/commands/push.js.map +1 -1
  31. package/dist/commands/update.d.ts.map +1 -1
  32. package/dist/commands/update.js +16 -0
  33. package/dist/commands/update.js.map +1 -1
  34. package/dist/dev-compose.d.ts +17 -0
  35. package/dist/dev-compose.d.ts.map +1 -0
  36. package/dist/dev-compose.js +374 -0
  37. package/dist/dev-compose.js.map +1 -0
  38. package/dist/diff-output.d.ts +4 -0
  39. package/dist/diff-output.d.ts.map +1 -0
  40. package/dist/diff-output.js +12 -0
  41. package/dist/diff-output.js.map +1 -0
  42. package/dist/docker-postgres.d.ts +21 -3
  43. package/dist/docker-postgres.d.ts.map +1 -1
  44. package/dist/docker-postgres.js +130 -18
  45. package/dist/docker-postgres.js.map +1 -1
  46. package/dist/engine-client.d.ts +5 -3
  47. package/dist/engine-client.d.ts.map +1 -1
  48. package/dist/engine-client.js +2 -1
  49. package/dist/engine-client.js.map +1 -1
  50. package/dist/kong-config.d.ts +4 -0
  51. package/dist/kong-config.d.ts.map +1 -1
  52. package/dist/kong-config.js +12 -1
  53. package/dist/kong-config.js.map +1 -1
  54. package/dist/process-manager.d.ts +2 -0
  55. package/dist/process-manager.d.ts.map +1 -1
  56. package/dist/process-manager.js +16 -1
  57. package/dist/process-manager.js.map +1 -1
  58. package/dist/project-config.d.ts +21 -1
  59. package/dist/project-config.d.ts.map +1 -1
  60. package/dist/project-config.js +15 -0
  61. package/dist/project-config.js.map +1 -1
  62. package/dist/runtime-routes.d.ts +9 -0
  63. package/dist/runtime-routes.d.ts.map +1 -1
  64. package/dist/runtime-routes.js +75 -12
  65. package/dist/runtime-routes.js.map +1 -1
  66. package/dist/schema-ast-v2.d.ts +127 -0
  67. package/dist/schema-ast-v2.d.ts.map +1 -0
  68. package/dist/schema-ast-v2.js +226 -0
  69. package/dist/schema-ast-v2.js.map +1 -0
  70. package/dist/seed.d.ts +8 -0
  71. package/dist/seed.d.ts.map +1 -0
  72. package/dist/seed.js +32 -0
  73. package/dist/seed.js.map +1 -0
  74. package/dist/self-host-compose.d.ts +12 -4
  75. package/dist/self-host-compose.d.ts.map +1 -1
  76. package/dist/self-host-compose.js +146 -35
  77. package/dist/self-host-compose.js.map +1 -1
  78. package/dist/studio-admin-roles.d.ts +7 -0
  79. package/dist/studio-admin-roles.d.ts.map +1 -0
  80. package/dist/studio-admin-roles.js +14 -0
  81. package/dist/studio-admin-roles.js.map +1 -0
  82. package/dist/studio-dev-server.d.ts +22 -0
  83. package/dist/studio-dev-server.d.ts.map +1 -0
  84. package/dist/studio-dev-server.js +28 -0
  85. package/dist/studio-dev-server.js.map +1 -0
  86. package/dist/type-extractor.d.ts +3 -30
  87. package/dist/type-extractor.d.ts.map +1 -1
  88. package/dist/type-extractor.js +485 -148
  89. package/dist/type-extractor.js.map +1 -1
  90. package/dist/type-resolver.d.ts +33 -0
  91. package/dist/type-resolver.d.ts.map +1 -0
  92. package/dist/type-resolver.js +338 -0
  93. package/dist/type-resolver.js.map +1 -0
  94. package/package.json +7 -3
  95. package/src/TYPE-RESOLUTION.md +294 -0
  96. package/src/app/proxy-dev-app.ts +67 -0
  97. package/src/binary-cache.ts +20 -0
  98. package/src/commands/cloud.ts +40 -30
  99. package/src/commands/deploy.ts +3 -18
  100. package/src/commands/dev.ts +72 -69
  101. package/src/commands/diff.ts +11 -1
  102. package/src/commands/init.ts +16 -3
  103. package/src/commands/push.ts +49 -13
  104. package/src/commands/update.ts +17 -0
  105. package/src/dev-compose.ts +455 -0
  106. package/src/diff-output.ts +12 -0
  107. package/src/docker-postgres.ts +184 -27
  108. package/src/engine-client.ts +9 -4
  109. package/src/kong-config.ts +16 -1
  110. package/src/process-manager.ts +18 -1
  111. package/src/project-config.ts +34 -1
  112. package/src/runtime-routes.ts +87 -12
  113. package/src/schema-ast-v2.ts +324 -0
  114. package/src/seed.ts +43 -0
  115. package/src/self-host-compose.ts +168 -36
  116. package/src/studio-admin-roles.ts +16 -0
  117. package/src/studio-dev-server.ts +53 -0
  118. package/src/type-extractor.ts +649 -186
  119. package/src/type-resolver.ts +457 -0
  120. package/tests/config.test.ts +34 -3
  121. package/tests/docker-postgres.test.ts +39 -0
  122. package/tests/normalize-admin-config.test.ts +48 -0
  123. package/tests/proxy-dev-app.test.ts +33 -0
  124. package/tests/runtime-contract.test.ts +119 -4
  125. package/tests/studio-admin-roles.test.ts +27 -0
  126. package/tests/type-extractor.test.ts +607 -23
  127. package/tests/type-resolver.test.ts +59 -0
  128. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,455 @@
1
+ /**
2
+ * `supatype dev` when `provider: docker` — full self-host Compose stack (Kong gateway).
3
+ */
4
+
5
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"
6
+ import { homedir } from "node:os"
7
+ import { dirname, join, resolve } from "node:path"
8
+ import { spawnSync } from "node:child_process"
9
+ import { startProxyDevApp } from "./app/proxy-dev-app.js"
10
+ import { loadSchemaAst } from "./config.js"
11
+ import {
12
+ COMPOSE_DEV_KONG_PORT,
13
+ projectRootFromConfig,
14
+ resolveRuntimeProvider,
15
+ schemaPathFromProject,
16
+ type SupatypeProjectConfig,
17
+ } from "./project-config.js"
18
+ import { signJwt } from "./jwt.js"
19
+ import { isPortInUse } from "./postgres-ctl.js"
20
+ import { composeProjectName, runDockerCompose, writeSelfHostCompose, type SelfHostComposePaths } from "./self-host-compose.js"
21
+ import { hasEngineOverride } from "./binary-cache.js"
22
+ import { startStudioViteDevServer } from "./studio-dev-server.js"
23
+ import { ensureEngine, engineRequest } from "./engine-client.js"
24
+ import { withAdminRoles } from "./studio-admin-roles.js"
25
+
26
+ const LOCAL_JWT_SECRET = "super-secret-jwt-token-with-at-least-32-characters-long"
27
+
28
+ /** Default host port for compose Postgres when `overrides.engine` is set (devLocal). */
29
+ const COMPOSE_DEV_DB_PORT = 54329
30
+
31
+ export interface DevComposeOptions {
32
+ watch: boolean
33
+ }
34
+
35
+ /** In-compose Postgres URL (SCRAM; not published to the host). */
36
+ export function composeDbUrl(): string {
37
+ return "postgresql://supatype_admin:postgres@db:5432/supatype?sslmode=disable"
38
+ }
39
+
40
+ /**
41
+ * Resolve the host Kong port for this project. Persisted in `.env` as
42
+ * SUPATYPE_KONG_PORT so re-runs are stable; on first run it picks the default
43
+ * (18473) or the next free port, so multiple projects can run concurrently.
44
+ */
45
+ async function resolveDevDbPort(cwd: string): Promise<number> {
46
+ const envPath = join(cwd, ".env")
47
+ if (existsSync(envPath)) {
48
+ const m = readFileSync(envPath, "utf8").match(/^SUPATYPE_DEV_DB_PORT=(\d+)/m)
49
+ if (m && m[1]) return Number(m[1])
50
+ }
51
+ let port = COMPOSE_DEV_DB_PORT
52
+ while (await isPortInUse(port)) port++
53
+ return port
54
+ }
55
+
56
+ function readEnvValue(cwd: string, key: string, fallback: string): string {
57
+ const envPath = join(cwd, ".env")
58
+ if (existsSync(envPath)) {
59
+ const m = readFileSync(envPath, "utf8").match(new RegExp(`^${key}=(.+)$`, "m"))
60
+ if (m?.[1]) return m[1].trim()
61
+ }
62
+ return fallback
63
+ }
64
+
65
+ /** Postgres DSN for compose db when published to the host (local engine push). */
66
+ function hostComposeDbUrl(cwd: string): string {
67
+ const port = readEnvValue(cwd, "SUPATYPE_DEV_DB_PORT", String(COMPOSE_DEV_DB_PORT))
68
+ const user = readEnvValue(cwd, "POSTGRES_USER", "supatype_admin")
69
+ const pass = readEnvValue(cwd, "POSTGRES_PASSWORD", "postgres")
70
+ const db = readEnvValue(cwd, "POSTGRES_DB", "supatype")
71
+ return `postgresql://${user}:${pass}@127.0.0.1:${port}/${db}?sslmode=disable`
72
+ }
73
+
74
+ async function resolveKongPort(cwd: string): Promise<number> {
75
+ const envPath = join(cwd, ".env")
76
+ if (existsSync(envPath)) {
77
+ const m = readFileSync(envPath, "utf8").match(/^SUPATYPE_KONG_PORT=(\d+)/m)
78
+ if (m && m[1]) return Number(m[1])
79
+ }
80
+ let port = COMPOSE_DEV_KONG_PORT
81
+ while (await isPortInUse(port)) port++
82
+ return port
83
+ }
84
+
85
+ function upsertEnvFile(cwd: string, updates: Record<string, string>): void {
86
+ const envPath = join(cwd, ".env")
87
+ const existing = existsSync(envPath) ? readFileSync(envPath, "utf8") : ""
88
+ const keys = new Set(Object.keys(updates))
89
+ const kept = existing
90
+ .split("\n")
91
+ .filter((line) => {
92
+ const key = line.split("=")[0]?.trim()
93
+ return key && line.includes("=") && !keys.has(key)
94
+ })
95
+ const merged = [...kept, ...Object.entries(updates).map(([key, value]) => `${key}=${value}`)]
96
+ writeFileSync(envPath, `${merged.join("\n").trimEnd()}\n`, "utf8")
97
+ }
98
+
99
+ /** Keep compose + Studio + Astro on the same freshly signed dev JWTs every `dev` / `push`. */
100
+ function ensureDevComposeEnv(
101
+ cwd: string,
102
+ anonKey: string,
103
+ serviceRoleKey: string,
104
+ kongPort: number,
105
+ devDbPort?: number,
106
+ ): void {
107
+ const apiUrl = `http://localhost:${kongPort}`
108
+ const updates: Record<string, string> = {
109
+ POSTGRES_USER: "supatype_admin",
110
+ POSTGRES_PASSWORD: "postgres",
111
+ POSTGRES_DB: "supatype",
112
+ JWT_SECRET: LOCAL_JWT_SECRET,
113
+ ANON_KEY: anonKey,
114
+ SERVICE_ROLE_KEY: serviceRoleKey,
115
+ PUBLIC_SUPATYPE_ANON_KEY: anonKey,
116
+ PUBLIC_SUPATYPE_URL: apiUrl,
117
+ SUPATYPE_KONG_PORT: String(kongPort),
118
+ API_EXTERNAL_URL: apiUrl,
119
+ SITE_URL: apiUrl,
120
+ GOTRUE_MAILER_AUTOCONFIRM: "true",
121
+ }
122
+ if (devDbPort !== undefined) {
123
+ updates.SUPATYPE_DEV_DB_PORT = String(devDbPort)
124
+ }
125
+ upsertEnvFile(cwd, updates)
126
+ }
127
+
128
+ async function waitComposeHealthy(paths: SelfHostComposePaths, cwd: string, maxMs: number, composeProject: string): Promise<void> {
129
+ const composeDir = dirname(paths.composePath)
130
+ const envFile = join(cwd, ".env")
131
+ const baseArgs = ["compose", "-p", composeProject, "-f", paths.composePath]
132
+ if (existsSync(envFile)) baseArgs.push("--env-file", envFile)
133
+
134
+ const deadline = Date.now() + maxMs
135
+ while (Date.now() < deadline) {
136
+ const ready = spawnSync(
137
+ "docker",
138
+ [...baseArgs, "exec", "-T", "db", "pg_isready", "-U", "supatype_admin"],
139
+ { cwd: composeDir, encoding: "utf8" },
140
+ )
141
+ if (ready.status === 0) return
142
+ await new Promise((r) => setTimeout(r, 2000))
143
+ }
144
+ throw new Error("Compose db service did not become healthy in time")
145
+ }
146
+
147
+ async function waitKongReady(kongPort: number, maxSec: number): Promise<void> {
148
+ const base = `http://localhost:${kongPort}`
149
+ for (let i = 0; i < maxSec; i++) {
150
+ try {
151
+ const res = await fetch(`${base}/auth/v1/health`)
152
+ if (res.ok) return
153
+ } catch {
154
+ /* retry */
155
+ }
156
+ await new Promise((r) => setTimeout(r, 1000))
157
+ }
158
+ throw new Error(`Kong gateway at ${base} did not become ready within ${maxSec}s`)
159
+ }
160
+
161
+ let _lastPushedAst: string | null = null
162
+ let _lastFailedAst: string | null = null
163
+
164
+ /**
165
+ * Regenerate admin-config + TypeScript types from the AST using the **host** engine.
166
+ * Only schema push/migrate runs in compose (Postgres is not on the host).
167
+ */
168
+ async function refreshSchemaArtifacts(
169
+ cwd: string,
170
+ config: SupatypeProjectConfig,
171
+ ast: unknown,
172
+ ): Promise<void> {
173
+ const supatypeDir = join(cwd, ".supatype")
174
+ const adminConfigPath = join(supatypeDir, "admin-config.json")
175
+
176
+ try {
177
+ await ensureEngine()
178
+ } catch (err) {
179
+ console.warn(
180
+ `[supatype] Host engine unavailable — admin/types not refreshed: ${(err as Error).message}`,
181
+ )
182
+ return
183
+ }
184
+
185
+ const typesPath = config.output?.types
186
+ if (typeof typesPath === "string" && typesPath.trim().length > 0) {
187
+ try {
188
+ const result = await engineRequest<{ code?: string; message?: string }>("/generate", {
189
+ ast,
190
+ lang: "typescript",
191
+ })
192
+ const typesCode = result.code ?? result.message
193
+ if (typeof typesCode === "string" && typesCode.includes("export type")) {
194
+ const marker = typesCode.indexOf("// Generated by supatype-engine")
195
+ const ts = (marker >= 0 ? typesCode.slice(marker) : typesCode).trimStart()
196
+ const hostPath = join(cwd, typesPath)
197
+ mkdirSync(dirname(hostPath), { recursive: true })
198
+ writeFileSync(hostPath, ts)
199
+ console.log(`[supatype] Types written to ${typesPath}`)
200
+ } else {
201
+ console.warn("[supatype] Type generation produced no output.")
202
+ }
203
+ } catch (err) {
204
+ console.warn(`[supatype] Type generation failed: ${(err as Error).message}`)
205
+ }
206
+ }
207
+
208
+ try {
209
+ const admin = withAdminRoles(await engineRequest<unknown>("/admin", { ast }), config)
210
+ writeFileSync(adminConfigPath, `${JSON.stringify(admin, null, 2)}\n`)
211
+ console.log("[supatype] Admin config written to .supatype/admin-config.json")
212
+ } catch (err) {
213
+ console.warn(
214
+ `[supatype] Admin config generation failed — Studio may show stale field widgets: ${(err as Error).message}`,
215
+ )
216
+ }
217
+ }
218
+
219
+ async function runComposeSchemaPush(
220
+ cwd: string,
221
+ config: SupatypeProjectConfig,
222
+ paths: SelfHostComposePaths,
223
+ schemaPath: string,
224
+ composeProject: string,
225
+ ): Promise<void> {
226
+ const ast = loadSchemaAst(schemaPath, cwd)
227
+ const astJson = JSON.stringify(ast)
228
+
229
+ const supatypeDir = join(cwd, ".supatype")
230
+ mkdirSync(supatypeDir, { recursive: true })
231
+ const astPath = join(supatypeDir, "schema.ast.json")
232
+ // Always materialise on disk — schema-engine reads via bind mount; skip must not omit the write.
233
+ writeFileSync(astPath, astJson)
234
+ if (astJson === _lastPushedAst && astJson !== _lastFailedAst) return
235
+
236
+ if (!existsSync(astPath)) {
237
+ throw new Error(`Failed to write schema AST at ${astPath}`)
238
+ }
239
+
240
+ // Admin + types come from the AST only (no DB) — refresh before push so Studio stays
241
+ // in sync even when migration fails (e.g. bad engine image, lossy column change).
242
+ await refreshSchemaArtifacts(cwd, config, ast)
243
+
244
+ if (hasEngineOverride(config)) {
245
+ console.log("[supatype] Applying schema via local engine (overrides.engine)...")
246
+ await ensureEngine()
247
+ const pgSchema = config.schema?.pg_schema ?? "public"
248
+ try {
249
+ await engineRequest("/push", {
250
+ ast,
251
+ database_url: hostComposeDbUrl(cwd),
252
+ schema: pgSchema,
253
+ force: true,
254
+ })
255
+ } catch (err) {
256
+ _lastFailedAst = astJson
257
+ throw err
258
+ }
259
+ _lastPushedAst = astJson
260
+ _lastFailedAst = null
261
+ console.log("[supatype] Schema applied.")
262
+ return
263
+ }
264
+
265
+ console.log("[supatype] Applying schema via compose schema-engine...")
266
+ let push = runComposeEnginePush(paths, cwd, composeProject)
267
+ // Windows Docker bind mounts can lag briefly after the host write.
268
+ if (push.status !== 0) {
269
+ await new Promise((r) => setTimeout(r, 250))
270
+ push = runComposeEnginePush(paths, cwd, composeProject)
271
+ }
272
+ if (push.status !== 0) {
273
+ _lastFailedAst = astJson
274
+ throw new Error(push.output || `Engine schema push failed (exit ${push.status})`)
275
+ }
276
+ _lastPushedAst = astJson
277
+ _lastFailedAst = null
278
+
279
+ console.log("[supatype] Schema applied.")
280
+ }
281
+
282
+ function runComposeEnginePush(
283
+ paths: SelfHostComposePaths,
284
+ cwd: string,
285
+ composeProject: string,
286
+ ): { status: number; output: string } {
287
+ const envFile = resolve(cwd, ".env")
288
+ const composeArgs = ["compose"]
289
+ if (composeProject) composeArgs.push("-p", composeProject)
290
+ composeArgs.push("--project-directory", cwd)
291
+ composeArgs.push("-f", paths.composePath)
292
+ if (existsSync(envFile)) {
293
+ composeArgs.push("--env-file", envFile)
294
+ }
295
+ composeArgs.push(
296
+ "--profile",
297
+ "tools",
298
+ "run",
299
+ "--rm",
300
+ "schema-engine",
301
+ "push",
302
+ "-i",
303
+ "/project/.supatype/schema.ast.json",
304
+ "--database-url",
305
+ composeDbUrl(),
306
+ "--force",
307
+ "--non-interactive",
308
+ )
309
+ const result = spawnSync("docker", composeArgs, {
310
+ cwd,
311
+ encoding: "utf8",
312
+ maxBuffer: 10 * 1024 * 1024,
313
+ })
314
+ const output = `${result.stdout ?? ""}${result.stderr ?? ""}`.trim()
315
+ if (output.length > 0) {
316
+ console.error(output)
317
+ }
318
+ return { status: result.status ?? 1, output }
319
+ }
320
+
321
+ /**
322
+ * `supatype push` when `provider: docker`. Uses in-compose schema-engine unless
323
+ * `overrides.engine` is set — then Postgres is published to the host and push runs
324
+ * through the local engine binary (AST v2, contributor builds).
325
+ */
326
+ export async function pushSchemaDocker(cwd: string, config: SupatypeProjectConfig): Promise<void> {
327
+ if (resolveRuntimeProvider(config) !== "docker") {
328
+ throw new Error("pushSchemaDocker requires provider: docker")
329
+ }
330
+ const project = composeProjectName(config.project.name)
331
+ const kongPort = await resolveKongPort(cwd)
332
+ const devDbPort = hasEngineOverride(config) ? await resolveDevDbPort(cwd) : undefined
333
+
334
+ const now = Math.floor(Date.now() / 1000)
335
+ const jwtBase = { iss: "supatype", iat: now, exp: now + 315_360_000 }
336
+ const anonKey = signJwt({ ...jwtBase, role: "anon" }, LOCAL_JWT_SECRET)
337
+ const serviceRoleKey = signJwt({ ...jwtBase, role: "service_role" }, LOCAL_JWT_SECRET)
338
+ ensureDevComposeEnv(cwd, anonKey, serviceRoleKey, kongPort, devDbPort)
339
+
340
+ const paths = writeSelfHostCompose(cwd, config, { devLocal: true })
341
+
342
+ console.log(`[supatype] provider: docker — applying schema via compose (project ${project})...`)
343
+ const up = runDockerCompose(paths.composePath, ["up", "-d", "db"], cwd, project)
344
+ if (up !== 0) process.exit(up)
345
+ await waitComposeHealthy(paths, cwd, 120_000, project)
346
+
347
+ const schemaPath = schemaPathFromProject(config, cwd)
348
+ await runComposeSchemaPush(cwd, config, paths, schemaPath, project)
349
+ console.log("[supatype] Schema pushed.")
350
+ }
351
+
352
+ export async function runDevCompose(cwd: string, config: SupatypeProjectConfig, opts: DevComposeOptions): Promise<void> {
353
+ if (resolveRuntimeProvider(config) !== "docker") {
354
+ throw new Error("runDevCompose requires provider: docker")
355
+ }
356
+
357
+ // Per-project compose name + port isolate this project from any other Supatype
358
+ // stack on the machine (own containers, volumes, network, and gateway port).
359
+ const project = composeProjectName(config.project.name)
360
+ const kongPort = await resolveKongPort(cwd)
361
+ const devDbPort = hasEngineOverride(config) ? await resolveDevDbPort(cwd) : undefined
362
+
363
+ const now = Math.floor(Date.now() / 1000)
364
+ const jwtBase = { iss: "supatype", iat: now, exp: now + 315_360_000 }
365
+ const anonKey = signJwt({ ...jwtBase, role: "anon" }, LOCAL_JWT_SECRET)
366
+ const serviceRoleKey = signJwt({ ...jwtBase, role: "service_role" }, LOCAL_JWT_SECRET)
367
+
368
+ ensureDevComposeEnv(cwd, anonKey, serviceRoleKey, kongPort, devDbPort)
369
+
370
+ console.log(`[supatype] provider: docker — starting self-host Compose stack (project ${project}, gateway :${kongPort})...`)
371
+ const paths = writeSelfHostCompose(cwd, config, { devLocal: true })
372
+
373
+ const upStatus = runDockerCompose(paths.composePath, ["up", "-d"], cwd, project)
374
+ if (upStatus !== 0) process.exit(upStatus)
375
+
376
+ console.log("[supatype] Waiting for Postgres (compose)...")
377
+ await waitComposeHealthy(paths, cwd, 180_000, project)
378
+
379
+ const schemaPath = schemaPathFromProject(config, cwd)
380
+ await runComposeSchemaPush(cwd, config, paths, schemaPath, project).catch((e: unknown) =>
381
+ console.error("[supatype] Initial schema push failed:", (e as Error).message),
382
+ )
383
+
384
+ console.log("[supatype] Waiting for API gateway...")
385
+ await waitKongReady(kongPort, 120)
386
+
387
+ const pidDir = join(homedir(), ".supatype", "projects", config.project.name, "pid")
388
+ mkdirSync(pidDir, { recursive: true })
389
+
390
+ let studioProc: Awaited<ReturnType<typeof startStudioViteDevServer>> = null
391
+ const studioOverride = config.overrides?.studio
392
+ if (studioOverride) {
393
+ studioProc = startStudioViteDevServer({
394
+ cwd,
395
+ studioOverride,
396
+ pidDir,
397
+ serviceRoleKey,
398
+ proxyTarget: `http://localhost:${kongPort}`,
399
+ viteSupatypeUrl: `http://localhost:${kongPort}`,
400
+ basePath: "/studio/",
401
+ })
402
+ studioProc?.start()
403
+ if (studioProc) {
404
+ console.log("[supatype] Studio Vite dev server (overrides.studio) — live reload at /studio/")
405
+ }
406
+ }
407
+
408
+ console.log(`
409
+ [supatype] Services running (Docker Compose · project ${project}):
410
+ API (Kong) http://localhost:${kongPort}
411
+ REST API http://localhost:${kongPort}/rest/v1/
412
+ Auth http://localhost:${kongPort}/auth/v1/
413
+ Storage http://localhost:${kongPort}/storage/v1/
414
+ Realtime ws://localhost:${kongPort}/realtime/v1/
415
+ Studio http://localhost:${kongPort}/studio/
416
+
417
+ API keys (local dev only):
418
+ anon key ${anonKey}
419
+ service_role ${serviceRoleKey}
420
+
421
+ Press Ctrl+C to stop.
422
+ `)
423
+
424
+ const appProc = startProxyDevApp(cwd, config, pidDir)
425
+
426
+ const cleanup = async () => {
427
+ console.log("\n[supatype] Shutting down compose...")
428
+ await studioProc?.stop()
429
+ await appProc?.stop()
430
+ runDockerCompose(paths.composePath, ["down"], cwd, project)
431
+ process.exit(0)
432
+ }
433
+ process.once("SIGINT", () => void cleanup())
434
+ process.once("SIGTERM", () => void cleanup())
435
+
436
+ if (opts.watch) {
437
+ const schemaDir = join(projectRootFromConfig(config, cwd), config.schema?.path ?? "schema/index.ts", "..")
438
+ console.log(`[supatype] Watching ${schemaDir} for changes...`)
439
+ const { watch } = await import("node:fs")
440
+ let debounceTimer: ReturnType<typeof setTimeout> | null = null
441
+ watch(schemaDir, { recursive: true }, (_eventType, filename) => {
442
+ if (!filename?.endsWith(".ts")) return
443
+ if (debounceTimer) clearTimeout(debounceTimer)
444
+ debounceTimer = setTimeout(() => {
445
+ debounceTimer = null
446
+ console.log(`\n[supatype] Change detected in ${filename}, pushing schema...`)
447
+ runComposeSchemaPush(cwd, config, paths, schemaPath, project).catch((e: unknown) =>
448
+ console.error("[supatype] Schema push failed:", (e as Error).message),
449
+ )
450
+ }, 300)
451
+ })
452
+ }
453
+
454
+ await new Promise<never>(() => undefined)
455
+ }
@@ -0,0 +1,12 @@
1
+ import type { DiffResult } from "./engine-client.js"
2
+
3
+ /** Print engine diff warnings before the operation list. */
4
+ export function printDiffWarnings(diff: DiffResult): void {
5
+ const warnings = diff.warnings ?? []
6
+ if (warnings.length === 0) return
7
+ console.log(`\n${warnings.length} warning(s):\n`)
8
+ for (const w of warnings) {
9
+ console.log(` [!] ${w}`)
10
+ }
11
+ console.log()
12
+ }