oblaka-iac 0.0.17 → 0.0.19

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/README.md CHANGED
@@ -70,6 +70,14 @@ Generates a `wrangler.jsonc` config inside each worker directory:
70
70
  bunx oblaka oblaka.ts
71
71
  ```
72
72
 
73
+ To generate a second config next to the default one (for example for a test environment), pass `--config` with a file name. Unlike wrangler's `--config`, it is a bare file name ending in `.json` or `.jsonc`, not a path: the file is written into each worker's `dir`.
74
+
75
+ ```bash
76
+ bunx oblaka oblaka.ts --env test --config wrangler.test.jsonc
77
+ ```
78
+
79
+ Durable Object migration history is read back from the generated file itself, so each config file name keeps its own history. Renaming the config of an already deployed worker starts the migrations from `v0001` again; rename the existing file first instead.
80
+
73
81
  ### Deploy to Cloudflare
74
82
 
75
83
  Creates resources on Cloudflare and generates the config:
@@ -102,17 +110,17 @@ bunx oblaka oblaka.ts \
102
110
 
103
111
  ## CLI options
104
112
 
105
- | Option | Env variable | Default | Description |
106
- | ------------------- | ----------------------- | ---------------- | -------------------------------- |
107
- | `--env` | `CLOUDFLARE_ENV` | `local` | Environment name |
108
- | `--remote` | | `false` | Deploy resources to Cloudflare |
109
- | `--dry-run` | | `false` | Preview changes without applying |
110
- | `--account-id` | `CLOUDFLARE_ACCOUNT_ID` | | Cloudflare account ID |
111
- | `--api-token` | `CLOUDFLARE_API_TOKEN` | | Cloudflare API token |
112
- | `--config` | | `wrangler.jsonc` | Output config filename |
113
- | `--state-namespace` | | `cf-state` | KV namespace for state storage |
114
- | `--destroy` | | `false` | Destroy managed resources |
115
- | `--out-state` | | | Export state to a JSON file |
113
+ | Option | Env variable | Default | Description |
114
+ | ------------------- | ----------------------- | ---------------- | ------------------------------------ |
115
+ | `--env` | `CLOUDFLARE_ENV` | `local` | Environment name |
116
+ | `--remote` | | `false` | Deploy resources to Cloudflare |
117
+ | `--dry-run` | | `false` | Preview changes without applying |
118
+ | `--account-id` | `CLOUDFLARE_ACCOUNT_ID` | | Cloudflare account ID |
119
+ | `--api-token` | `CLOUDFLARE_API_TOKEN` | | Cloudflare API token |
120
+ | `--config` | | `wrangler.jsonc` | Output config file name (not a path) |
121
+ | `--state-namespace` | | `cf-state` | KV namespace for state storage |
122
+ | `--destroy` | | `false` | Destroy managed resources |
123
+ | `--out-state` | | | Export state to a JSON file |
116
124
 
117
125
  ## State management
118
126
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oblaka-iac",
3
- "version": "0.0.17",
3
+ "version": "0.0.19",
4
4
  "main": "./src/index.ts",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/api.ts CHANGED
@@ -2,7 +2,7 @@ import * as fs from 'node:fs/promises'
2
2
  import * as path from 'node:path'
3
3
  import { CloudflareDeployExecutor } from './commands/deploy'
4
4
  import type { Input } from './commands/input'
5
- import type { ConfigWriter, GeneratedConfig } from './commands/resource-processor'
5
+ import { type ConfigWriter, DEFAULT_CONFIG_FILE_NAME, type GeneratedConfig } from './commands/resource-processor'
6
6
  import type { Worker } from './resources'
7
7
 
8
8
  export type Definition = Worker
@@ -16,6 +16,8 @@ export interface DeployOptions {
16
16
  dryRun?: boolean
17
17
  remote?: boolean
18
18
  cwd?: string
19
+ /** File name (not a path) of the generated wrangler config in each worker's `dir`. Defaults to `wrangler.jsonc`. */
20
+ configFileName?: string
19
21
  }
20
22
 
21
23
  export interface DeployResult {
@@ -28,7 +30,7 @@ export interface DeployResult {
28
30
  * (the result of `define()` / `new Worker(...)`) against Cloudflare.
29
31
  *
30
32
  * Unlike the CLI, this skips loading the recipe file and works directly on the in-memory
31
- * definition. It always returns the generated wrangler config(s); `wrangler.jsonc` is written
33
+ * definition. It always returns the generated wrangler config(s); the config (`wrangler.jsonc` by default) is written
32
34
  * to disk only when `remote` is set and `dryRun` is not (mirroring the CLI deploy behavior).
33
35
  *
34
36
  * State is stored in the `cf-state` KV namespace under a per-env key (the `env` value).
@@ -57,6 +59,7 @@ export const deploy = async (definition: Definition | undefined, options: Deploy
57
59
  // Programmatic deploy always supplies an explicit apiToken, so wrangler's
58
60
  // OAuth token provider (and its persistence) is never exercised.
59
61
  persistToken: false,
62
+ configFileName: options.configFileName || DEFAULT_CONFIG_FILE_NAME,
60
63
  }
61
64
 
62
65
  const shouldWrite = remote && !dryRun
@@ -24,6 +24,7 @@ export class CloudflareConfigureExecutor {
24
24
  await fs.writeFile(configPath, content)
25
25
  written.push(configPath)
26
26
  },
27
+ this.input.configFileName,
27
28
  )
28
29
 
29
30
  try {
@@ -71,7 +71,7 @@ export class CloudflareDeployExecutor implements ResourceApplier {
71
71
  }
72
72
 
73
73
  private async run(definition: Worker | undefined): Promise<GeneratedConfig[]> {
74
- const resourceProcessor = new ResourceProcessor(this, this.configWriter)
74
+ const resourceProcessor = new ResourceProcessor(this, this.configWriter, this.input.configFileName)
75
75
  const generated = await resourceProcessor.process({ definition, env: this.input.env })
76
76
 
77
77
  this.log(`Changed ${this.totalChanged} resources`)
@@ -1,4 +1,5 @@
1
1
  import { parseArgs } from 'node:util'
2
+ import { DEFAULT_CONFIG_FILE_NAME, validateConfigFileName } from './resource-processor'
2
3
 
3
4
  export const input = (() => {
4
5
  const { values, positionals } = parseArgs({
@@ -16,6 +17,7 @@ export const input = (() => {
16
17
  validate: { type: 'boolean' },
17
18
  'out-state': { type: 'string' },
18
19
  'no-persist-token': { type: 'boolean' },
20
+ config: { type: 'string' },
19
21
  },
20
22
  })
21
23
 
@@ -25,6 +27,13 @@ export const input = (() => {
25
27
  process.exit(1)
26
28
  }
27
29
 
30
+ const configFileName = values.config || DEFAULT_CONFIG_FILE_NAME
31
+ const configFileNameError = validateConfigFileName(configFileName)
32
+ if (configFileNameError) {
33
+ console.error(`Invalid --config: ${configFileNameError}`)
34
+ process.exit(1)
35
+ }
36
+
28
37
  return {
29
38
  main,
30
39
  env: values.env || process.env.CLOUDFLARE_ENV || 'local',
@@ -40,6 +49,8 @@ export const input = (() => {
40
49
  // When using OAuth, persist the refreshed token back to wrangler's config
41
50
  // unless the user opts out (e.g. read-only credentials dir).
42
51
  persistToken: !(values['no-persist-token'] ?? false),
52
+ // Name of the generated wrangler config, written into each worker's `dir`.
53
+ configFileName,
43
54
  }
44
55
  })()
45
56
 
@@ -6,13 +6,36 @@ import type { Config, DefineFn, Resource } from '../types'
6
6
  import { getNextMigrationTag } from '../utils/migrations'
7
7
  import { tryReadFile } from './utils/fs'
8
8
 
9
+ export const DEFAULT_CONFIG_FILE_NAME = 'wrangler.jsonc'
10
+
11
+ /**
12
+ * Returns an error message when `name` cannot be used as the generated config's file name.
13
+ * It is joined onto each worker's `dir`, so it must be a bare file name, and the generator
14
+ * always writes JSONC, so it must end in `.json` or `.jsonc`.
15
+ */
16
+ export const validateConfigFileName = (name: string): string | undefined => {
17
+ if (!name || path.basename(name) !== name || name === '.' || name === '..') {
18
+ return `Config file name must be a bare file name without a directory, got '${name}'`
19
+ }
20
+ if (!/\.jsonc?$/.test(name)) {
21
+ return `Config file name must end with .json or .jsonc, got '${name}'`
22
+ }
23
+ return undefined
24
+ }
25
+
9
26
  export class ResourceProcessor {
10
27
  private workers: Worker[] = []
11
28
 
12
29
  constructor(
13
30
  private readonly resourceHandler: ResourceApplier,
14
31
  private readonly configWriter: ConfigWriter = defaultConfigWriter,
32
+ private readonly configFileName: string = DEFAULT_CONFIG_FILE_NAME,
15
33
  ) {
34
+ // Fail before any resource is applied, not when the config is written.
35
+ const error = validateConfigFileName(configFileName)
36
+ if (error) {
37
+ throw new Error(error)
38
+ }
16
39
  }
17
40
 
18
41
  public async run({ main, env }: {
@@ -49,7 +72,7 @@ export class ResourceProcessor {
49
72
  if (!worker) {
50
73
  return undefined
51
74
  }
52
- const configPath = path.join(worker.options.dir, 'wrangler.jsonc')
75
+ const configPath = path.join(worker.options.dir, this.configFileName)
53
76
  const existingConfig = jsoncParser.parse((await tryReadFile(configPath)) || '{}') as Config
54
77
  const { bindings, dir, deleteDurableObjectsOnRemoval, ...workerConfig } = worker.options
55
78
 
@@ -28,6 +28,7 @@ export class CloudflareValidateExecutor {
28
28
  mismatches.push(`${configPath}: content is outdated, run the generator to update`)
29
29
  }
30
30
  },
31
+ this.input.configFileName,
31
32
  )
32
33
  await resourceProcessor.run({ main: this.input.main, env: this.input.env })
33
34
 
@@ -11,6 +11,13 @@ export class Container implements BindableResource<ContainerState> {
11
11
  name: string
12
12
  className: string
13
13
  image: string
14
+ /**
15
+ * Docker build context for the image, RELATIVE to the wrangler config directory (emitted as
16
+ * `image_build_context`). When unset, wrangler defaults the context to the Dockerfile's own
17
+ * directory. Set it (e.g. `'../..'`) when the Dockerfile must COPY files from OUTSIDE its
18
+ * directory — e.g. a Dockerfile in `packages/runner` that copies sibling packages from a monorepo root.
19
+ */
20
+ imageBuildContext?: string
14
21
  maxInstances: number
15
22
  instanceType: 'dev' | 'basic' | 'standard'
16
23
  /**
@@ -63,6 +70,7 @@ export class Container implements BindableResource<ContainerState> {
63
70
  class_name: this.options.className,
64
71
  name: args.state?.name ?? this.options.name,
65
72
  image: this.options.image,
73
+ image_build_context: this.options.imageBuildContext,
66
74
  instance_type: this.options.instanceType,
67
75
  max_instances: this.options.maxInstances,
68
76
  rollout_active_grace_period: this.options.rolloutActiveGracePeriod,