@zabaca/zbc 0.13.0 → 0.15.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.
Files changed (48) hide show
  1. package/CHANGELOG.md +117 -6
  2. package/package.json +5 -2
  3. package/src/commands/add.ts +13 -0
  4. package/src/commands/apply.ts +33 -9
  5. package/src/commands/destroy.ts +1 -1
  6. package/src/commands/init.ts +1 -0
  7. package/src/engine/apply.ts +110 -22
  8. package/src/engine/destroy.ts +183 -14
  9. package/src/engine/discover.ts +1 -1
  10. package/src/engine/resolve.ts +113 -14
  11. package/src/utils/copy-template.ts +10 -0
  12. package/templates/CLAUDE.md +1 -1
  13. package/templates/apps/inbox/package.json +3 -1
  14. package/templates/apps/inbox/tsconfig.json +3 -1
  15. package/templates/apps/inbox/worker/api.ts +197 -0
  16. package/templates/apps/inbox/worker/commands.ts +299 -0
  17. package/templates/apps/inbox/worker/inbox-do.ts +53 -10
  18. package/templates/apps/inbox/worker/index.ts +15 -202
  19. package/templates/apps/inbox/worker/mcp.ts +49 -100
  20. package/templates/infra/modules/cloudflare/index.ts +29 -73
  21. package/templates/infra/modules/cloudflare-access/index.ts +3 -3
  22. package/templates/infra/modules/cloudflare-api/index.ts +144 -32
  23. package/templates/infra/modules/cloudflare-api/registry.json +1 -1
  24. package/templates/infra/modules/cloudflare-email/index.ts +103 -144
  25. package/templates/infra/modules/cloudflare-email/registry.json +1 -1
  26. package/templates/infra/modules/cloudflare-token/index.ts +10 -32
  27. package/templates/infra/modules/cloudflare-token/registry.json +1 -1
  28. package/templates/infra/modules/cloudflare-tunnel/index.ts +3 -3
  29. package/templates/infra/modules/cloudflare-zone/index.ts +2 -2
  30. package/templates/infra/modules/fly/index.ts +17 -42
  31. package/templates/infra/modules/fly/registry.json +1 -1
  32. package/templates/infra/modules/host-file/index.ts +6 -7
  33. package/templates/infra/modules/provision-core/index.ts +21 -15
  34. package/templates/infra/modules/r2/index.ts +40 -71
  35. package/templates/infra/modules/r2/registry.json +1 -1
  36. package/templates/infra/modules/remote-provision/index.ts +1 -3
  37. package/templates/infra/modules/turso/index.ts +2 -15
  38. package/templates/infra/modules/vm-provision/index.ts +10 -3
  39. package/templates/infra/src/context.ts +115 -0
  40. package/templates/infra/src/define-module.ts +41 -2
  41. package/templates/infra/src/index.ts +11 -1
  42. package/templates/infra/src/types.ts +85 -3
  43. package/src/commands/add-registry.test.ts +0 -92
  44. package/src/commands/secret.test.ts +0 -717
  45. package/src/commands/vendor-mode.test.ts +0 -183
  46. package/src/infra-types.ts +0 -38
  47. package/src/utils/subtree.test.ts +0 -127
  48. package/templates/apps/inbox/worker/send-op.ts +0 -174
@@ -1,3 +1,13 @@
1
1
  export { defineModule } from './define-module'
2
2
  export { defineConfig } from './config'
3
- export type { ModuleDefinition, ModuleInstance, ApplyContext, InstanceOptions } from './types'
3
+ export { createApplyContext, ensureApplyContext, resolveOutput, resolveSecret } from './context'
4
+ export type {
5
+ ModuleDefinition,
6
+ ModuleInstance,
7
+ ApplyContext,
8
+ ApplyContextInput,
9
+ OutputRef,
10
+ OutputOptions,
11
+ SecretOptions,
12
+ InstanceOptions,
13
+ } from './types'
@@ -4,18 +4,87 @@ export type ApplyFn<TConfig, TOutputs> = (config: TConfig, ctx: ApplyContext) =>
4
4
 
5
5
  export type DestroyFn<TConfig> = (config: TConfig, ctx: ApplyContext) => Promise<void>
6
6
 
7
- export interface ApplyContext {
7
+ /**
8
+ * The three raw fields a caller has to supply. `defineModule` turns one of
9
+ * these into a full `ApplyContext` before the module body sees it (see
10
+ * `context.ts`), which is why the engine — and a test — may hand `apply` a
11
+ * plain object and the module still gets `secret()` and `output()`.
12
+ */
13
+ export interface ApplyContextInput {
8
14
  secrets: Record<string, string>
9
15
  imports: Record<string, unknown>
10
16
  projectRoot: string
11
17
  }
12
18
 
19
+ /** A `{ from, output }` reference into an imported instance's outputs. */
20
+ export interface OutputRef {
21
+ from?: string
22
+ output?: string
23
+ }
24
+
25
+ export interface SecretOptions {
26
+ /**
27
+ * What wanted the secret, for the error message — e.g.
28
+ * `flySecrets entry "WALGIT_S3_ACCESS_KEY_ID"`. With an alias the env var
29
+ * name and the secrets.yaml key are different strings, and a message naming
30
+ * only one sends the reader to the wrong file.
31
+ */
32
+ field?: string
33
+ /**
34
+ * Presence is the contract rather than non-emptiness. An intentionally blank
35
+ * placeholder — `KEY:` with no value, or `KEY: ""` — lets a script no-op the
36
+ * step it gates until the real value is filled in.
37
+ */
38
+ allowBlank?: boolean
39
+ }
40
+
41
+ export interface OutputOptions {
42
+ /** Accept an emitted empty string — for outputs where "nothing to do" is a real answer. */
43
+ allowBlank?: boolean
44
+ }
45
+
46
+ /**
47
+ * What a module's `apply`/`destroy` receives.
48
+ *
49
+ * The two methods are the engine's answer to the same two questions every
50
+ * module used to answer itself: "what is this secret" and "what did the
51
+ * instance I imported emit". Six copies of the second and seventeen of the
52
+ * first is how they failed differently for the same cause.
53
+ */
54
+ export interface ApplyContext extends ApplyContextInput {
55
+ /**
56
+ * A value from this environment's secrets.yaml. Throws naming the key (and
57
+ * `field`, when given) if the key is absent or blank; `allowBlank` accepts a
58
+ * key that is present but intentionally empty.
59
+ */
60
+ secret(key: string, opts?: SecretOptions): string
61
+ /**
62
+ * An imported instance's output. Throws naming `field` when the ref is
63
+ * incomplete, when the instance is not among this instance's imports, or
64
+ * when it does not emit that output. `allowBlank` accepts an emitted empty
65
+ * string — "nothing to do" is a real answer for some outputs.
66
+ */
67
+ output(ref: OutputRef, field: string, opts?: OutputOptions): string
68
+ }
69
+
70
+ /**
71
+ * The shape `defineModule` publishes: it accepts the raw three fields, because
72
+ * it normalizes them itself. A caller holding a full `ApplyContext` may pass
73
+ * that instead — it is already one of these.
74
+ */
75
+ export type BoundApplyFn<TConfig, TOutputs> = (
76
+ config: TConfig,
77
+ ctx: ApplyContextInput,
78
+ ) => Promise<TOutputs>
79
+
80
+ export type BoundDestroyFn<TConfig> = (config: TConfig, ctx: ApplyContextInput) => Promise<void>
81
+
13
82
  export interface ModuleDefinition<TConfig extends z.ZodType, TOutputs extends z.ZodType> {
14
83
  name: string
15
84
  configSchema: TConfig
16
85
  outputsSchema: TOutputs
17
- apply: ApplyFn<z.infer<TConfig>, z.infer<TOutputs>>
18
- destroy?: DestroyFn<z.infer<TConfig>>
86
+ apply: BoundApplyFn<z.infer<TConfig>, z.infer<TOutputs>>
87
+ destroy?: BoundDestroyFn<z.infer<TConfig>>
19
88
  instance: (opts: InstanceOptions<TConfig>) => ModuleInstance<TOutputs>
20
89
  }
21
90
 
@@ -23,6 +92,17 @@ export interface InstanceOptions<TConfig extends z.ZodType> {
23
92
  name: string
24
93
  config: z.input<TConfig>
25
94
  imports?: ModuleInstance[]
95
+ /**
96
+ * Destroy, then apply, on every `zbc apply` — a clean resource each run.
97
+ * Requires the module to define `destroy`; an ephemeral instance of a module
98
+ * without one is refused before anything is applied.
99
+ *
100
+ * A property of the INSTANCE, not of the module's config: whether a preview
101
+ * resource is thrown away each run is the environment's decision, and four
102
+ * modules each restating it in their own schema is how three of them ended up
103
+ * with three different failure policies and the fourth with none at all.
104
+ */
105
+ ephemeral?: boolean
26
106
  }
27
107
 
28
108
  export interface ModuleInstance<TOutputs extends z.ZodType = z.ZodType> {
@@ -30,6 +110,8 @@ export interface ModuleInstance<TOutputs extends z.ZodType = z.ZodType> {
30
110
  moduleName: string
31
111
  config: unknown
32
112
  imports: ModuleInstance[]
113
+ /** See `InstanceOptions.ephemeral`. Always set — the engine reads it directly. */
114
+ ephemeral: boolean
33
115
  _outputsSchema: TOutputs
34
116
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
35
117
  _definition: ModuleDefinition<any, TOutputs>
@@ -1,92 +0,0 @@
1
- import { afterEach, expect, test } from 'bun:test'
2
- import { spawnSync } from 'node:child_process'
3
- import * as fs from 'node:fs'
4
- import * as os from 'node:os'
5
- import * as path from 'node:path'
6
-
7
- /**
8
- * `zbc add <unknown>` tells the caller what it could have asked for, and that
9
- * sentence is the only place the built-in registry is enumerated for a human.
10
- *
11
- * It was a hand-written literal, and it went stale the first time modules were
12
- * added to `templates/infra/modules/` — the four host primitives contributed
13
- * contributed later resolved perfectly well and were named nowhere, so
14
- * the only way to discover them was to already know they existed. Every other
15
- * "Available:" in this CLI computes its list (`apply.ts`, `destroy.ts`,
16
- * `engine/resolve.ts` all join a live array); this one is now the same shape,
17
- * and this file is what stops it drifting back.
18
- *
19
- * The census is walked here with `readdirSync`, where the implementation globs
20
- * through `Bun.file(...).exists()`. Two different mechanisms on purpose: a test
21
- * that recomputed the implementation would agree with it while both were wrong.
22
- */
23
-
24
- const CLI = path.resolve(import.meta.dir, '../index.ts')
25
- const MODULES = path.resolve(import.meta.dir, '../../templates/infra/modules')
26
- const APPS = path.resolve(import.meta.dir, '../../templates/apps')
27
-
28
- const roots: string[] = []
29
- afterEach(() => {
30
- for (const r of roots.splice(0)) fs.rmSync(r, { recursive: true, force: true })
31
- })
32
-
33
- /** Directories holding a registry.json — what `zbc add` can actually resolve. */
34
- function census(dir: string): string[] {
35
- return fs
36
- .readdirSync(dir, { withFileTypes: true })
37
- .filter((e) => e.isDirectory() && fs.existsSync(path.join(dir, e.name, 'registry.json')))
38
- .map((e) => e.name)
39
- .toSorted()
40
- }
41
-
42
- /** The smallest tree `zbc add` will get as far as resolution in. */
43
- function project(): string {
44
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'zbc-add-registry-'))
45
- roots.push(dir)
46
- fs.writeFileSync(
47
- path.join(dir, 'zbc.config.ts'),
48
- "export default { project: 'p', environments: ['production'] }\n",
49
- )
50
- fs.mkdirSync(path.join(dir, 'packages/infra'), { recursive: true })
51
- fs.writeFileSync(path.join(dir, 'packages/infra/package.json'), '{"name":"@p/infra"}\n')
52
- return dir
53
- }
54
-
55
- /** citty wraps and colours its error line, so flatten before matching — a
56
- * line-oriented match cannot see a claim that wrapped. */
57
- function addUnknown(): string {
58
- const res = spawnSync('bun', [CLI, 'add', 'no-such-module', '--no-prompt'], {
59
- cwd: project(),
60
- encoding: 'utf8',
61
- })
62
- expect(res.status).not.toBe(0)
63
- return `${res.stdout}\n${res.stderr}`.replace(/\s+/g, ' ')
64
- }
65
-
66
- test('the built-in registry census is non-empty and holds the promoted host primitives', () => {
67
- const modules = census(MODULES)
68
- const apps = census(APPS)
69
- // Floors, because every assertion below is vacuously true against an empty
70
- // walk — a mistyped MODULES would otherwise pass silently.
71
- expect(modules.length).toBeGreaterThanOrEqual(12)
72
- expect(apps.length).toBeGreaterThanOrEqual(3)
73
- // Named rather than counted: the group this file exists because of.
74
- expect(modules).toContain('host-dir')
75
- expect(modules).toContain('host-exec')
76
- expect(modules).toContain('host-symlink')
77
- expect(modules).toContain('systemd-mask')
78
- })
79
-
80
- test('`zbc add <unknown>` names every module and app it could have resolved', () => {
81
- const expected = [...census(MODULES), ...census(APPS)].toSorted()
82
- const listed = addUnknown()
83
- .match(/Available: ([^.]+)\./)?.[1]
84
- .split(',')
85
- .map((s) => s.trim())
86
- .toSorted()
87
-
88
- // Set equality, not substring containment: "cloudflare" is a prefix of
89
- // "cloudflare-token", so a `toContain` sweep passes on a list missing the
90
- // short name entirely.
91
- expect(listed).toEqual(expected)
92
- })