@zabaca/zbc 0.6.2 → 0.8.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 +1 -1
- package/src/commands/add.ts +107 -4
- package/src/commands/secret.test.ts +717 -0
- package/src/commands/secret.ts +429 -0
- package/src/engine/apply.ts +3 -1
- package/src/index.ts +2 -0
- package/templates/CLAUDE.md +1 -1
- package/templates/apps/inbox/package.json +6 -2
- package/templates/apps/inbox/public/index.html +3 -3
- package/templates/apps/inbox/registry.json +3 -3
- package/templates/apps/inbox/worker/inbox-do.ts +291 -36
- package/templates/apps/inbox/worker/index.ts +212 -97
- package/templates/apps/inbox/worker/mcp.ts +279 -0
- package/templates/apps/inbox/worker/send-op.ts +174 -0
- package/templates/apps/inbox/worker/shared.ts +117 -0
- package/templates/apps/inbox/wrangler.jsonc +17 -4
- package/templates/apps/secret-relay/package.json +10 -0
- package/templates/apps/secret-relay/registry.json +11 -0
- package/templates/apps/secret-relay/src/crypto.ts +151 -0
- package/templates/apps/secret-relay/src/page.ts +173 -0
- package/templates/apps/secret-relay/src/relay.ts +139 -0
- package/templates/apps/secret-relay/src/worker.ts +42 -0
- package/templates/apps/secret-relay/wrangler.jsonc +11 -0
- package/templates/infra/modules/cloudflare/index.ts +214 -5
- package/templates/infra/modules/r2/index.ts +137 -0
- package/templates/infra/modules/r2/registry.json +8 -0
package/package.json
CHANGED
package/src/commands/add.ts
CHANGED
|
@@ -33,6 +33,12 @@ interface RegistryManifest {
|
|
|
33
33
|
signupUrl?: string
|
|
34
34
|
tokenUrl?: string
|
|
35
35
|
instructions?: string
|
|
36
|
+
/** app only: generate this instance file under environments/<env>/ after
|
|
37
|
+
* scaffolding, substituting {{ACCOUNT_ID}} / {{PROJECT_NAME}}. Keeps the
|
|
38
|
+
* one-command flow declarative: files land in the repo, `zbc apply`
|
|
39
|
+
* converges (CONTEXT.md: scaffold freely, deploy only through the graph). */
|
|
40
|
+
instanceFile?: string
|
|
41
|
+
instanceTemplate?: string
|
|
36
42
|
}
|
|
37
43
|
|
|
38
44
|
async function resolveBundledDir(name: string): Promise<string> {
|
|
@@ -42,7 +48,7 @@ async function resolveBundledDir(name: string): Promise<string> {
|
|
|
42
48
|
if (await Bun.file(registry).exists()) return dir
|
|
43
49
|
}
|
|
44
50
|
throw new Error(
|
|
45
|
-
`"${name}" not found in built-in registry. Available: cloudflare, cloudflare-email, turso, inbox.`,
|
|
51
|
+
`"${name}" not found in built-in registry. Available: cloudflare, cloudflare-email, r2, turso, inbox, secret-relay.`,
|
|
46
52
|
)
|
|
47
53
|
}
|
|
48
54
|
|
|
@@ -159,8 +165,10 @@ async function installApp(
|
|
|
159
165
|
}
|
|
160
166
|
}
|
|
161
167
|
|
|
162
|
-
// 2. Copy the package
|
|
163
|
-
//
|
|
168
|
+
// 2. Copy the package verbatim — app templates are placeholder-free (all
|
|
169
|
+
// per-project identity lives in the instance file: workerName,
|
|
170
|
+
// r2Bindings, workerVars). registry.json stays in the CLI — it's not app
|
|
171
|
+
// code. `vars` kept for future templates; a no-op with no {{}} tokens.
|
|
164
172
|
const { project } = await loadConfig(projectRoot)
|
|
165
173
|
await copyTemplateDir(sourceDir, destDir, {
|
|
166
174
|
vars: { PROJECT_NAME: project },
|
|
@@ -173,6 +181,46 @@ async function installApp(
|
|
|
173
181
|
printPostInstall(registry)
|
|
174
182
|
}
|
|
175
183
|
|
|
184
|
+
/** Generate an app's declared instance file. Files only — convergence stays
|
|
185
|
+
* with `zbc apply` (never deploys from here). */
|
|
186
|
+
async function generateInstanceFile(
|
|
187
|
+
registry: RegistryManifest,
|
|
188
|
+
projectRoot: string,
|
|
189
|
+
opts: { accountId?: string; env: string },
|
|
190
|
+
): Promise<void> {
|
|
191
|
+
if (!registry.instanceFile || !registry.instanceTemplate) return
|
|
192
|
+
|
|
193
|
+
const instancePath = path.join(
|
|
194
|
+
projectRoot,
|
|
195
|
+
'packages/infra/environments',
|
|
196
|
+
opts.env,
|
|
197
|
+
registry.instanceFile,
|
|
198
|
+
)
|
|
199
|
+
const instanceName = registry.instanceFile.replace(/\.ts$/, '')
|
|
200
|
+
|
|
201
|
+
if (await Bun.file(instancePath).exists()) {
|
|
202
|
+
console.log(`✓ instance file already exists at ${path.relative(projectRoot, instancePath)}`)
|
|
203
|
+
return
|
|
204
|
+
}
|
|
205
|
+
if (registry.instanceTemplate.includes('{{ACCOUNT_ID}}') && !opts.accountId) {
|
|
206
|
+
console.log('')
|
|
207
|
+
console.log(
|
|
208
|
+
`No --account-id given — create the instance file yourself, or re-run: zbc add ${registry.name} --account-id <cloudflare account id>`,
|
|
209
|
+
)
|
|
210
|
+
return
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const { project } = await loadConfig(projectRoot)
|
|
214
|
+
const content = registry.instanceTemplate
|
|
215
|
+
.replaceAll('{{ACCOUNT_ID}}', opts.accountId ?? '')
|
|
216
|
+
.replaceAll('{{PROJECT_NAME}}', project)
|
|
217
|
+
await fs.mkdir(path.dirname(instancePath), { recursive: true })
|
|
218
|
+
await Bun.write(instancePath, content)
|
|
219
|
+
console.log(`✓ instance file generated: ${path.relative(projectRoot, instancePath)}`)
|
|
220
|
+
console.log('')
|
|
221
|
+
console.log(`Deploy it with: zbc apply ${opts.env} ${instanceName}`)
|
|
222
|
+
}
|
|
223
|
+
|
|
176
224
|
export const addCommand = defineCommand({
|
|
177
225
|
meta: {
|
|
178
226
|
name: 'add',
|
|
@@ -181,9 +229,24 @@ export const addCommand = defineCommand({
|
|
|
181
229
|
args: {
|
|
182
230
|
module: {
|
|
183
231
|
type: 'positional',
|
|
184
|
-
description:
|
|
232
|
+
description:
|
|
233
|
+
'Module or app name (e.g. turso, cloudflare, cloudflare-email, r2, inbox, secret-relay)',
|
|
185
234
|
required: true,
|
|
186
235
|
},
|
|
236
|
+
'account-id': {
|
|
237
|
+
type: 'string',
|
|
238
|
+
description: 'Cloudflare account id — lets app templates generate their instance file',
|
|
239
|
+
},
|
|
240
|
+
env: {
|
|
241
|
+
type: 'string',
|
|
242
|
+
description: 'Environment for the generated instance file (default: production)',
|
|
243
|
+
default: 'production',
|
|
244
|
+
},
|
|
245
|
+
prompt: {
|
|
246
|
+
type: 'boolean',
|
|
247
|
+
description: 'Collect missing registry-declared secrets via a Secret Request (default: true)',
|
|
248
|
+
default: true,
|
|
249
|
+
},
|
|
187
250
|
},
|
|
188
251
|
async run({ args }) {
|
|
189
252
|
const name = args.module
|
|
@@ -202,6 +265,11 @@ export const addCommand = defineCommand({
|
|
|
202
265
|
|
|
203
266
|
if (registry.kind === 'app') {
|
|
204
267
|
await installApp(registry, sourceDir, projectRoot, infraDir)
|
|
268
|
+
await generateInstanceFile(registry, projectRoot, {
|
|
269
|
+
accountId: args['account-id'],
|
|
270
|
+
env: args.env,
|
|
271
|
+
})
|
|
272
|
+
await collectDeclaredSecrets(registry, projectRoot, args.env, args.prompt)
|
|
205
273
|
return
|
|
206
274
|
}
|
|
207
275
|
|
|
@@ -213,5 +281,40 @@ export const addCommand = defineCommand({
|
|
|
213
281
|
`Next: create an instance file under packages/infra/environments/<env>/ that imports from ../../modules/${name}.`,
|
|
214
282
|
)
|
|
215
283
|
}
|
|
284
|
+
await collectDeclaredSecrets(registry, projectRoot, args.env, args.prompt)
|
|
216
285
|
},
|
|
217
286
|
})
|
|
287
|
+
|
|
288
|
+
/** Post-install: collect the registry's declared secrets via a Secret Request.
|
|
289
|
+
* A missing relay downgrades to instructions — `add` itself still succeeds. */
|
|
290
|
+
async function collectDeclaredSecrets(
|
|
291
|
+
registry: RegistryManifest,
|
|
292
|
+
projectRoot: string,
|
|
293
|
+
env: string,
|
|
294
|
+
prompt: boolean,
|
|
295
|
+
): Promise<void> {
|
|
296
|
+
const keys = registry.secrets ?? []
|
|
297
|
+
if (keys.length === 0) return
|
|
298
|
+
if (!prompt) {
|
|
299
|
+
console.log('')
|
|
300
|
+
console.log(`Secrets required in ${env}/secrets.yaml (skipped --no-prompt): ${keys.join(', ')}`)
|
|
301
|
+
return
|
|
302
|
+
}
|
|
303
|
+
const { collectSecrets } = await import('./secret')
|
|
304
|
+
try {
|
|
305
|
+
await collectSecrets({
|
|
306
|
+
projectRoot,
|
|
307
|
+
env,
|
|
308
|
+
keys,
|
|
309
|
+
reason: `required by the ${registry.name} module`,
|
|
310
|
+
})
|
|
311
|
+
} catch (err) {
|
|
312
|
+
if (err instanceof Error && err.message.includes('no Secret Relay found')) {
|
|
313
|
+
console.log('')
|
|
314
|
+
console.log(`Secrets still needed in ${env}/secrets.yaml: ${keys.join(', ')}`)
|
|
315
|
+
console.log('(no Secret Relay deployed — `zbc add secret-relay` enables browser collection)')
|
|
316
|
+
return
|
|
317
|
+
}
|
|
318
|
+
throw err
|
|
319
|
+
}
|
|
320
|
+
}
|