@zabaca/zbc 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -4
- package/src/commands/add.ts +143 -67
- package/src/commands/init.ts +2 -2
- package/src/utils/copy-template.ts +5 -0
- package/templates/CLAUDE.md +1 -1
- package/templates/apps/inbox/package.json +19 -0
- package/templates/apps/inbox/public/index.html +189 -0
- package/templates/apps/inbox/registry.json +9 -0
- package/templates/apps/inbox/tsconfig.json +17 -0
- package/templates/apps/inbox/worker/inbox-do.ts +141 -0
- package/templates/apps/inbox/worker/index.ts +210 -0
- package/templates/apps/inbox/wrangler.jsonc +35 -0
- package/templates/gitignore +2 -1
- package/templates/infra/modules/cloudflare/index.ts +172 -43
- package/templates/infra/modules/cloudflare/registry.json +1 -1
- package/templates/infra/modules/cloudflare-email/index.ts +469 -0
- package/templates/infra/modules/cloudflare-email/registry.json +12 -0
- package/templates/workflows/preview.yml +3 -2
- package/templates/infra/modules/nats-server/index.ts +0 -267
- package/templates/infra/modules/nats-server/registry.json +0 -12
- package/templates/infra/modules/vercel/index.ts +0 -358
- package/templates/infra/modules/vercel/registry.json +0 -15
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as path from 'node:path'
|
|
2
2
|
import * as fs from 'node:fs'
|
|
3
|
-
import {
|
|
3
|
+
import { execSync, spawnSync } from 'node:child_process'
|
|
4
4
|
import { z } from 'zod'
|
|
5
5
|
import { defineModule } from '../../src/define-module'
|
|
6
6
|
|
|
@@ -8,14 +8,14 @@ import { defineModule } from '../../src/define-module'
|
|
|
8
8
|
* cloudflare — deploys a Cloudflare Worker (optionally with static assets, a
|
|
9
9
|
* Durable Object, and a Container) via `wrangler deploy`.
|
|
10
10
|
*
|
|
11
|
-
* This is a THIN orchestrator, the same shape as the
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* (a
|
|
11
|
+
* This is a THIN orchestrator, the same shape as the turso module: `apply` runs
|
|
12
|
+
* on the operator machine, reads CF creds from `ctx.secrets`, runs an optional
|
|
13
|
+
* local build, then shells `wrangler deploy` in the package's own directory. The
|
|
14
|
+
* Worker TOPOLOGY (name, assets binding, durable_objects, containers,
|
|
15
|
+
* migrations) lives in that package's `wrangler.jsonc` — wrangler is the source
|
|
16
|
+
* of truth for it, not this module. That keeps the module reusable for both
|
|
17
|
+
* plain assets Workers (e.g. a static Astro/Next site) and container-backed
|
|
18
|
+
* payloads (e.g. a DO-bound NATS server).
|
|
19
19
|
*
|
|
20
20
|
* Auth is non-interactive: wrangler reads `CLOUDFLARE_API_TOKEN` +
|
|
21
21
|
* `CLOUDFLARE_ACCOUNT_ID` from the environment, so no `wrangler login` is
|
|
@@ -27,35 +27,58 @@ import { defineModule } from '../../src/define-module'
|
|
|
27
27
|
* with Docker, so Docker must be running locally at apply time, and the account
|
|
28
28
|
* must be on a Workers Paid plan with Containers enabled. A plain assets Worker
|
|
29
29
|
* (Phase A) has neither requirement.
|
|
30
|
+
*
|
|
31
|
+
* Two config knobs shape WHICH worker a given apply targets:
|
|
32
|
+
* - `wranglerEnv` selects a named `env.<name>` block in the package's
|
|
33
|
+
* wrangler.jsonc (`--env`), so one package can ship distinct workers per zbc
|
|
34
|
+
* environment from a single config file.
|
|
35
|
+
* - `workerName` renames the deployed worker (`--name`), which is what powers
|
|
36
|
+
* per-PR preview workers (`zbc-<app>-pr-<N>`). The `*.workers.dev` deploy-URL
|
|
37
|
+
* regex still matches a renamed worker's URL.
|
|
38
|
+
*
|
|
39
|
+
* IMPORT SYNC: `workerSecrets` and `workerVars` entries can be either a plain
|
|
40
|
+
* name (resolved from this environment's secrets.yaml, exactly as before) or a
|
|
41
|
+
* `{ name, from, output }` reference into an imported instance's outputs
|
|
42
|
+
* (`ctx.imports[from][output]`, populated by the engine from that instance's
|
|
43
|
+
* validated outputs). Secrets are pushed via `wrangler secret put`, piped
|
|
44
|
+
* through stdin; vars are passed as `wrangler deploy --var KEY:VALUE`. The two
|
|
45
|
+
* are deliberately separate fields — a var is visible in `wrangler.jsonc`/the
|
|
46
|
+
* dashboard, a secret is not — so an imported value's exposure is an explicit
|
|
47
|
+
* per-entry choice, never inferred. Referencing an instance not listed in this
|
|
48
|
+
* instance's `imports`, or an output key that instance doesn't emit, is a hard
|
|
49
|
+
* error naming both.
|
|
30
50
|
*/
|
|
31
51
|
|
|
32
52
|
/** Resolve the wrangler binary: prefer the package-local one, else `bunx`. */
|
|
33
53
|
function resolveWrangler(workdir: string): { cmd: string; pre: string[] } {
|
|
34
54
|
const local = path.join(workdir, 'node_modules', '.bin', 'wrangler')
|
|
35
55
|
if (fs.existsSync(local)) return { cmd: local, pre: [] }
|
|
36
|
-
// Fall back to bunx
|
|
37
|
-
|
|
56
|
+
// Fall back to bunx on the NODE runtime. Never pass --bun: wrangler under
|
|
57
|
+
// the bun runtime exits 0 after uploading the version but silently skips the
|
|
58
|
+
// deploy/trigger step (2026-07-12 lov incident in Zabaca/ceo: three green
|
|
59
|
+
// applies, zero code deployed).
|
|
60
|
+
return { cmd: 'bunx', pre: ['wrangler'] }
|
|
38
61
|
}
|
|
39
62
|
|
|
40
63
|
/** Run wrangler in `workdir` with CF creds in the env; return captured stdout. */
|
|
41
64
|
function wrangler(workdir: string, args: string[], env: NodeJS.ProcessEnv, input?: string): string {
|
|
42
65
|
const { cmd, pre } = resolveWrangler(workdir)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const stdout = (err as { stdout?: Buffer }).stdout?.toString() ?? ''
|
|
66
|
+
// spawnSync (not execFileSync) so BOTH streams are captured on success too:
|
|
67
|
+
// wrangler splits its human output across stdout/stderr, and parsing only
|
|
68
|
+
// stdout is how the 2026-07-12 silent-no-deploy went unnoticed.
|
|
69
|
+
const res = spawnSync(cmd, [...pre, ...args], {
|
|
70
|
+
cwd: workdir,
|
|
71
|
+
env,
|
|
72
|
+
input,
|
|
73
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
74
|
+
})
|
|
75
|
+
const combined = `${res.stdout?.toString() ?? ''}\n${res.stderr?.toString() ?? ''}`
|
|
76
|
+
if (res.status !== 0 || res.error) {
|
|
55
77
|
throw new Error(
|
|
56
|
-
`wrangler ${args.join(' ')} failed:\n${
|
|
78
|
+
`wrangler ${args.join(' ')} failed (exit ${res.status}):\n${combined.trim() || res.error?.message}`,
|
|
57
79
|
)
|
|
58
80
|
}
|
|
81
|
+
return combined
|
|
59
82
|
}
|
|
60
83
|
|
|
61
84
|
const buildSchema = z.object({
|
|
@@ -63,6 +86,56 @@ const buildSchema = z.object({
|
|
|
63
86
|
cwd: z.string().optional(),
|
|
64
87
|
})
|
|
65
88
|
|
|
89
|
+
/**
|
|
90
|
+
* A worker secret/var entry: either a plain name (resolved from this
|
|
91
|
+
* environment's secrets.yaml) or a reference into an imported instance's
|
|
92
|
+
* outputs (`ctx.imports[from][output]`).
|
|
93
|
+
*/
|
|
94
|
+
const workerValueSchema = z.union([
|
|
95
|
+
z.string(),
|
|
96
|
+
z.object({
|
|
97
|
+
/** Env var name inside the Worker. */
|
|
98
|
+
name: z.string(),
|
|
99
|
+
/** Instance name — must be listed in this instance's `imports`. */
|
|
100
|
+
from: z.string(),
|
|
101
|
+
/** Which output of that instance to read. */
|
|
102
|
+
output: z.string(),
|
|
103
|
+
}),
|
|
104
|
+
])
|
|
105
|
+
|
|
106
|
+
type WorkerValueEntry = z.infer<typeof workerValueSchema>
|
|
107
|
+
|
|
108
|
+
/** Resolve a workerSecrets/workerVars entry to its `{ name, value }` pair. */
|
|
109
|
+
function resolveWorkerValue(
|
|
110
|
+
entry: WorkerValueEntry,
|
|
111
|
+
ctx: { secrets: Record<string, string>; imports: Record<string, unknown> },
|
|
112
|
+
fieldName: string,
|
|
113
|
+
): { name: string; value: string } {
|
|
114
|
+
if (typeof entry === 'string') {
|
|
115
|
+
const value = ctx.secrets[entry]
|
|
116
|
+
if (!value) {
|
|
117
|
+
throw new Error(
|
|
118
|
+
`${fieldName} references "${entry}" but it's missing from this environment's secrets.yaml`,
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
return { name: entry, value }
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const instanceOutputs = ctx.imports[entry.from]
|
|
125
|
+
if (instanceOutputs === undefined) {
|
|
126
|
+
throw new Error(
|
|
127
|
+
`${fieldName} entry "${entry.name}" references instance "${entry.from}", which is not in this instance's imports`,
|
|
128
|
+
)
|
|
129
|
+
}
|
|
130
|
+
const value = (instanceOutputs as Record<string, unknown> | null)?.[entry.output]
|
|
131
|
+
if (typeof value !== 'string') {
|
|
132
|
+
throw new Error(
|
|
133
|
+
`${fieldName} entry "${entry.name}" references output "${entry.output}" on instance "${entry.from}", which doesn't emit it`,
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
return { name: entry.name, value }
|
|
137
|
+
}
|
|
138
|
+
|
|
66
139
|
export const cloudflareModule = defineModule({
|
|
67
140
|
name: 'cloudflare',
|
|
68
141
|
configSchema: z.object({
|
|
@@ -76,12 +149,21 @@ export const cloudflareModule = defineModule({
|
|
|
76
149
|
/** Optional local build run before deploy (e.g. `vite build` for assets). */
|
|
77
150
|
build: buildSchema.optional(),
|
|
78
151
|
/**
|
|
79
|
-
* Worker secrets to push after deploy. Each entry
|
|
80
|
-
* environment's secrets.yaml
|
|
81
|
-
* binding (read at runtime as `env.<NAME>`)
|
|
82
|
-
*
|
|
152
|
+
* Worker secrets to push after deploy. Each entry is either a plain name
|
|
153
|
+
* — a key in this environment's secrets.yaml, becoming a Worker secret
|
|
154
|
+
* binding of the same name (read at runtime as `env.<NAME>`) — or a
|
|
155
|
+
* `{ name, from, output }` reference into an imported instance's outputs.
|
|
156
|
+
* Pushed via `wrangler secret put` with the value piped through stdin.
|
|
83
157
|
*/
|
|
84
|
-
workerSecrets: z.array(
|
|
158
|
+
workerSecrets: z.array(workerValueSchema).default([]),
|
|
159
|
+
/**
|
|
160
|
+
* Worker vars to set at deploy time (`wrangler deploy --var KEY:VALUE`).
|
|
161
|
+
* Same entry shape as `workerSecrets` — plain name (from secrets.yaml) or
|
|
162
|
+
* `{ name, from, output }` — but for non-secret, publicly-visible config.
|
|
163
|
+
* Unlike secrets, var values are passed on the command line, so never
|
|
164
|
+
* route sensitive values through this field.
|
|
165
|
+
*/
|
|
166
|
+
workerVars: z.array(workerValueSchema).default([]),
|
|
85
167
|
/**
|
|
86
168
|
* For container-backed Workers: roll the running container to the new image
|
|
87
169
|
* IMMEDIATELY on deploy (`--containers-rollout immediate`). The wrangler
|
|
@@ -91,6 +173,23 @@ export const cloudflareModule = defineModule({
|
|
|
91
173
|
* deploy time. No-op for asset-only Workers.
|
|
92
174
|
*/
|
|
93
175
|
immediateContainerRollout: z.boolean().default(false),
|
|
176
|
+
/**
|
|
177
|
+
* Wrangler named environment (`--env <name>`). When set, deploy and every
|
|
178
|
+
* `secret put` target the matching `env.<name>` block in the package's
|
|
179
|
+
* wrangler.jsonc, so a non-production zbc environment (e.g. `preview`) can
|
|
180
|
+
* ship a DISTINCT worker (own name/domain/bindings) from the same package —
|
|
181
|
+
* without a separate wrangler config. Omit for the top-level (production)
|
|
182
|
+
* worker.
|
|
183
|
+
*/
|
|
184
|
+
wranglerEnv: z.string().optional(),
|
|
185
|
+
/**
|
|
186
|
+
* Override the deployed worker name (`--name <workerName>`). This is what
|
|
187
|
+
* enables per-PR preview workers (e.g. `zbc-landing-pr-42`) from a single
|
|
188
|
+
* wrangler.jsonc: the preview instance sets a PR-scoped name so each PR
|
|
189
|
+
* lands on its own isolated worker + `*.workers.dev` URL. Omit to use the
|
|
190
|
+
* `name` declared in wrangler.jsonc.
|
|
191
|
+
*/
|
|
192
|
+
workerName: z.string().optional(),
|
|
94
193
|
}),
|
|
95
194
|
outputs: z.object({
|
|
96
195
|
deployUrl: z.string(),
|
|
@@ -116,27 +215,53 @@ export const cloudflareModule = defineModule({
|
|
|
116
215
|
execSync(config.build.command, { cwd: buildCwd, stdio: 'inherit', env })
|
|
117
216
|
}
|
|
118
217
|
|
|
119
|
-
// 2.
|
|
218
|
+
// 2. Resolve every workerVars/workerSecrets entry BEFORE deploy so a bad
|
|
219
|
+
// reference (unknown import, missing output/secret) fails fast, without
|
|
220
|
+
// leaving a deployed-but-unconfigured worker. `resolveWorkerValue` is
|
|
221
|
+
// pure — only the wrangler pushes below have side effects. --var values
|
|
222
|
+
// additionally ship as part of `wrangler deploy` itself (not a follow-up
|
|
223
|
+
// call); secrets are pushed after deploy (step 4) since the script must
|
|
224
|
+
// exist first.
|
|
225
|
+
const resolvedVars = config.workerVars.map((entry) =>
|
|
226
|
+
resolveWorkerValue(entry, ctx, 'workerVars'),
|
|
227
|
+
)
|
|
228
|
+
const resolvedSecrets = config.workerSecrets.map((entry) =>
|
|
229
|
+
resolveWorkerValue(entry, ctx, 'workerSecrets'),
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
// 3. Deploy. Creates/updates the Worker script (+ assets, DO, container).
|
|
233
|
+
// --env selects the named wrangler environment (e.g. preview) so it ships
|
|
234
|
+
// a distinct worker from the same package; --name overrides the worker
|
|
235
|
+
// name (per-PR preview workers); both omitted = the package's default.
|
|
120
236
|
const deployArgs = ['deploy']
|
|
237
|
+
if (config.wranglerEnv) deployArgs.push('--env', config.wranglerEnv)
|
|
238
|
+
if (config.workerName) deployArgs.push('--name', config.workerName)
|
|
121
239
|
if (config.immediateContainerRollout) deployArgs.push('--containers-rollout', 'immediate')
|
|
240
|
+
for (const { name, value } of resolvedVars) deployArgs.push('--var', `${name}:${value}`)
|
|
122
241
|
console.log(
|
|
123
|
-
` Deploying via wrangler (in ${config.workdir})${config.immediateContainerRollout ? ' [immediate container rollout]' : ''}`,
|
|
242
|
+
` Deploying via wrangler (in ${config.workdir})${config.wranglerEnv ? ` [env: ${config.wranglerEnv}]` : ''}${config.workerName ? ` [name: ${config.workerName}]` : ''}${config.immediateContainerRollout ? ' [immediate container rollout]' : ''}${resolvedVars.length ? ` [vars: ${resolvedVars.map((v) => v.name).join(', ')}]` : ''}`,
|
|
124
243
|
)
|
|
125
244
|
const out = wrangler(workdir, deployArgs, env)
|
|
245
|
+
// Success theater guard: wrangler can exit 0 without actually deploying
|
|
246
|
+
// (the --bun incident above). Require the deploy confirmation line.
|
|
247
|
+
if (!/Deployed\s+\S+\s+triggers/.test(out)) {
|
|
248
|
+
throw new Error(
|
|
249
|
+
`wrangler deploy exited 0 but printed no "Deployed ... triggers" confirmation:\n${out}`,
|
|
250
|
+
)
|
|
251
|
+
}
|
|
126
252
|
const urlMatch = out.match(/https:\/\/[^\s"',]+\.workers\.dev[^\s"',]*/)
|
|
127
253
|
const deployUrl = urlMatch?.[0] ?? ''
|
|
128
254
|
console.log(` Deployed: ${deployUrl || '(URL not parsed — see wrangler output)'}`)
|
|
129
255
|
|
|
130
|
-
//
|
|
131
|
-
//
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
wrangler(workdir, ['secret', 'put', name], env, value)
|
|
256
|
+
// 4. Push Worker secrets (after deploy: the script must exist first).
|
|
257
|
+
// Already resolved in step 2, so a misconfigured reference never reaches
|
|
258
|
+
// this point. Value piped via stdin so it never lands in a command
|
|
259
|
+
// string or the log.
|
|
260
|
+
for (const { name, value } of resolvedSecrets) {
|
|
261
|
+
const secretArgs = ['secret', 'put', name]
|
|
262
|
+
if (config.wranglerEnv) secretArgs.push('--env', config.wranglerEnv)
|
|
263
|
+
if (config.workerName) secretArgs.push('--name', config.workerName)
|
|
264
|
+
wrangler(workdir, secretArgs, env, value)
|
|
140
265
|
console.log(` Set Worker secret: ${name}`)
|
|
141
266
|
}
|
|
142
267
|
|
|
@@ -152,9 +277,13 @@ export const cloudflareModule = defineModule({
|
|
|
152
277
|
}
|
|
153
278
|
const workdir = path.resolve(ctx.projectRoot, config.workdir)
|
|
154
279
|
// `wrangler delete` removes the Worker (and its DO/container). Non-fatal if
|
|
155
|
-
// already gone.
|
|
280
|
+
// already gone. --env / --name target the same worker `apply` deployed
|
|
281
|
+
// (named environment and/or per-PR preview name).
|
|
156
282
|
try {
|
|
157
|
-
|
|
283
|
+
const deleteArgs = ['delete', '--force']
|
|
284
|
+
if (config.wranglerEnv) deleteArgs.push('--env', config.wranglerEnv)
|
|
285
|
+
if (config.workerName) deleteArgs.push('--name', config.workerName)
|
|
286
|
+
wrangler(workdir, deleteArgs, env)
|
|
158
287
|
console.log(' Deleted Worker')
|
|
159
288
|
} catch (err) {
|
|
160
289
|
console.log(` Worker delete skipped: ${(err as Error).message}`)
|
|
@@ -11,5 +11,5 @@
|
|
|
11
11
|
"secrets": ["CLOUDFLARE_API_TOKEN"],
|
|
12
12
|
"signupUrl": "https://dash.cloudflare.com/sign-up",
|
|
13
13
|
"tokenUrl": "https://dash.cloudflare.com/profile/api-tokens",
|
|
14
|
-
"instructions": "Add CLOUDFLARE_API_TOKEN to packages/infra/environments/<env>/secrets.yaml; set accountId in the instance config (not a secret). The Worker topology lives in the package's own wrangler.jsonc, which is the source of truth for it. Container-backed Workers need Docker running locally at apply time and a Workers Paid plan with Containers enabled."
|
|
14
|
+
"instructions": "Add CLOUDFLARE_API_TOKEN to packages/infra/environments/<env>/secrets.yaml; set accountId in the instance config (not a secret). The Worker topology lives in the package's own wrangler.jsonc, which is the source of truth for it. For per-PR preview workers, set workerName (e.g. `zbc-<app>-pr-${process.env.PR_NUMBER}`) on the preview instance; use wranglerEnv to select a named `env.<name>` block in wrangler.jsonc instead. Container-backed Workers need Docker running locally at apply time and a Workers Paid plan with Containers enabled. workerSecrets/workerVars entries are either a plain name (from secrets.yaml) or `{ name, from, output }` to pull a value from an imported instance's outputs (the instance must also be listed in this instance's `imports`) — secrets are pushed via `wrangler secret put`, vars via `wrangler deploy --var`."
|
|
15
15
|
}
|