@zabaca/zbc 0.7.0 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zabaca/zbc",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "Zabaca stack CLI — scaffold and deploy infrastructure",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -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, r2, 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
 
@@ -175,6 +181,46 @@ async function installApp(
175
181
  printPostInstall(registry)
176
182
  }
177
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
+
178
224
  export const addCommand = defineCommand({
179
225
  meta: {
180
226
  name: 'add',
@@ -183,9 +229,24 @@ export const addCommand = defineCommand({
183
229
  args: {
184
230
  module: {
185
231
  type: 'positional',
186
- description: 'Module or app name (e.g. turso, cloudflare, cloudflare-email, r2, inbox)',
232
+ description:
233
+ 'Module or app name (e.g. turso, cloudflare, cloudflare-email, r2, inbox, secret-relay)',
187
234
  required: true,
188
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
+ },
189
250
  },
190
251
  async run({ args }) {
191
252
  const name = args.module
@@ -204,6 +265,11 @@ export const addCommand = defineCommand({
204
265
 
205
266
  if (registry.kind === 'app') {
206
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)
207
273
  return
208
274
  }
209
275
 
@@ -215,5 +281,40 @@ export const addCommand = defineCommand({
215
281
  `Next: create an instance file under packages/infra/environments/<env>/ that imports from ../../modules/${name}.`,
216
282
  )
217
283
  }
284
+ await collectDeclaredSecrets(registry, projectRoot, args.env, args.prompt)
218
285
  },
219
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
+ }