@supatype/cli 0.1.0-alpha.6 → 0.1.0-alpha.8

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 (350) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/.turbo/turbo-test.log +208 -1
  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/app-config.d.ts +7 -0
  9. package/dist/app-config.d.ts.map +1 -0
  10. package/dist/app-config.js +113 -0
  11. package/dist/app-config.js.map +1 -0
  12. package/dist/augmentation-generator.d.ts +2 -0
  13. package/dist/augmentation-generator.d.ts.map +1 -0
  14. package/dist/augmentation-generator.js +111 -0
  15. package/dist/augmentation-generator.js.map +1 -0
  16. package/dist/binary-cache.d.ts +94 -0
  17. package/dist/binary-cache.d.ts.map +1 -0
  18. package/dist/binary-cache.js +669 -0
  19. package/dist/binary-cache.js.map +1 -0
  20. package/dist/cli.d.ts.map +1 -1
  21. package/dist/cli.js +13 -7
  22. package/dist/cli.js.map +1 -1
  23. package/dist/commands/admin.d.ts.map +1 -1
  24. package/dist/commands/admin.js +4 -3
  25. package/dist/commands/admin.js.map +1 -1
  26. package/dist/commands/app.d.ts.map +1 -1
  27. package/dist/commands/app.js +56 -209
  28. package/dist/commands/app.js.map +1 -1
  29. package/dist/commands/cache.d.ts +6 -0
  30. package/dist/commands/cache.d.ts.map +1 -0
  31. package/dist/commands/cache.js +105 -0
  32. package/dist/commands/cache.js.map +1 -0
  33. package/dist/commands/cloud.d.ts +20 -0
  34. package/dist/commands/cloud.d.ts.map +1 -1
  35. package/dist/commands/cloud.js +50 -52
  36. package/dist/commands/cloud.js.map +1 -1
  37. package/dist/commands/db.d.ts.map +1 -1
  38. package/dist/commands/db.js +47 -54
  39. package/dist/commands/db.js.map +1 -1
  40. package/dist/commands/deploy.d.ts +2 -1
  41. package/dist/commands/deploy.d.ts.map +1 -1
  42. package/dist/commands/deploy.js +79 -52
  43. package/dist/commands/deploy.js.map +1 -1
  44. package/dist/commands/dev.d.ts +11 -0
  45. package/dist/commands/dev.d.ts.map +1 -1
  46. package/dist/commands/dev.js +759 -385
  47. package/dist/commands/dev.js.map +1 -1
  48. package/dist/commands/diff.d.ts.map +1 -1
  49. package/dist/commands/diff.js +30 -15
  50. package/dist/commands/diff.js.map +1 -1
  51. package/dist/commands/engine.d.ts +1 -3
  52. package/dist/commands/engine.d.ts.map +1 -1
  53. package/dist/commands/engine.js +13 -85
  54. package/dist/commands/engine.js.map +1 -1
  55. package/dist/commands/functions.d.ts.map +1 -1
  56. package/dist/commands/functions.js +92 -105
  57. package/dist/commands/functions.js.map +1 -1
  58. package/dist/commands/generate.d.ts.map +1 -1
  59. package/dist/commands/generate.js +22 -12
  60. package/dist/commands/generate.js.map +1 -1
  61. package/dist/commands/init.d.ts +1 -1
  62. package/dist/commands/init.d.ts.map +1 -1
  63. package/dist/commands/init.js +137 -410
  64. package/dist/commands/init.js.map +1 -1
  65. package/dist/commands/migrate-from-v1.d.ts +5 -0
  66. package/dist/commands/migrate-from-v1.d.ts.map +1 -0
  67. package/dist/commands/migrate-from-v1.js +125 -0
  68. package/dist/commands/migrate-from-v1.js.map +1 -0
  69. package/dist/commands/migrate.d.ts.map +1 -1
  70. package/dist/commands/migrate.js +27 -23
  71. package/dist/commands/migrate.js.map +1 -1
  72. package/dist/commands/pg.d.ts +8 -0
  73. package/dist/commands/pg.d.ts.map +1 -0
  74. package/dist/commands/pg.js +102 -0
  75. package/dist/commands/pg.js.map +1 -0
  76. package/dist/commands/pull.d.ts.map +1 -1
  77. package/dist/commands/pull.js +5 -66
  78. package/dist/commands/pull.js.map +1 -1
  79. package/dist/commands/push.d.ts.map +1 -1
  80. package/dist/commands/push.js +128 -38
  81. package/dist/commands/push.js.map +1 -1
  82. package/dist/commands/seed.d.ts +2 -0
  83. package/dist/commands/seed.d.ts.map +1 -1
  84. package/dist/commands/seed.js +44 -11
  85. package/dist/commands/seed.js.map +1 -1
  86. package/dist/commands/self-host.d.ts +7 -1
  87. package/dist/commands/self-host.d.ts.map +1 -1
  88. package/dist/commands/self-host.js +272 -758
  89. package/dist/commands/self-host.js.map +1 -1
  90. package/dist/commands/self-update.d.ts +9 -0
  91. package/dist/commands/self-update.d.ts.map +1 -0
  92. package/dist/commands/self-update.js +33 -0
  93. package/dist/commands/self-update.js.map +1 -0
  94. package/dist/commands/status.d.ts.map +1 -1
  95. package/dist/commands/status.js +4 -3
  96. package/dist/commands/status.js.map +1 -1
  97. package/dist/commands/types.d.ts +3 -0
  98. package/dist/commands/types.d.ts.map +1 -0
  99. package/dist/commands/types.js +62 -0
  100. package/dist/commands/types.js.map +1 -0
  101. package/dist/commands/update.d.ts +7 -0
  102. package/dist/commands/update.d.ts.map +1 -0
  103. package/dist/commands/update.js +93 -0
  104. package/dist/commands/update.js.map +1 -0
  105. package/dist/components.d.ts +5 -0
  106. package/dist/components.d.ts.map +1 -0
  107. package/dist/components.js +3 -0
  108. package/dist/components.js.map +1 -0
  109. package/dist/config.d.ts +10 -51
  110. package/dist/config.d.ts.map +1 -1
  111. package/dist/config.js +101 -33
  112. package/dist/config.js.map +1 -1
  113. package/dist/dev-compose.d.ts +17 -0
  114. package/dist/dev-compose.d.ts.map +1 -0
  115. package/dist/dev-compose.js +374 -0
  116. package/dist/dev-compose.js.map +1 -0
  117. package/dist/diff-output.d.ts +4 -0
  118. package/dist/diff-output.d.ts.map +1 -0
  119. package/dist/diff-output.js +12 -0
  120. package/dist/diff-output.js.map +1 -0
  121. package/dist/docker-postgres.d.ts +57 -0
  122. package/dist/docker-postgres.d.ts.map +1 -0
  123. package/dist/docker-postgres.js +208 -0
  124. package/dist/docker-postgres.js.map +1 -0
  125. package/dist/engine-client.d.ts +69 -0
  126. package/dist/engine-client.d.ts.map +1 -0
  127. package/dist/engine-client.js +157 -0
  128. package/dist/engine-client.js.map +1 -0
  129. package/dist/ensure-binary.d.ts +7 -0
  130. package/dist/ensure-binary.d.ts.map +1 -0
  131. package/dist/ensure-binary.js +17 -0
  132. package/dist/ensure-binary.js.map +1 -0
  133. package/dist/functions-router-gen.d.ts +14 -0
  134. package/dist/functions-router-gen.d.ts.map +1 -0
  135. package/dist/functions-router-gen.js +199 -0
  136. package/dist/functions-router-gen.js.map +1 -0
  137. package/dist/index.d.ts +4 -5
  138. package/dist/index.d.ts.map +1 -1
  139. package/dist/index.js +2 -3
  140. package/dist/index.js.map +1 -1
  141. package/dist/kong-config.d.ts +25 -0
  142. package/dist/kong-config.d.ts.map +1 -0
  143. package/dist/kong-config.js +71 -0
  144. package/dist/kong-config.js.map +1 -0
  145. package/dist/local-gateway.d.ts +7 -0
  146. package/dist/local-gateway.d.ts.map +1 -0
  147. package/dist/local-gateway.js +9 -0
  148. package/dist/local-gateway.js.map +1 -0
  149. package/dist/local-storage.d.ts +8 -0
  150. package/dist/local-storage.d.ts.map +1 -0
  151. package/dist/local-storage.js +14 -0
  152. package/dist/local-storage.js.map +1 -0
  153. package/dist/pgbouncer-userlist.d.ts +5 -0
  154. package/dist/pgbouncer-userlist.d.ts.map +1 -0
  155. package/dist/pgbouncer-userlist.js +14 -0
  156. package/dist/pgbouncer-userlist.js.map +1 -0
  157. package/dist/postgres-ctl.d.ts +44 -0
  158. package/dist/postgres-ctl.d.ts.map +1 -0
  159. package/dist/postgres-ctl.js +137 -0
  160. package/dist/postgres-ctl.js.map +1 -0
  161. package/dist/process-manager.d.ts +43 -0
  162. package/dist/process-manager.d.ts.map +1 -0
  163. package/dist/process-manager.js +135 -0
  164. package/dist/process-manager.js.map +1 -0
  165. package/dist/project-config.d.ts +235 -0
  166. package/dist/project-config.d.ts.map +1 -0
  167. package/dist/project-config.js +160 -0
  168. package/dist/project-config.js.map +1 -0
  169. package/dist/pull-utils.d.ts +15 -0
  170. package/dist/pull-utils.d.ts.map +1 -1
  171. package/dist/pull-utils.js +12 -0
  172. package/dist/pull-utils.js.map +1 -1
  173. package/dist/release-pins.d.ts +7 -0
  174. package/dist/release-pins.d.ts.map +1 -0
  175. package/dist/release-pins.js +27 -0
  176. package/dist/release-pins.js.map +1 -0
  177. package/dist/release-public-key.d.ts +8 -0
  178. package/dist/release-public-key.d.ts.map +1 -0
  179. package/dist/release-public-key.js +13 -0
  180. package/dist/release-public-key.js.map +1 -0
  181. package/dist/runtime-routes.d.ts +34 -0
  182. package/dist/runtime-routes.d.ts.map +1 -0
  183. package/dist/runtime-routes.js +252 -0
  184. package/dist/runtime-routes.js.map +1 -0
  185. package/dist/schema-ast-v2.d.ts +127 -0
  186. package/dist/schema-ast-v2.d.ts.map +1 -0
  187. package/dist/schema-ast-v2.js +226 -0
  188. package/dist/schema-ast-v2.js.map +1 -0
  189. package/dist/scripts/postinstall.d.ts +5 -6
  190. package/dist/scripts/postinstall.d.ts.map +1 -1
  191. package/dist/scripts/postinstall.js +36 -20
  192. package/dist/scripts/postinstall.js.map +1 -1
  193. package/dist/self-host-compose.d.ts +22 -0
  194. package/dist/self-host-compose.d.ts.map +1 -0
  195. package/dist/self-host-compose.js +347 -0
  196. package/dist/self-host-compose.js.map +1 -0
  197. package/dist/storage-provision.d.ts +24 -0
  198. package/dist/storage-provision.d.ts.map +1 -0
  199. package/dist/storage-provision.js +44 -0
  200. package/dist/storage-provision.js.map +1 -0
  201. package/dist/studio-admin-roles.d.ts +7 -0
  202. package/dist/studio-admin-roles.d.ts.map +1 -0
  203. package/dist/studio-admin-roles.js +14 -0
  204. package/dist/studio-admin-roles.js.map +1 -0
  205. package/dist/studio-dev-server.d.ts +22 -0
  206. package/dist/studio-dev-server.d.ts.map +1 -0
  207. package/dist/studio-dev-server.js +28 -0
  208. package/dist/studio-dev-server.js.map +1 -0
  209. package/dist/systemd.d.ts +26 -0
  210. package/dist/systemd.d.ts.map +1 -0
  211. package/dist/systemd.js +102 -0
  212. package/dist/systemd.js.map +1 -0
  213. package/dist/tsx-runner.d.ts.map +1 -1
  214. package/dist/tsx-runner.js +9 -2
  215. package/dist/tsx-runner.js.map +1 -1
  216. package/dist/type-extractor.d.ts +4 -0
  217. package/dist/type-extractor.d.ts.map +1 -0
  218. package/dist/type-extractor.js +1213 -0
  219. package/dist/type-extractor.js.map +1 -0
  220. package/dist/type-resolver.d.ts +33 -0
  221. package/dist/type-resolver.d.ts.map +1 -0
  222. package/dist/type-resolver.js +338 -0
  223. package/dist/type-resolver.js.map +1 -0
  224. package/package.json +4 -3
  225. package/releases/deno/VERSION +1 -0
  226. package/scripts/mirror-deno-release.sh +76 -0
  227. package/src/TYPE-RESOLUTION.md +294 -0
  228. package/src/app/proxy-dev-app.ts +67 -0
  229. package/src/app-config.ts +128 -0
  230. package/src/augmentation-generator.ts +126 -0
  231. package/src/binary-cache.ts +822 -0
  232. package/src/cli.ts +13 -8
  233. package/src/commands/admin.ts +4 -3
  234. package/src/commands/app.ts +67 -231
  235. package/src/commands/cache.ts +117 -0
  236. package/src/commands/cloud.ts +63 -64
  237. package/src/commands/db.ts +54 -63
  238. package/src/commands/deploy.ts +96 -62
  239. package/src/commands/dev.ts +933 -405
  240. package/src/commands/diff.ts +31 -29
  241. package/src/commands/engine.ts +13 -116
  242. package/src/commands/functions.ts +97 -115
  243. package/src/commands/generate.ts +23 -10
  244. package/src/commands/init.ts +149 -414
  245. package/src/commands/migrate-from-v1.ts +131 -0
  246. package/src/commands/migrate.ts +27 -23
  247. package/src/commands/pg.ts +133 -0
  248. package/src/commands/pull.ts +6 -85
  249. package/src/commands/push.ts +161 -56
  250. package/src/commands/seed.ts +54 -12
  251. package/src/commands/self-host.ts +312 -880
  252. package/src/commands/self-update.ts +45 -0
  253. package/src/commands/status.ts +4 -3
  254. package/src/commands/types.ts +76 -0
  255. package/src/commands/update.ts +109 -0
  256. package/src/components.ts +6 -0
  257. package/src/config.ts +127 -94
  258. package/src/dev-compose.ts +455 -0
  259. package/src/diff-output.ts +12 -0
  260. package/src/docker-postgres.ts +295 -0
  261. package/src/engine-client.ts +236 -0
  262. package/src/ensure-binary.ts +28 -0
  263. package/src/functions-router-gen.ts +224 -0
  264. package/src/index.ts +4 -12
  265. package/src/kong-config.ts +93 -0
  266. package/src/local-gateway.ts +9 -0
  267. package/src/local-storage.ts +14 -0
  268. package/src/pgbouncer-userlist.ts +15 -0
  269. package/src/postgres-ctl.ts +171 -0
  270. package/src/process-manager.ts +168 -0
  271. package/src/project-config.ts +386 -0
  272. package/src/pull-utils.ts +24 -0
  273. package/src/release-pins.ts +31 -0
  274. package/src/release-public-key.ts +12 -0
  275. package/src/runtime-routes.ts +291 -0
  276. package/src/schema-ast-v2.ts +324 -0
  277. package/src/scripts/postinstall.ts +36 -25
  278. package/src/self-host-compose.ts +389 -0
  279. package/src/storage-provision.ts +58 -0
  280. package/src/studio-admin-roles.ts +16 -0
  281. package/src/studio-dev-server.ts +53 -0
  282. package/src/systemd.ts +137 -0
  283. package/src/tsx-runner.ts +11 -1
  284. package/src/type-extractor.ts +1479 -0
  285. package/src/type-resolver.ts +457 -0
  286. package/tests/app-command.test.ts +54 -0
  287. package/tests/augmentation-generator.test.ts +59 -0
  288. package/tests/binary-cache-cloud-overrides.test.ts +123 -0
  289. package/tests/cached-artifact-format.test.ts +84 -0
  290. package/tests/cli-help.test.ts +40 -14
  291. package/tests/config.test.ts +171 -37
  292. package/tests/docker-postgres.test.ts +39 -0
  293. package/tests/engine-distribution.test.ts +3 -3
  294. package/tests/ensure-binary.test.ts +59 -0
  295. package/tests/init.test.ts +28 -86
  296. package/tests/migrate-from-v1.test.ts +29 -0
  297. package/tests/normalize-admin-config.test.ts +48 -0
  298. package/tests/pg-spawn-env.test.ts +18 -0
  299. package/tests/postgres-archive-tag.test.ts +9 -0
  300. package/tests/proxy-dev-app.test.ts +33 -0
  301. package/tests/pull-utils.test.ts +36 -1
  302. package/tests/release-pins.test.ts +28 -0
  303. package/tests/runtime-contract.test.ts +351 -0
  304. package/tests/seed-discover.test.ts +31 -0
  305. package/tests/studio-admin-roles.test.ts +27 -0
  306. package/tests/tsconfig.json +9 -0
  307. package/tests/type-extractor.test.ts +985 -0
  308. package/tests/type-resolver.test.ts +59 -0
  309. package/tsconfig.tsbuildinfo +1 -1
  310. package/vitest.config.ts +12 -0
  311. package/dist/engine/cache.d.ts +0 -37
  312. package/dist/engine/cache.d.ts.map +0 -1
  313. package/dist/engine/cache.js +0 -121
  314. package/dist/engine/cache.js.map +0 -1
  315. package/dist/engine/download.d.ts +0 -19
  316. package/dist/engine/download.d.ts.map +0 -1
  317. package/dist/engine/download.js +0 -108
  318. package/dist/engine/download.js.map +0 -1
  319. package/dist/engine/platform.d.ts +0 -24
  320. package/dist/engine/platform.d.ts.map +0 -1
  321. package/dist/engine/platform.js +0 -50
  322. package/dist/engine/platform.js.map +0 -1
  323. package/dist/engine/resolve.d.ts +0 -37
  324. package/dist/engine/resolve.d.ts.map +0 -1
  325. package/dist/engine/resolve.js +0 -133
  326. package/dist/engine/resolve.js.map +0 -1
  327. package/dist/engine/update-notify.d.ts +0 -11
  328. package/dist/engine/update-notify.d.ts.map +0 -1
  329. package/dist/engine/update-notify.js +0 -43
  330. package/dist/engine/update-notify.js.map +0 -1
  331. package/dist/engine/verify.d.ts +0 -50
  332. package/dist/engine/verify.d.ts.map +0 -1
  333. package/dist/engine/verify.js +0 -161
  334. package/dist/engine/verify.js.map +0 -1
  335. package/dist/engine-version.d.ts +0 -35
  336. package/dist/engine-version.d.ts.map +0 -1
  337. package/dist/engine-version.js +0 -35
  338. package/dist/engine-version.js.map +0 -1
  339. package/dist/engine.d.ts +0 -34
  340. package/dist/engine.d.ts.map +0 -1
  341. package/dist/engine.js +0 -76
  342. package/dist/engine.js.map +0 -1
  343. package/src/engine/cache.ts +0 -135
  344. package/src/engine/download.ts +0 -143
  345. package/src/engine/platform.ts +0 -66
  346. package/src/engine/resolve.ts +0 -197
  347. package/src/engine/update-notify.ts +0 -50
  348. package/src/engine/verify.ts +0 -206
  349. package/src/engine-version.ts +0 -39
  350. package/src/engine.ts +0 -99
@@ -0,0 +1,168 @@
1
+ /**
2
+ * ProcessManager — spawn a child process, write its PID, stream logs with a
3
+ * colored prefix, and restart on crash with exponential backoff.
4
+ */
5
+
6
+ import { type ChildProcess, spawn } from "node:child_process"
7
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"
8
+ import { unlink } from "node:fs/promises"
9
+ import { join } from "node:path"
10
+
11
+ export interface ProcessOptions {
12
+ /** Human-readable label (used in log prefix and PID filename). */
13
+ label: string
14
+ /** Directory to write {label}.pid to. */
15
+ pidDir: string
16
+ /** ANSI colour prefix string (e.g. "\x1b[36m"). Pass "" for no colour. */
17
+ colour?: string
18
+ /** Working directory for the spawned process. Defaults to process.cwd(). */
19
+ cwd?: string
20
+ /** Environment variables to merge with process.env. */
21
+ env?: Record<string, string>
22
+ /** Initial restart backoff in ms. Doubles each crash up to maxBackoffMs. */
23
+ initialBackoffMs?: number
24
+ /** Maximum restart backoff cap in ms. */
25
+ maxBackoffMs?: number
26
+ /** Called when the process exits cleanly (code 0). */
27
+ onExit?: () => void
28
+ /** Use shell to spawn (required for pnpm/npm/yarn .cmd shims on Windows). */
29
+ shell?: boolean
30
+ }
31
+
32
+ const RESET = "\x1b[0m"
33
+
34
+ export class ProcessManager {
35
+ private child: ChildProcess | null = null
36
+ private stopped = false
37
+ private backoffMs: number
38
+ private opts: Required<ProcessOptions>
39
+
40
+ constructor(
41
+ private readonly bin: string,
42
+ private readonly args: string[],
43
+ opts: ProcessOptions,
44
+ ) {
45
+ this.opts = {
46
+ colour: "\x1b[36m",
47
+ cwd: process.cwd(),
48
+ env: {},
49
+ initialBackoffMs: 1_000,
50
+ maxBackoffMs: 30_000,
51
+ onExit: () => {},
52
+ shell: false,
53
+ ...opts,
54
+ }
55
+ this.backoffMs = this.opts.initialBackoffMs
56
+ }
57
+
58
+ /** Start the process. Returns immediately — the process runs in the background. */
59
+ start(): void {
60
+ this.stopped = false
61
+ this.spawn()
62
+ }
63
+
64
+ /** Stop the process and clear the PID file. */
65
+ async stop(): Promise<void> {
66
+ this.stopped = true
67
+ if (this.child && !this.child.killed) {
68
+ this.child.kill("SIGTERM")
69
+ // Give it 5s to exit gracefully, then SIGKILL.
70
+ await new Promise<void>((resolve) => {
71
+ const timeout = setTimeout(() => {
72
+ this.child?.kill("SIGKILL")
73
+ resolve()
74
+ }, 5_000)
75
+ this.child!.once("exit", () => {
76
+ clearTimeout(timeout)
77
+ resolve()
78
+ })
79
+ })
80
+ }
81
+ await this.clearPid()
82
+ }
83
+
84
+ private spawn(): void {
85
+ if (this.stopped) return
86
+
87
+ const env = { ...process.env, ...this.opts.env } as NodeJS.ProcessEnv
88
+ this.child = spawn(this.bin, this.args, {
89
+ env,
90
+ cwd: this.opts.cwd,
91
+ stdio: "pipe",
92
+ ...(this.opts.shell ? { shell: true } : {}),
93
+ })
94
+
95
+ const pid = this.child.pid
96
+ if (pid) this.writePid(pid)
97
+
98
+ const prefix = this.opts.colour
99
+ ? `${this.opts.colour}[${this.opts.label}]${RESET} `
100
+ : `[${this.opts.label}] `
101
+
102
+ this.child.stdout?.on("data", (chunk: Buffer) => {
103
+ for (const line of chunk.toString().split("\n")) {
104
+ if (line) process.stdout.write(prefix + line + "\n")
105
+ }
106
+ })
107
+
108
+ this.child.stderr?.on("data", (chunk: Buffer) => {
109
+ for (const line of chunk.toString().split("\n")) {
110
+ if (line) process.stderr.write(prefix + line + "\n")
111
+ }
112
+ })
113
+
114
+ this.child.once("error", (err) => {
115
+ if (this.stopped) return
116
+ process.stderr.write(`${prefix}failed to start: ${err.message}\n`)
117
+ setTimeout(() => {
118
+ this.backoffMs = Math.min(this.backoffMs * 2, this.opts.maxBackoffMs)
119
+ this.spawn()
120
+ }, this.backoffMs)
121
+ })
122
+
123
+ this.child.once("exit", (code, signal) => {
124
+ if (this.stopped) return
125
+
126
+ if (code === 0) {
127
+ this.opts.onExit()
128
+ return
129
+ }
130
+
131
+ const reason = signal ? `signal ${signal}` : `code ${code}`
132
+ process.stderr.write(
133
+ `${prefix}process exited (${reason}), restarting in ${this.backoffMs}ms\n`,
134
+ )
135
+
136
+ setTimeout(() => {
137
+ this.backoffMs = Math.min(this.backoffMs * 2, this.opts.maxBackoffMs)
138
+ this.spawn()
139
+ }, this.backoffMs)
140
+ })
141
+ }
142
+
143
+ private writePid(pid: number): void {
144
+ try {
145
+ mkdirSync(this.opts.pidDir, { recursive: true })
146
+ writeFileSync(join(this.opts.pidDir, `${this.opts.label}.pid`), String(pid))
147
+ } catch {
148
+ // Non-fatal — PID tracking is best-effort.
149
+ }
150
+ }
151
+
152
+ private async clearPid(): Promise<void> {
153
+ try {
154
+ await unlink(join(this.opts.pidDir, `${this.opts.label}.pid`))
155
+ } catch {
156
+ // Ignore — file may already be gone.
157
+ }
158
+ }
159
+ }
160
+
161
+ /** Read a PID from a file (returns null if not found or stale). */
162
+ export function readPid(pidDir: string, label: string): number | null {
163
+ const pidFile = join(pidDir, `${label}.pid`)
164
+ if (!existsSync(pidFile)) return null
165
+ const raw = readFileSync(pidFile, "utf8").trim()
166
+ const pid = Number(raw)
167
+ return Number.isFinite(pid) && pid > 0 ? pid : null
168
+ }
@@ -0,0 +1,386 @@
1
+ import { existsSync } from "node:fs"
2
+ import { resolve } from "node:path"
3
+ import type { ComponentVersions } from "./components.js"
4
+
5
+ // ---------------------------------------------------------------------------
6
+ // Config schema (single canonical shape; loaded from supatype.config.ts)
7
+ // ---------------------------------------------------------------------------
8
+
9
+ export interface SupatypeProjectConfig {
10
+ /**
11
+ * Runtime stack for local dev and `supatype update`.
12
+ * "native" = host binaries (default). "docker" = self-host Compose stack.
13
+ * Falls back to `database.provider` when omitted (deprecated).
14
+ */
15
+ provider?: "native" | "docker"
16
+ supatype?: {
17
+ /**
18
+ * Base directory for Supatype project assets (schema, functions, etc).
19
+ * "." means the current working directory (default).
20
+ */
21
+ root?: string
22
+ }
23
+ project: {
24
+ /** Project name — used for per-project state dirs and logging. */
25
+ name: string
26
+ /** Cloud project reference (set by `supatype link`). */
27
+ ref?: string
28
+ }
29
+ database: {
30
+ /**
31
+ * Database backend.
32
+ * "native" = supatype manages a native Postgres binary (downloaded from CDN).
33
+ * "docker" = supatype runs supatype/postgres via Docker (includes all extensions).
34
+ */
35
+ provider: "native" | "docker"
36
+ /**
37
+ * Directory where Postgres stores its data files (provider=native).
38
+ * Defaults to ~/.supatype/projects/{name}/data when omitted.
39
+ */
40
+ data_dir?: string
41
+ /**
42
+ * Docker image to use (provider=docker).
43
+ * Defaults to supatype/postgres:latest.
44
+ * Override in supatype.local.config.ts for local builds.
45
+ */
46
+ image?: string
47
+ }
48
+ server: {
49
+ /**
50
+ * Server mode.
51
+ * "dev" = no TLS, permissive CORS, Vite HMR proxy
52
+ * "standalone" = ACME TLS (Let's Encrypt)
53
+ * "managed" = cloud-managed, HMAC tenant verification
54
+ */
55
+ mode: "dev" | "standalone" | "managed"
56
+ /** Port supatype-server listens on (default: 54321). */
57
+ port?: number
58
+ /** Port PostgREST listens on in local dev (default: 3001). */
59
+ postgrestPort?: number
60
+ /** Domain for ACME TLS certificate (mode=standalone). */
61
+ domain?: string
62
+ }
63
+ app: {
64
+ /**
65
+ * How the root path "/" is handled by supatype-server.
66
+ * "none" = 404
67
+ * "static" = serve files from static_dir
68
+ * "proxy" = reverse-proxy to upstream
69
+ */
70
+ mode: "none" | "static" | "proxy"
71
+ /** Directory to serve static files from (mode=static). */
72
+ static_dir?: string
73
+ /** Upstream URL to proxy to (mode=proxy). */
74
+ upstream?: string
75
+ /**
76
+ * Vite dev server base URL for HMR (`/_vite/*`) when `server.mode` is dev.
77
+ * Example: `http://127.0.0.1:5173`. Sets `SUPATYPE_VITE_DEV_URL` for supatype-server.
78
+ * When omitted, dev still falls back to `SUPATYPE_APP_UPSTREAM` for non-proxy app modes.
79
+ */
80
+ vite_dev_url?: string
81
+ /**
82
+ * package.json script name for `supatype dev` to run when mode is proxy.
83
+ * Default: `"start"`. Ignored for static/none modes.
84
+ */
85
+ start?: string
86
+ }
87
+ /**
88
+ * Pinned binary versions per component. Use **`"local"`** with the matching **`overrides.*`**
89
+ * entry when testing a local build (Phase 10.7).
90
+ */
91
+ versions: ComponentVersions
92
+ /**
93
+ * Override component binaries with local build paths.
94
+ * Intended for supatype contributors testing local changes.
95
+ * Cannot be combined with a linked cloud project (`project.ref`, `.supatype/cloud.json`, or `.supatype/linked.json`; hard error in `resolveBinary`).
96
+ */
97
+ overrides?: {
98
+ /** Path to local engine binary. */
99
+ engine?: string
100
+ /** Path to local supatype-server binary. */
101
+ server?: string
102
+ /** Path to a directory containing a local Postgres installation. */
103
+ postgres_dir?: string
104
+ /** Path to a local deno binary. */
105
+ deno?: string
106
+ /** Path to the @supatype/studio package directory (starts Vite dev server). */
107
+ studio?: string
108
+ /** Path to a local PostgREST binary. */
109
+ postgrest?: string
110
+ }
111
+ email?: {
112
+ /**
113
+ * Email delivery provider.
114
+ * "console" = log to stdout (default for dev)
115
+ * "smtp" = SMTP (set `smtp` below and/or GOTRUE_SMTP_* in `.env`)
116
+ * "resend" = Resend API (requires RESEND_API_KEY, RESEND_FROM)
117
+ * "ses" = AWS SES v2 (ambient credentials, requires SES_FROM)
118
+ */
119
+ provider: "console" | "smtp" | "resend" | "ses"
120
+ /**
121
+ * SMTP settings for provider=smtp (merged into process env as GOTRUE_SMTP_*).
122
+ * Omitted keys can still be set via `.env` / shell.
123
+ */
124
+ smtp?: {
125
+ host?: string
126
+ port?: number
127
+ user?: string
128
+ pass?: string
129
+ admin_email?: string
130
+ sender_name?: string
131
+ }
132
+ /** Resend API key (provider=resend, or set RESEND_API_KEY env var). */
133
+ resend_api_key?: string
134
+ /** From address for Resend (provider=resend, or set RESEND_FROM env var). */
135
+ resend_from?: string
136
+ /** From address for SES (provider=ses, or set SES_FROM env var). */
137
+ ses_from?: string
138
+ /**
139
+ * When true, `supatype dev` enables the GoTrue send-email HTTP hook pointing at this
140
+ * server's POST `/internal/v0hooks/send-email` (signed delivery, dev-only secret).
141
+ * Override `GOTRUE_HOOK_SEND_EMAIL_*` in `.env` if needed.
142
+ */
143
+ send_email_hook?: boolean
144
+ /**
145
+ * Override hook target URL when `send_email_hook` is true (e.g. HTTPS tunnel or Edge URL).
146
+ * Default: `http://127.0.0.1:<serverPort>/internal/v0hooks/send-email`.
147
+ */
148
+ send_email_hook_uri?: string
149
+ /**
150
+ * Standard Webhooks v1 secrets for the send-email hook (`v1,whsec_...`, pipe-separated for rotation).
151
+ * Default in dev: a fixed local secret; override for team-shared dev or CI.
152
+ */
153
+ send_email_hook_secrets?: string
154
+ }
155
+ storage?: {
156
+ /**
157
+ * Storage backend.
158
+ * "local" = files on disk (LocalStoragePath required)
159
+ * "s3" = AWS S3 or compatible (ambient credentials)
160
+ */
161
+ provider: "local" | "s3"
162
+ /** Local directory to store objects in (provider=local). */
163
+ local_path?: string
164
+ }
165
+ schema?: {
166
+ /** Path (or glob) to the schema entry point. Defaults to "schema/index.ts". */
167
+ path?: string
168
+ /** Postgres schema name. Defaults to "public". */
169
+ pg_schema?: string
170
+ }
171
+ functions?: {
172
+ /** Path to edge functions directory, relative to `supatype.root` when not absolute. */
173
+ path?: string
174
+ }
175
+ output?: {
176
+ /** Path for generated TypeScript types. */
177
+ types?: string
178
+ /** Path for generated client helpers. */
179
+ client?: string
180
+ }
181
+ /**
182
+ * App build configuration for `supatype deploy`.
183
+ * Separate from `app` which controls how supatype-server serves at runtime.
184
+ */
185
+ build?: {
186
+ /** Framework name. Auto-detected from package.json when omitted. */
187
+ framework?: "nextjs" | "astro" | "vite" | "remix-spa" | "sveltekit" | "nuxt" | "static"
188
+ /** Path to the app directory. Defaults to cwd. */
189
+ directory?: string
190
+ /** Build command. Inferred from framework when omitted. */
191
+ buildCommand?: string
192
+ /** Build output directory. Inferred from framework when omitted. */
193
+ outputDirectory?: string
194
+ /** Enable SPA fallback routing. */
195
+ spa?: boolean
196
+ /** Environment variables injected at build time. */
197
+ env?: Record<string, string>
198
+ /** Custom response headers for the deployed static site. */
199
+ headers?: Record<string, string>
200
+ }
201
+ /**
202
+ * Optional Postgres URL for CLI commands that talk to the DB (`push`, `migrate`, …).
203
+ * When omitted, `DATABASE_URL` from the environment is used, then a local default DSN.
204
+ */
205
+ connection?: string
206
+ /** Studio admin panel access (Gap Appendices task 47). */
207
+ admin?: {
208
+ /** JWT `app_metadata.role` values allowed to use Studio. Default: admin, supatype_admin */
209
+ roles?: string[]
210
+ }
211
+ }
212
+
213
+ // ---------------------------------------------------------------------------
214
+ // Merge + validate
215
+ // ---------------------------------------------------------------------------
216
+
217
+ /**
218
+ * Merge each top-level section from `override` on top of `base`.
219
+ * Within each section, override values win. New optional sections in override are added.
220
+ */
221
+ export function mergeProjectConfig(
222
+ base: SupatypeProjectConfig,
223
+ override: Partial<SupatypeProjectConfig>,
224
+ ): SupatypeProjectConfig {
225
+ return {
226
+ ...(base.provider !== undefined || override.provider !== undefined
227
+ ? { provider: override.provider ?? base.provider }
228
+ : {}),
229
+ ...(base.supatype !== undefined || override.supatype !== undefined
230
+ ? { supatype: { ...base.supatype, ...override.supatype } as NonNullable<SupatypeProjectConfig["supatype"]> }
231
+ : {}),
232
+ project: { ...base.project, ...override.project },
233
+ database: { ...base.database, ...override.database },
234
+ server: { ...base.server, ...override.server },
235
+ app: { ...base.app, ...override.app },
236
+ versions: { ...base.versions, ...override.versions },
237
+ ...(base.overrides !== undefined || override.overrides !== undefined
238
+ ? {
239
+ overrides: {
240
+ ...base.overrides,
241
+ ...override.overrides,
242
+ } as NonNullable<SupatypeProjectConfig["overrides"]>,
243
+ }
244
+ : {}),
245
+ ...(base.email !== undefined || override.email !== undefined
246
+ ? (() => {
247
+ const b = base.email
248
+ const o = override.email
249
+ const mergedSmtp =
250
+ b?.smtp !== undefined || o?.smtp !== undefined
251
+ ? { ...(b?.smtp ?? {}), ...(o?.smtp ?? {}) }
252
+ : undefined
253
+ return {
254
+ email: {
255
+ ...b,
256
+ ...o,
257
+ ...(mergedSmtp !== undefined ? { smtp: mergedSmtp } : {}),
258
+ } as NonNullable<SupatypeProjectConfig["email"]>,
259
+ }
260
+ })()
261
+ : {}),
262
+ ...(base.storage !== undefined || override.storage !== undefined
263
+ ? {
264
+ storage: {
265
+ ...base.storage,
266
+ ...override.storage,
267
+ } as NonNullable<SupatypeProjectConfig["storage"]>,
268
+ }
269
+ : {}),
270
+ ...(base.schema !== undefined || override.schema !== undefined
271
+ ? { schema: { ...base.schema, ...override.schema } as NonNullable<SupatypeProjectConfig["schema"]> }
272
+ : {}),
273
+ ...(base.functions !== undefined || override.functions !== undefined
274
+ ? { functions: { ...base.functions, ...override.functions } as NonNullable<SupatypeProjectConfig["functions"]> }
275
+ : {}),
276
+ ...(base.output !== undefined || override.output !== undefined
277
+ ? { output: { ...base.output, ...override.output } as NonNullable<SupatypeProjectConfig["output"]> }
278
+ : {}),
279
+ ...(base.build !== undefined || override.build !== undefined
280
+ ? { build: { ...base.build, ...override.build } as NonNullable<SupatypeProjectConfig["build"]> }
281
+ : {}),
282
+ ...(base.connection !== undefined || override.connection !== undefined
283
+ ? { connection: override.connection ?? base.connection }
284
+ : {}),
285
+ ...(base.admin !== undefined || override.admin !== undefined
286
+ ? { admin: { ...base.admin, ...override.admin } as NonNullable<SupatypeProjectConfig["admin"]> }
287
+ : {}),
288
+ }
289
+ }
290
+
291
+ export function validateProjectConfig(raw: unknown, filename: string): SupatypeProjectConfig {
292
+ if (typeof raw !== "object" || raw === null) {
293
+ throw new Error(`${filename}: expected a config object at the root`)
294
+ }
295
+
296
+ const cfg = raw as Record<string, unknown>
297
+
298
+ if (!cfg["project"] || typeof (cfg["project"] as Record<string, unknown>)["name"] !== "string") {
299
+ throw new Error(`${filename}: project.name is required`)
300
+ }
301
+ if (!cfg["database"]) {
302
+ throw new Error(`${filename}: database section is required`)
303
+ }
304
+ if (!cfg["server"]) {
305
+ throw new Error(`${filename}: server section is required`)
306
+ }
307
+ if (!cfg["app"]) {
308
+ throw new Error(`${filename}: app section is required`)
309
+ }
310
+ if (!cfg["versions"]) {
311
+ throw new Error(`${filename}: versions section is required`)
312
+ }
313
+
314
+ return raw as SupatypeProjectConfig
315
+ }
316
+
317
+ /** Schema entry path (with fallback). */
318
+ export function schemaPathFromProject(cfg: SupatypeProjectConfig, cwd: string): string {
319
+ return resolve(projectRootFromConfig(cfg, cwd), cfg.schema?.path ?? "schema/index.ts")
320
+ }
321
+
322
+ /** Resolve project root for schema/functions defaults. */
323
+ export function projectRootFromConfig(cfg: SupatypeProjectConfig, cwd: string): string {
324
+ return resolve(cwd, cfg.supatype?.root ?? ".")
325
+ }
326
+
327
+ /** Candidate functions directories in lookup order. */
328
+ export function functionsPathCandidatesFromProject(cfg: SupatypeProjectConfig, cwd: string): string[] {
329
+ const root = projectRootFromConfig(cfg, cwd)
330
+ if (cfg.functions?.path) {
331
+ return [resolve(root, cfg.functions.path)]
332
+ }
333
+ // Prefer modern default, but keep legacy fallback for compatibility.
334
+ return [resolve(root, "functions"), resolve(root, "supatype/functions")]
335
+ }
336
+
337
+ /** Preferred default functions path (used when creating new functions). */
338
+ export function preferredFunctionsPathFromProject(cfg: SupatypeProjectConfig, cwd: string): string {
339
+ const candidates = functionsPathCandidatesFromProject(cfg, cwd)
340
+ for (const dir of candidates) {
341
+ if (existsSync(dir)) return dir
342
+ }
343
+ return candidates[0] ?? resolve(projectRootFromConfig(cfg, cwd), "functions")
344
+ }
345
+
346
+ /**
347
+ * Derive the supatype-server base URL from the project config.
348
+ * Returns undefined if the mode is "managed" (cloud controls the URL).
349
+ */
350
+ export function serverBaseUrl(cfg: SupatypeProjectConfig): string | undefined {
351
+ const port = cfg.server.port ?? 54321
352
+ switch (cfg.server.mode) {
353
+ case "dev":
354
+ case "standalone":
355
+ if (cfg.server.mode === "dev" && resolveRuntimeProvider(cfg) === "docker") {
356
+ return `http://localhost:${COMPOSE_DEV_KONG_PORT}`
357
+ }
358
+ return cfg.server.domain
359
+ ? `https://${cfg.server.domain}`
360
+ : `http://localhost:${port}`
361
+ case "managed":
362
+ return undefined
363
+ }
364
+ }
365
+
366
+ /** Resolved runtime provider (`config.provider` ?? `database.provider` ?? native). */
367
+ export function resolveRuntimeProvider(cfg: SupatypeProjectConfig): "native" | "docker" {
368
+ return cfg.provider ?? cfg.database.provider ?? "native"
369
+ }
370
+
371
+ /** Kong gateway port when `provider: docker` (self-host compose dev). */
372
+ export const COMPOSE_DEV_KONG_PORT = 18473
373
+
374
+ /** The local Postgres DSN derived from project name (dev default). */
375
+ export function localDSN(cfg: SupatypeProjectConfig): string {
376
+ const port = 5432 // standard; per-project state dir isolates data dirs
377
+ return `postgres://postgres:postgres@127.0.0.1:${port}/${cfg.project.name}?sslmode=disable`
378
+ }
379
+
380
+ /**
381
+ * Resolve the database connection string.
382
+ * Prefers optional `connection` in config, then `DATABASE_URL` env, then a local default DSN.
383
+ */
384
+ export function connectionString(cfg: SupatypeProjectConfig): string {
385
+ return cfg.connection ?? process.env["DATABASE_URL"] ?? localDSN(cfg)
386
+ }
package/src/pull-utils.ts CHANGED
@@ -11,6 +11,30 @@ export interface ColumnInfo {
11
11
  hasDefault: boolean
12
12
  }
13
13
 
14
+ /** Engine `/introspect` column shape (see {@link IntrospectResult} in engine-client). */
15
+ export interface IntrospectColumn {
16
+ name: string
17
+ type: string
18
+ nullable: boolean
19
+ default?: string
20
+ primaryKey?: boolean
21
+ unique?: boolean
22
+ references?: { table: string; column: string }
23
+ }
24
+
25
+ /** Map engine introspection JSON to {@link ColumnInfo} for {@link pgTypeToField}. */
26
+ export function introspectColumnToColumnInfo(col: IntrospectColumn): ColumnInfo {
27
+ const def = col.default
28
+ return {
29
+ name: col.name,
30
+ pgType: col.type,
31
+ nullable: col.nullable,
32
+ isPrimary: col.primaryKey ?? false,
33
+ isUnique: col.unique ?? false,
34
+ hasDefault: def !== undefined && def !== "",
35
+ }
36
+ }
37
+
14
38
  /** Map a Postgres column type to the corresponding field.X() call string. */
15
39
  export function pgTypeToField(col: ColumnInfo): string {
16
40
  const opts: Record<string, unknown> = { required: !col.nullable }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Release pins read from files under packages/cli/releases/.
3
+ * CI (deno-releases workflow) uses the same paths — bump only releases/deno/VERSION.
4
+ */
5
+
6
+ import { readFileSync } from "node:fs"
7
+ import { dirname, join } from "node:path"
8
+ import { fileURLToPath } from "node:url"
9
+
10
+ const CLI_PACKAGE_ROOT = join(dirname(fileURLToPath(import.meta.url)), "..")
11
+
12
+ function readPinFile(...segments: string[]): string {
13
+ const path = join(CLI_PACKAGE_ROOT, "releases", ...segments)
14
+ let raw: string
15
+ try {
16
+ raw = readFileSync(path, "utf8")
17
+ } catch {
18
+ throw new Error(
19
+ `Missing release pin file: ${path}\n` +
20
+ "Expected packages/cli/releases/… — reinstall @supatype/cli or build from the monorepo.",
21
+ )
22
+ }
23
+ const version = raw.trim()
24
+ if (!/^\d+\.\d+\.\d+/.test(version)) {
25
+ throw new Error(`Invalid pin in ${path}: expected semver like 2.2.0, got ${JSON.stringify(raw)}`)
26
+ }
27
+ return version
28
+ }
29
+
30
+ /** Pinned Deno runtime for edge functions (also published to CDN under /deno/v{version}/). */
31
+ export const DENO_RELEASE_PIN = readPinFile("deno", "VERSION")
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Minisign public key for CDN release verification.
3
+ * Populated at publish via scripts/embed-release-pubkey.mjs (MINISIGN_PUBLIC_KEY secret),
4
+ * or overridden at runtime with SUPATYPE_RELEASE_PUBLIC_KEY.
5
+ */
6
+ export const EMBEDDED_RELEASE_PUBLIC_KEY = "untrusted comment: minisign public key DE133E99689AE71D\nRWQd55pomT4T3t+5FLZZ6+h0DoHrtiuU9RBUHHm9BcFHeGRvW7BXxBCr"
7
+
8
+ export function releasePublicKey(): string {
9
+ const fromEnv = process.env["SUPATYPE_RELEASE_PUBLIC_KEY"]?.trim()
10
+ if (fromEnv) return fromEnv
11
+ return EMBEDDED_RELEASE_PUBLIC_KEY.trim()
12
+ }