@astrale-os/sdk 0.1.6 → 0.1.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 (103) hide show
  1. package/dist/cli/bin.d.ts +7 -0
  2. package/dist/cli/bin.d.ts.map +1 -0
  3. package/dist/cli/bin.js +15 -0
  4. package/dist/cli/bin.js.map +1 -0
  5. package/dist/cli/dotenv.d.ts +13 -0
  6. package/dist/cli/dotenv.d.ts.map +1 -0
  7. package/dist/cli/dotenv.js +46 -0
  8. package/dist/cli/dotenv.js.map +1 -0
  9. package/dist/cli/index.d.ts +15 -0
  10. package/dist/cli/index.d.ts.map +1 -0
  11. package/dist/cli/index.js +15 -0
  12. package/dist/cli/index.js.map +1 -0
  13. package/dist/cli/run.d.ts +84 -0
  14. package/dist/cli/run.d.ts.map +1 -0
  15. package/dist/cli/run.js +603 -0
  16. package/dist/cli/run.js.map +1 -0
  17. package/dist/cli/spec.d.ts +19 -0
  18. package/dist/cli/spec.d.ts.map +1 -0
  19. package/dist/cli/spec.js +31 -0
  20. package/dist/cli/spec.js.map +1 -0
  21. package/dist/config/adapter.d.ts +140 -0
  22. package/dist/config/adapter.d.ts.map +1 -0
  23. package/dist/config/adapter.js +40 -0
  24. package/dist/config/adapter.js.map +1 -0
  25. package/dist/config/define-domain.d.ts +112 -0
  26. package/dist/config/define-domain.d.ts.map +1 -0
  27. package/dist/config/define-domain.js +98 -0
  28. package/dist/config/define-domain.js.map +1 -0
  29. package/dist/config/deploy.d.ts +28 -0
  30. package/dist/config/deploy.d.ts.map +1 -0
  31. package/dist/config/deploy.js +24 -0
  32. package/dist/config/deploy.js.map +1 -0
  33. package/dist/config/index.d.ts +21 -0
  34. package/dist/config/index.d.ts.map +1 -0
  35. package/dist/config/index.js +18 -0
  36. package/dist/config/index.js.map +1 -0
  37. package/dist/define/remote-function.d.ts +19 -11
  38. package/dist/define/remote-function.d.ts.map +1 -1
  39. package/dist/define/remote-function.js.map +1 -1
  40. package/dist/dispatch/call-remote.d.ts +7 -3
  41. package/dist/dispatch/call-remote.d.ts.map +1 -1
  42. package/dist/dispatch/call-remote.js.map +1 -1
  43. package/dist/dispatch/dispatcher.d.ts.map +1 -1
  44. package/dist/dispatch/dispatcher.js +8 -4
  45. package/dist/dispatch/dispatcher.js.map +1 -1
  46. package/dist/dispatch/index.d.ts +1 -1
  47. package/dist/dispatch/index.d.ts.map +1 -1
  48. package/dist/dispatch/index.js.map +1 -1
  49. package/dist/dispatch/self.d.ts +46 -10
  50. package/dist/dispatch/self.d.ts.map +1 -1
  51. package/dist/dispatch/self.js +65 -8
  52. package/dist/dispatch/self.js.map +1 -1
  53. package/dist/domain/define.d.ts +3 -3
  54. package/dist/domain/define.js +3 -3
  55. package/dist/index.d.ts +5 -4
  56. package/dist/index.d.ts.map +1 -1
  57. package/dist/index.js +8 -2
  58. package/dist/index.js.map +1 -1
  59. package/dist/method/class.d.ts.map +1 -1
  60. package/dist/method/class.js.map +1 -1
  61. package/dist/method/context.d.ts +32 -7
  62. package/dist/method/context.d.ts.map +1 -1
  63. package/dist/method/index.d.ts +1 -1
  64. package/dist/method/index.d.ts.map +1 -1
  65. package/dist/method/single.d.ts +16 -11
  66. package/dist/method/single.d.ts.map +1 -1
  67. package/dist/method/single.js.map +1 -1
  68. package/dist/server/domain-entry.d.ts +67 -0
  69. package/dist/server/domain-entry.d.ts.map +1 -0
  70. package/dist/server/domain-entry.js +58 -0
  71. package/dist/server/domain-entry.js.map +1 -0
  72. package/dist/server/index.d.ts +3 -1
  73. package/dist/server/index.d.ts.map +1 -1
  74. package/dist/server/index.js +2 -1
  75. package/dist/server/index.js.map +1 -1
  76. package/dist/server/worker-entry.d.ts +80 -5
  77. package/dist/server/worker-entry.d.ts.map +1 -1
  78. package/dist/server/worker-entry.js +105 -24
  79. package/dist/server/worker-entry.js.map +1 -1
  80. package/package.json +12 -3
  81. package/src/cli/bin.ts +15 -0
  82. package/src/cli/dotenv.ts +45 -0
  83. package/src/cli/index.ts +15 -0
  84. package/src/cli/run.ts +710 -0
  85. package/src/cli/spec.ts +42 -0
  86. package/src/config/adapter.ts +172 -0
  87. package/src/config/define-domain.ts +218 -0
  88. package/src/config/deploy.ts +35 -0
  89. package/src/config/index.ts +31 -0
  90. package/src/define/remote-function.ts +42 -13
  91. package/src/dispatch/call-remote.ts +7 -2
  92. package/src/dispatch/dispatcher.ts +8 -4
  93. package/src/dispatch/index.ts +1 -1
  94. package/src/dispatch/self.ts +96 -10
  95. package/src/domain/define.ts +3 -3
  96. package/src/index.ts +25 -4
  97. package/src/method/class.ts +4 -3
  98. package/src/method/context.ts +38 -7
  99. package/src/method/index.ts +1 -1
  100. package/src/method/single.ts +30 -11
  101. package/src/server/domain-entry.ts +113 -0
  102. package/src/server/index.ts +3 -1
  103. package/src/server/worker-entry.ts +128 -24
package/src/cli/run.ts ADDED
@@ -0,0 +1,710 @@
1
+ /**
2
+ * The `astrale-domain` CLI — dev | build | deploy | publish.
3
+ *
4
+ * Thin by design: it reads `astrale.config.ts`, resolves `envs[<env>] → params`
5
+ * via the adapter, loads the env's secrets file, then drives `adapter.watch`
6
+ * (dev) or `adapter.deploy` (build+ship). It prints the resulting URL and the
7
+ * exact `astrale domain install <url> --direct` line. It boots no kernel and knows
8
+ * nothing provider-specific — that all lives in the adapter.
9
+ *
10
+ * The diagnostic spec (`.astrale/spec.json`) is written AFTER watch/deploy
11
+ * returns, at the live URL: the install graph (and therefore `schemaHash`)
12
+ * embeds `binding.remoteUrl`s derived from the serving URL, so a spec built at
13
+ * a guessed URL would carry a hash that never matches the worker's `/meta`.
14
+ * Only `astrale-domain build` (no URL exists yet) uses an explicit placeholder
15
+ * and says so.
16
+ *
17
+ * astrale-domain dev # = deploy dev --watch
18
+ * astrale-domain prod # = deploy prod
19
+ * astrale-domain deploy <env> # any env key
20
+ * astrale-domain build # rebuild spec only (placeholder URL)
21
+ * astrale-domain publish [env] # register the deployed URL in the admin catalog (NO deploy)
22
+ *
23
+ * `publish` registers a domain's ALREADY-deployed URL in the admin catalog by
24
+ * SHELLING OUT to the operator CLI (`astrale domain publish --origin --name
25
+ * --public-url`); it does NOT deploy — run `prod` / `deploy <env>` first, or use
26
+ * `deploy --publish` to deploy AND register in one step. Standalone publish
27
+ * defaults the registered address to `https://<origin>` (what every fleet domain
28
+ * serves at); pass `--public-url` for workers.dev / split-host deploys. Auth
29
+ * lives entirely in the operator CLI — this build tool never touches
30
+ * credentials. Requires `astrale` on PATH (the same CLI the deploy footer
31
+ * already points you at for `domain install`).
32
+ */
33
+
34
+ import { spawn } from 'node:child_process'
35
+ import { existsSync, readFileSync, watch as watchFs } from 'node:fs'
36
+ import { mkdir, writeFile } from 'node:fs/promises'
37
+ import { createRequire } from 'node:module'
38
+ import { basename, dirname, isAbsolute, join } from 'node:path'
39
+ import { pathToFileURL } from 'node:url'
40
+
41
+ import type { DeployResult, DomainAdapter, DomainInfo, WatchHandle } from '../config/adapter'
42
+ import type { DeployConfig } from '../config/deploy'
43
+
44
+ import { loadDotenvFile } from './dotenv'
45
+ import { buildProjectSpec } from './spec'
46
+
47
+ const CONFIG_NAMES = ['astrale.config.ts', 'astrale.config.js', 'astrale.config.mjs']
48
+
49
+ type ParsedArgs = {
50
+ command: 'dev' | 'build' | 'deploy' | 'publish'
51
+ env: string
52
+ watch: boolean
53
+ /** `--port <n>` (dev only) — overrides the env's local dev port. */
54
+ port?: number
55
+ /** `--host <url>` (dev only) — public URL of a tunnel/proxy front: binds 0.0.0.0 + pins WORKER_URL. */
56
+ host?: string
57
+ /** `--publish` tail-flag on deploy/prod: ALSO register the freshly-deployed URL (deploy + register). */
58
+ publish?: boolean
59
+ /** `--name <slug>` — registry name to publish under (default: the origin's first label, e.g. `ai-gateway`). */
60
+ name?: string
61
+ /** `--install-by-default` — mark the published domain for install on every new instance. */
62
+ installByDefault?: boolean
63
+ /** `--public-url <url>` — (publish only) the address the catalog points at (default: `https://<origin>`). */
64
+ publicUrl?: string
65
+ }
66
+
67
+ export async function run(argv: readonly string[]): Promise<number> {
68
+ // `--help` anywhere prints usage — previously `prod --help` IGNORED the
69
+ // flag and started a real deploy (observed in an external test run).
70
+ if (argv.includes('--help') || argv.includes('-h')) {
71
+ printUsage()
72
+ return 0
73
+ }
74
+ let parsed: ParsedArgs
75
+ try {
76
+ parsed = parseArgs(argv)
77
+ } catch (err) {
78
+ printUsage((err as Error).message)
79
+ return 2
80
+ }
81
+
82
+ // The CLI imports the project's TypeScript modules (astrale.config.ts, the ★
83
+ // files) directly — that requires a TS-native runtime. Fail with the remedy
84
+ // instead of a cryptic ERR_UNKNOWN_FILE_EXTENSION deep in `import()`.
85
+ if (!process.versions.bun) {
86
+ error(
87
+ 'astrale-domain requires Bun (it imports your astrale.config.ts directly). ' +
88
+ 'Install it from https://bun.sh, then re-run.',
89
+ )
90
+ return 1
91
+ }
92
+
93
+ const projectDir = process.cwd()
94
+ const configPath = findConfig(projectDir)
95
+ if (!configPath) {
96
+ error(`No astrale.config.ts found in ${projectDir}. Run this from a domain project root.`)
97
+ return 1
98
+ }
99
+
100
+ let def: DeployConfig
101
+ try {
102
+ def = await loadConfig(configPath)
103
+ } catch (err) {
104
+ // Adapter-swap papercut: editing the `adapter:` call without the import
105
+ // (or vice versa) surfaces as a bare ReferenceError. Name the real fix.
106
+ const m = err instanceof Error ? /^(\w+) is not defined/.exec(err.message) : null
107
+ if (m) {
108
+ error(
109
+ `astrale.config.ts references \`${m[1]}\` but never imports it.\n` +
110
+ ` If you swapped adapters (cloudflare ⇄ astrale), update BOTH lines:\n` +
111
+ ` import { ${m[1]} } from '@astrale-os/adapter-cloudflare'\n` +
112
+ ` adapter: ${m[1]}({ ... })`,
113
+ )
114
+ return 1
115
+ }
116
+ throw err
117
+ }
118
+ const specPath = join(projectDir, '.astrale', 'spec.json')
119
+
120
+ if (parsed.command === 'build') {
121
+ // Spec-only: no adapter params, no secrets, no deploy → no real URL.
122
+ // Build at an explicit placeholder and say so: the schemaHash of this
123
+ // spec will NOT match a live worker's /meta (the install graph embeds
124
+ // URL-derived bindings).
125
+ const placeholderUrl = `https://${def.origin}`
126
+ try {
127
+ await writeSpec({ projectDir, specPath, def, url: placeholderUrl })
128
+ } catch (err) {
129
+ error(`Spec build failed: ${(err as Error).message}`)
130
+ return 1
131
+ }
132
+ info(`Built spec → ${rel(projectDir, specPath)} (origin: ${def.origin})`)
133
+ info(
134
+ `note: built at placeholder ${placeholderUrl} — its schemaHash differs from a deployed worker.`,
135
+ )
136
+ return 0
137
+ }
138
+
139
+ if (parsed.command === 'publish') {
140
+ // Register the already-deployed URL in the admin catalog — NO deploy. The
141
+ // public address defaults to `https://<origin>` (the canonical custom-domain
142
+ // prod URL — what every fleet domain serves at); `--public-url` overrides it
143
+ // for workers.dev / split-host deploys. No adapter, secrets, or params: this
144
+ // only points the registry at an address the author already deployed.
145
+ const pkg = readPackageMeta(projectDir)
146
+ const name = parsed.name ?? def.origin.split('.')[0] ?? def.origin
147
+ const url = parsed.publicUrl ?? `https://${def.origin}`
148
+ info(`registering ${def.origin} → ${url} in the admin catalog (no deploy)`)
149
+ return await publishToAdmin({
150
+ origin: def.origin,
151
+ name,
152
+ url,
153
+ ...(pkg.description ? { description: pkg.description } : {}),
154
+ ...(parsed.installByDefault ? { installByDefault: true } : {}),
155
+ })
156
+ }
157
+
158
+ const adapter = def.adapter as DomainAdapter<unknown>
159
+ // CLI param overrides — applied over the env's params here AND re-applied by
160
+ // the config hot-regen path, which re-resolves `adapter.params` from the
161
+ // fresh config (without them, the first config edit would silently drop the
162
+ // flags — e.g. lose the `--host`-pinned WORKER_URL and drift the iss).
163
+ const paramOverrides: Record<string, unknown> = {}
164
+ // CLI port override beats the env's configured port (both adapters read
165
+ // `params.port` in watch).
166
+ if (parsed.command === 'dev' && parsed.port !== undefined) paramOverrides.port = parsed.port
167
+ // `--host <public-url>`: off-localhost dev (sandbox preview, tunnel). The
168
+ // adapter binds 0.0.0.0 and pins WORKER_URL so the worker's per-request-Host
169
+ // identity doesn't drift to the proxy's internal hostname.
170
+ if (parsed.command === 'dev' && parsed.host !== undefined) paramOverrides.host = parsed.host
171
+ const params = { ...(adapter.params(parsed.env) as Record<string, unknown>), ...paramOverrides }
172
+
173
+ const domain = toDomainInfo(def)
174
+
175
+ // The client SPA is a DECLARED binding (`defineDomain({ client })`), not a
176
+ // folder we sniff for: its presence + source dir come from the definition.
177
+ const clientDir = def.client ? join(projectDir, def.client.dir) : undefined
178
+ const secrets = loadSecrets(adapter, params, projectDir)
179
+
180
+ if (parsed.command === 'dev' || (parsed.command === 'deploy' && parsed.watch)) {
181
+ return runWatch({
182
+ adapter,
183
+ params,
184
+ paramOverrides,
185
+ projectDir,
186
+ specPath,
187
+ clientDir,
188
+ secrets,
189
+ domain,
190
+ env: parsed.env,
191
+ def,
192
+ configPath,
193
+ })
194
+ }
195
+
196
+ // deploy (no watch)
197
+ const result = await adapter.deploy(params, {
198
+ projectDir,
199
+ specPath,
200
+ secrets,
201
+ ...(clientDir ? { clientDir } : {}),
202
+ domain,
203
+ env: parsed.env,
204
+ })
205
+ await writeSpecBestEffort({ projectDir, specPath, def, url: result.url })
206
+ printDeployed(result, def)
207
+
208
+ if (parsed.publish) {
209
+ const pkg = readPackageMeta(projectDir)
210
+ // The registry `name` is a short catalog slug, distinct from the FQDN-like
211
+ // `origin` AND from the npm package name (`@scope/…` for a published domain —
212
+ // illegal as a catalog slug). Default to the origin's first label (the fleet
213
+ // convention, e.g. `ai-gateway`); `--name` overrides.
214
+ const name = parsed.name ?? def.origin.split('.')[0] ?? def.origin
215
+ return await publishToAdmin({
216
+ origin: def.origin,
217
+ name,
218
+ url: result.url,
219
+ ...(pkg.description ? { description: pkg.description } : {}),
220
+ ...(parsed.installByDefault ? { installByDefault: true } : {}),
221
+ })
222
+ }
223
+ return 0
224
+ }
225
+
226
+ /**
227
+ * Register the just-deployed URL in the admin catalog by shelling out to the
228
+ * operator CLI (`astrale domain publish`). Deliberately a subprocess, not a
229
+ * library call: all credential/admin-target resolution lives in `@astrale-os/
230
+ * astrale` and we don't want it (or a kernel client) pulled into this build
231
+ * tool — we only own the fresh URL. A missing `astrale` is reported with the
232
+ * exact line to run by hand, so a deploy that already succeeded isn't a dead end.
233
+ */
234
+ async function publishToAdmin(args: {
235
+ origin: string
236
+ name: string
237
+ url: string
238
+ description?: string
239
+ installByDefault?: boolean
240
+ }): Promise<number> {
241
+ // `--public-url`, not `--url`: the operator CLI reserves `--url` for kernel
242
+ // targeting, so the domain's public address rides its own flag.
243
+ const cliArgs = [
244
+ 'domain',
245
+ 'publish',
246
+ '--origin',
247
+ args.origin,
248
+ '--name',
249
+ args.name,
250
+ '--public-url',
251
+ args.url,
252
+ ]
253
+ if (args.description) cliArgs.push('--description', args.description)
254
+ if (args.installByDefault) cliArgs.push('--install-by-default')
255
+
256
+ info(`publishing to the admin catalog → astrale ${cliArgs.join(' ')}`)
257
+ return await new Promise<number>((resolveCode) => {
258
+ const child = spawn('astrale', cliArgs, { stdio: 'inherit' })
259
+ child.on('error', (err) => {
260
+ if ((err as NodeJS.ErrnoException).code === 'ENOENT') {
261
+ error(
262
+ `\`astrale\` CLI not found on PATH. The deploy succeeded — publish it by hand:\n` +
263
+ ` astrale ${cliArgs.join(' ')}`,
264
+ )
265
+ } else {
266
+ error(`failed to run \`astrale domain publish\`: ${err.message}`)
267
+ }
268
+ resolveCode(1)
269
+ })
270
+ child.on('exit', (code) => resolveCode(code ?? 1))
271
+ })
272
+ }
273
+
274
+ /** Best-effort read of the project's package.json `name`/`description` (publish defaults). */
275
+ function readPackageMeta(projectDir: string): { name?: string; description?: string } {
276
+ try {
277
+ const pkg = JSON.parse(readFileSync(join(projectDir, 'package.json'), 'utf8')) as {
278
+ name?: string
279
+ description?: string
280
+ }
281
+ return { name: pkg.name, description: pkg.description }
282
+ } catch {
283
+ return {}
284
+ }
285
+ }
286
+
287
+ async function runWatch(args: {
288
+ adapter: DomainAdapter<unknown>
289
+ params: unknown
290
+ paramOverrides: Record<string, unknown>
291
+ projectDir: string
292
+ specPath: string
293
+ clientDir?: string
294
+ secrets: Record<string, string>
295
+ domain: DomainInfo
296
+ env: string
297
+ def: DeployConfig
298
+ configPath: string
299
+ }): Promise<number> {
300
+ const { adapter, params, projectDir, specPath, clientDir, secrets, domain, env, def } = args
301
+ let reloads = 0
302
+ const onReload = () => {
303
+ reloads += 1
304
+ info(`↻ reloaded (#${reloads}) — same URL, no reinstall needed for handler edits`)
305
+ }
306
+ const handle: WatchHandle = await adapter.watch(params, {
307
+ projectDir,
308
+ specPath,
309
+ ...(clientDir ? { clientDir } : {}),
310
+ secrets,
311
+ domain,
312
+ env,
313
+ onReload,
314
+ })
315
+ await writeSpecBestEffort({ projectDir, specPath, def, url: handle.url })
316
+ printDevReady(handle.url, domain)
317
+
318
+ const stopConfigWatch = watchConfigForRegen({
319
+ configPath: args.configPath,
320
+ initial: def,
321
+ env,
322
+ paramOverrides: args.paramOverrides,
323
+ projectDir,
324
+ specPath,
325
+ ...(clientDir ? { clientDir } : {}),
326
+ url: handle.url,
327
+ onReload,
328
+ })
329
+
330
+ await new Promise<void>((resolveStop) => {
331
+ const stop = () => {
332
+ stopConfigWatch()
333
+ void handle.stop().finally(resolveStop)
334
+ }
335
+ process.on('SIGINT', stop)
336
+ process.on('SIGTERM', stop)
337
+ })
338
+ return 0
339
+ }
340
+
341
+ /**
342
+ * Watch `astrale.config.ts` while `dev` runs: on change, re-import the config
343
+ * (cache-busted), re-resolve env params + secrets, and re-run the adapter's
344
+ * codegen via `adapter.regenerate` — the running dev server (e.g. `wrangler
345
+ * dev`, which watches its generated config + entry) reloads them itself, so a
346
+ * config edit lands without restarting the CLI.
347
+ *
348
+ * Watches the config's DIRECTORY, not the file: editors save via atomic
349
+ * rename, which silently kills an inode-bound watch.
350
+ *
351
+ * Deliberate limits, surfaced to the user instead of half-applied:
352
+ * • origin / adapter changes re-key the worker's identity → restart;
353
+ * • a mid-edit broken config (syntax error, bad env) warns and keeps the
354
+ * previous generation running;
355
+ * • modules the config IMPORTS (e.g. the schema) stay module-cached — only
356
+ * the config file itself is re-evaluated, which covers everything
357
+ * `defineDomain` owns (vars, requires, postInstall, wrangler overlay…).
358
+ */
359
+ export function watchConfigForRegen(args: {
360
+ configPath: string
361
+ initial: DeployConfig
362
+ env: string
363
+ /** CLI flag overrides (dev --port / --host) — re-applied on every regen. */
364
+ paramOverrides: Record<string, unknown>
365
+ projectDir: string
366
+ specPath: string
367
+ clientDir?: string
368
+ url: string
369
+ onReload: () => void
370
+ }): () => void {
371
+ let running = false
372
+ let pending = false
373
+ let stopped = false
374
+ let timer: ReturnType<typeof setTimeout> | undefined
375
+
376
+ async function regen(): Promise<void> {
377
+ if (stopped) return
378
+ if (running) {
379
+ pending = true
380
+ return
381
+ }
382
+ running = true
383
+ try {
384
+ const def = await loadConfig(args.configPath, { fresh: true })
385
+ if (def.origin !== args.initial.origin || def.adapter.name !== args.initial.adapter.name) {
386
+ warn(
387
+ `astrale.config.ts: \`origin\` or adapter changed — that re-keys the worker's ` +
388
+ `identity; restart \`astrale-domain dev\` to apply.`,
389
+ )
390
+ return
391
+ }
392
+ const adapter = def.adapter as DomainAdapter<unknown>
393
+ if (!adapter.regenerate) {
394
+ warn(
395
+ `astrale.config.ts changed, but the "${adapter.name}" adapter has no \`regenerate\` — ` +
396
+ `restart \`astrale-domain dev\` to apply.`,
397
+ )
398
+ return
399
+ }
400
+ const params = {
401
+ ...(adapter.params(args.env) as Record<string, unknown>),
402
+ ...args.paramOverrides,
403
+ }
404
+ const domain = toDomainInfo(def)
405
+ await adapter.regenerate(params, {
406
+ projectDir: args.projectDir,
407
+ specPath: args.specPath,
408
+ ...(args.clientDir ? { clientDir: args.clientDir } : {}),
409
+ secrets: loadSecrets(adapter, params, args.projectDir),
410
+ domain,
411
+ env: args.env,
412
+ onReload: args.onReload,
413
+ })
414
+ await writeSpecBestEffort({
415
+ projectDir: args.projectDir,
416
+ specPath: args.specPath,
417
+ def,
418
+ url: args.url,
419
+ })
420
+ info(
421
+ `↻ astrale.config.ts changed — codegen regenerated, the dev server reloads it ` +
422
+ `(a port / workerName / remote change still needs a restart)`,
423
+ )
424
+ } catch (err) {
425
+ warn(
426
+ `astrale.config.ts changed but reloading it failed (previous config stays live): ` +
427
+ `${(err as Error).message}`,
428
+ )
429
+ } finally {
430
+ running = false
431
+ if (pending && !stopped) {
432
+ pending = false
433
+ void regen()
434
+ }
435
+ }
436
+ }
437
+
438
+ const configFile = basename(args.configPath)
439
+ const watcher = watchFs(dirname(args.configPath), (_event, file) => {
440
+ if (file && file !== configFile) return
441
+ clearTimeout(timer)
442
+ // Debounce: editors fire several fs events per save (write + rename).
443
+ timer = setTimeout(() => void regen(), 200)
444
+ })
445
+
446
+ return () => {
447
+ stopped = true
448
+ clearTimeout(timer)
449
+ watcher.close()
450
+ }
451
+ }
452
+
453
+ // ── spec ─────────────────────────────────────────────────────────────────
454
+
455
+ async function writeSpec(args: {
456
+ projectDir: string
457
+ specPath: string
458
+ def: DeployConfig
459
+ url: string
460
+ }): Promise<void> {
461
+ const { wire, schemaHash } = await buildProjectSpec(args.def, args.url)
462
+ await mkdir(dirname(args.specPath), { recursive: true })
463
+ await writeFile(args.specPath, JSON.stringify({ ...wire, schemaHash }, null, 2))
464
+ }
465
+
466
+ /**
467
+ * Diagnostic-spec write for dev/deploy: best-effort — the worker builds the
468
+ * live install bundle itself, so a malformed project still runs/deploys; we
469
+ * warn and continue.
470
+ */
471
+ async function writeSpecBestEffort(args: {
472
+ projectDir: string
473
+ specPath: string
474
+ def: DeployConfig
475
+ url: string
476
+ }): Promise<void> {
477
+ try {
478
+ await writeSpec(args)
479
+ } catch (err) {
480
+ const msg = (err as Error).message
481
+ warn(`(non-fatal) skipped the local diagnostic spec — the worker builds the real one: ${msg}`)
482
+ if (msg.includes('MISSING_DEF')) {
483
+ warn(
484
+ ` MISSING_DEF here usually means TWO copies of @astrale-os/kernel-core are loaded ` +
485
+ `(linked dev deps). Try \`pnpm dedupe\`; the deploy itself is unaffected.`,
486
+ )
487
+ }
488
+ }
489
+ }
490
+
491
+ // ── helpers ────────────────────────────────────────────────────────────────
492
+
493
+ function loadSecrets(
494
+ adapter: DomainAdapter<unknown>,
495
+ params: unknown,
496
+ projectDir: string,
497
+ ): Record<string, string> {
498
+ const file = adapter.secretsFile?.(params)
499
+ if (!file) return {}
500
+ const abs = isAbsolute(file) ? file : join(projectDir, file)
501
+ return loadDotenvFile(abs)
502
+ }
503
+
504
+ function findConfig(dir: string): string | undefined {
505
+ for (const name of CONFIG_NAMES) {
506
+ const candidate = join(dir, name)
507
+ if (existsSync(candidate)) return candidate
508
+ }
509
+ return undefined
510
+ }
511
+
512
+ function toDomainInfo(def: DeployConfig): DomainInfo {
513
+ return {
514
+ origin: def.origin,
515
+ requires: def.requires,
516
+ ...(def.postInstall ? { postInstall: def.postInstall } : {}),
517
+ // Presence comes from the definition the author wired — never a folder probe.
518
+ hasViews: def.views !== undefined,
519
+ hasFunctions: def.functions !== undefined,
520
+ hasClient: def.client !== undefined,
521
+ hasDeps: def.deps !== undefined,
522
+ }
523
+ }
524
+
525
+ async function loadConfig(path: string, opts?: { fresh: boolean }): Promise<DeployConfig> {
526
+ // Re-importing an EDITED file: Bun's ESM registry is keyed by path and never
527
+ // re-reads the source — a `?reload=N` query re-EVALUATES but serves the
528
+ // CACHED (stale) source. Bun unifies that registry with `require.cache`, so
529
+ // deleting the entry forces a fresh read on the next import (the CLI is
530
+ // Bun-gated, see `run()`). Modules the config itself imports stay cached —
531
+ // the documented hot-regen limit.
532
+ if (opts?.fresh) delete createRequire(import.meta.url).cache[path]
533
+ const mod = (await import(pathToFileURL(path).href)) as { default?: unknown }
534
+ const def = mod.default
535
+ if (!def || typeof def !== 'object' || !('adapter' in def) || !('origin' in def)) {
536
+ throw new Error(
537
+ `${path} must \`export default deploy(domain, adapter)\` from '@astrale-os/sdk' ` +
538
+ `(where \`domain\` is a \`defineDomain({ ... })\` from your domain.ts).`,
539
+ )
540
+ }
541
+ return def as DeployConfig
542
+ }
543
+
544
+ /** Require a plausible public URL for `--host`; default the scheme to https. */
545
+ function normalizeHost(raw: string | undefined): string {
546
+ if (!raw || raw.startsWith('-')) throw new Error(`--host needs a public URL, got "${raw ?? ''}"`)
547
+ const url = /^https?:\/\//.test(raw) ? raw : `https://${raw}`
548
+ try {
549
+ return new URL(url).origin
550
+ } catch {
551
+ throw new Error(`--host needs a valid URL, got "${raw}"`)
552
+ }
553
+ }
554
+
555
+ export function parseArgs(argv: readonly string[]): ParsedArgs {
556
+ const [cmd, ...rest] = argv
557
+ const watch = rest.includes('--watch')
558
+ // Tail-flag (deploy/prod/publish): register the deployed URL in the admin
559
+ // catalog after a successful deploy. The `publish` command implies it.
560
+ const publish = rest.includes('--publish')
561
+ const installByDefault = rest.includes('--install-by-default')
562
+ // `--port <n>` / `--port=<n>`: consume the flag AND its value so neither
563
+ // leaks into the positionals (a bare `8788` would be read as the env name).
564
+ let port: number | undefined
565
+ let host: string | undefined
566
+ let name: string | undefined
567
+ let publicUrl: string | undefined
568
+ const cleaned: string[] = []
569
+ for (let i = 0; i < rest.length; i++) {
570
+ const a = rest[i]!
571
+ if (a === '--port') {
572
+ const v = rest[++i]
573
+ port = Number(v)
574
+ if (!Number.isInteger(port) || port <= 0)
575
+ throw new Error(`--port needs a port number, got "${v}"`)
576
+ } else if (a.startsWith('--port=')) {
577
+ port = Number(a.slice('--port='.length))
578
+ if (!Number.isInteger(port) || port <= 0)
579
+ throw new Error(`--port needs a port number, got "${a}"`)
580
+ } else if (a === '--host') {
581
+ host = normalizeHost(rest[++i])
582
+ } else if (a.startsWith('--host=')) {
583
+ host = normalizeHost(a.slice('--host='.length))
584
+ } else if (a === '--name') {
585
+ name = rest[++i]
586
+ if (!name) throw new Error('--name needs a value (the registry slug to publish under)')
587
+ } else if (a.startsWith('--name=')) {
588
+ name = a.slice('--name='.length)
589
+ } else if (a === '--public-url') {
590
+ publicUrl = rest[++i]
591
+ if (!publicUrl)
592
+ throw new Error('--public-url needs a value (the address the catalog points at)')
593
+ } else if (a.startsWith('--public-url=')) {
594
+ publicUrl = a.slice('--public-url='.length)
595
+ } else {
596
+ cleaned.push(a)
597
+ }
598
+ }
599
+ const positionals = cleaned.filter((a) => !a.startsWith('-'))
600
+
601
+ // Publish-related fields shared by deploy/prod/publish (publish itself is
602
+ // gated per-command: opt-in via `--publish`, always-on for `publish`).
603
+ const pub = {
604
+ ...(name !== undefined ? { name } : {}),
605
+ ...(installByDefault ? { installByDefault } : {}),
606
+ }
607
+
608
+ switch (cmd) {
609
+ case 'dev':
610
+ return {
611
+ command: 'dev',
612
+ env: positionals[0] ?? 'dev',
613
+ watch: true,
614
+ ...(port !== undefined ? { port } : {}),
615
+ ...(host !== undefined ? { host } : {}),
616
+ }
617
+ case 'prod':
618
+ return { command: 'deploy', env: 'prod', watch, ...(publish ? { publish } : {}), ...pub }
619
+ case 'deploy': {
620
+ const env = positionals[0]
621
+ if (!env) throw new Error('`deploy` requires an env key, e.g. `deploy prod`.')
622
+ return { command: 'deploy', env, watch, ...(publish ? { publish } : {}), ...pub }
623
+ }
624
+ case 'publish':
625
+ // Register the ALREADY-deployed URL in the admin catalog — NO deploy.
626
+ // (Use `deploy [env] --publish` to deploy AND register in one step.) The
627
+ // public address defaults to `https://<origin>`; `--public-url` overrides.
628
+ return {
629
+ command: 'publish',
630
+ env: positionals[0] ?? 'prod',
631
+ watch: false,
632
+ ...pub,
633
+ ...(publicUrl !== undefined ? { publicUrl } : {}),
634
+ }
635
+ case 'build':
636
+ // Spec-only — no env resolution happens on this path.
637
+ return { command: 'build', env: 'dev', watch: false }
638
+ default:
639
+ throw new Error(cmd ? `Unknown command "${cmd}".` : 'Missing command.')
640
+ }
641
+ }
642
+
643
+ function rel(from: string, to: string): string {
644
+ return to.startsWith(from) ? `.${to.slice(from.length)}` : to
645
+ }
646
+
647
+ // ── output ───────────────────────────────────────────────────────────────
648
+
649
+ const GREEN = '\x1b[32m'
650
+ const DIM = '\x1b[2m'
651
+ const BOLD = '\x1b[1m'
652
+ const YELLOW = '\x1b[33m'
653
+ const RED = '\x1b[31m'
654
+ const RESET = '\x1b[0m'
655
+
656
+ function info(msg: string): void {
657
+ process.stdout.write(`${DIM}›${RESET} ${msg}\n`)
658
+ }
659
+ function warn(msg: string): void {
660
+ process.stderr.write(`${YELLOW}!${RESET} ${msg}\n`)
661
+ }
662
+ function error(msg: string): void {
663
+ process.stderr.write(`${RED}✗${RESET} ${msg}\n`)
664
+ }
665
+
666
+ function printDevReady(url: string, domain: DomainInfo): void {
667
+ process.stdout.write(
668
+ `\n${GREEN}✓${RESET} ${BOLD}${domain.origin}${RESET} running (dev, hot-reload)\n` +
669
+ ` ${BOLD}URL${RESET} ${GREEN}${url}${RESET}\n` +
670
+ ` ${DIM}install on an instance:${RESET}\n` +
671
+ ` ${BOLD}astrale domain install ${url} --direct${RESET}\n` +
672
+ ` ${DIM}edit a handler → reloads at the same URL. edit the schema → reinstall.${RESET}\n\n`,
673
+ )
674
+ }
675
+
676
+ function printDeployed(result: DeployResult, def: DeployConfig): void {
677
+ // Adapters that already completed the install (managed deploys) supply
678
+ // their own next-steps; the default hint would tell the user to redo it.
679
+ const footer =
680
+ result.nextSteps !== undefined
681
+ ? result.nextSteps === ''
682
+ ? ''
683
+ : `${result.nextSteps}\n`
684
+ : ` ${DIM}install on an instance:${RESET}\n` +
685
+ ` ${BOLD}astrale domain install ${result.url} --direct${RESET}\n`
686
+ process.stdout.write(
687
+ `\n${GREEN}✓${RESET} ${BOLD}${def.origin}${RESET} deployed\n` +
688
+ ` ${BOLD}URL${RESET} ${GREEN}${result.url}${RESET}\n` +
689
+ footer +
690
+ `\n`,
691
+ )
692
+ }
693
+
694
+ function printUsage(msg?: string): void {
695
+ if (msg) error(msg)
696
+ process.stderr.write(
697
+ `\nUsage:\n` +
698
+ ` astrale-domain dev # deploy dev --watch (hot-reload, prints URL)\n` +
699
+ ` astrale-domain prod # deploy prod\n` +
700
+ ` astrale-domain deploy <env> # deploy any env key (--watch optional)\n` +
701
+ ` astrale-domain publish [env] # register the already-deployed URL in the admin catalog (NO deploy)\n` +
702
+ ` astrale-domain build # rebuild the diagnostic spec only\n` +
703
+ `\nFlags:\n` +
704
+ ` --publish # on deploy/prod: ALSO register the deployed URL (deploy + register)\n` +
705
+ ` --name <slug> # registry name to publish under (default: the origin's first label)\n` +
706
+ ` --public-url <url> # (publish) address the catalog points at (default: https://<origin>)\n` +
707
+ ` --install-by-default # mark the published domain for install on every new instance\n` +
708
+ `\n publish shells out to \`astrale domain publish\` (needs \`astrale\` on PATH).\n\n`,
709
+ )
710
+ }