oblaka-iac 0.0.18 → 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 +19 -11
- package/package.json +1 -1
- package/src/api.ts +5 -2
- package/src/commands/configure.ts +1 -0
- package/src/commands/deploy.ts +1 -1
- package/src/commands/input.ts +11 -0
- package/src/commands/resource-processor.ts +24 -1
- package/src/commands/validate.ts +1 -0
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
|
|
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
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
|
|
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
|
package/src/commands/deploy.ts
CHANGED
|
@@ -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`)
|
package/src/commands/input.ts
CHANGED
|
@@ -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,
|
|
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
|
|
package/src/commands/validate.ts
CHANGED