oblaka-iac 0.0.3 → 0.0.6
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
package/src/commands/input.ts
CHANGED
|
@@ -3,6 +3,7 @@ import * as fs from 'node:fs/promises'
|
|
|
3
3
|
import * as path from 'node:path'
|
|
4
4
|
import { Worker } from '../resources'
|
|
5
5
|
import type { Config, DefineFn, Resource } from '../types'
|
|
6
|
+
import { getNextMigrationTag } from '../utils/migrations'
|
|
6
7
|
import { tryReadFile } from './utils/fs'
|
|
7
8
|
|
|
8
9
|
export class ResourceProcessor {
|
|
@@ -10,6 +11,7 @@ export class ResourceProcessor {
|
|
|
10
11
|
|
|
11
12
|
constructor(
|
|
12
13
|
private readonly resourceHandler: ResourceApplier,
|
|
14
|
+
private readonly configWriter: ConfigWriter = defaultConfigWriter,
|
|
13
15
|
) {
|
|
14
16
|
}
|
|
15
17
|
|
|
@@ -54,10 +56,44 @@ export class ResourceProcessor {
|
|
|
54
56
|
config = binding.configureBinding({ config, binding: bindingName, state, env })
|
|
55
57
|
}
|
|
56
58
|
|
|
57
|
-
|
|
59
|
+
if (worker.options.deleteDurableObjectsOnRemoval !== false) {
|
|
60
|
+
const oldClasses = new Set((existingConfig.durable_objects?.bindings ?? []).map((b: { class_name: string }) => b.class_name))
|
|
61
|
+
const newClasses = new Set((config.durable_objects?.bindings ?? []).map((b: { class_name: string }) => b.class_name))
|
|
62
|
+
const deletedClasses = [...oldClasses].filter(c => !newClasses.has(c))
|
|
63
|
+
|
|
64
|
+
if (deletedClasses.length > 0) {
|
|
65
|
+
const deletedClassesSet = new Set(deletedClasses)
|
|
66
|
+
config = {
|
|
67
|
+
...config,
|
|
68
|
+
migrations: [
|
|
69
|
+
...(config.migrations ?? [])
|
|
70
|
+
.flatMap(m => {
|
|
71
|
+
if (!m.new_sqlite_classes) return [m]
|
|
72
|
+
const filtered = m.new_sqlite_classes.filter((c: string) => !deletedClassesSet.has(c))
|
|
73
|
+
if (filtered.length === 0) {
|
|
74
|
+
const { new_sqlite_classes: _, ...rest } = m
|
|
75
|
+
return Object.keys(rest).length > 1 ? [rest as typeof m] : []
|
|
76
|
+
}
|
|
77
|
+
return [{ ...m, new_sqlite_classes: filtered }]
|
|
78
|
+
}),
|
|
79
|
+
{
|
|
80
|
+
tag: getNextMigrationTag(config),
|
|
81
|
+
deleted_classes: deletedClasses,
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const content = `/**\n * File is auto generated DO NOT EDIT\n */\n${JSON.stringify(config, null, '\t')}\n`
|
|
89
|
+
await this.configWriter(configPath, content)
|
|
58
90
|
}
|
|
59
91
|
}
|
|
60
92
|
|
|
93
|
+
export type ConfigWriter = (path: string, content: string) => Promise<void>
|
|
94
|
+
|
|
95
|
+
const defaultConfigWriter: ConfigWriter = (path, content) => fs.writeFile(path, content)
|
|
96
|
+
|
|
61
97
|
export interface ResourceApplier {
|
|
62
98
|
applyResource<T>(resource: Resource<T>): Promise<T | undefined>
|
|
63
99
|
}
|
package/src/commands/run.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
1
2
|
import { CloudflareConfigureExecutor } from './configure'
|
|
2
3
|
import { CloudflareDeployExecutor } from './deploy'
|
|
3
4
|
import { input } from './input'
|
|
4
5
|
import { registerSignalHandler } from './utils/pm'
|
|
6
|
+
import { CloudflareValidateExecutor } from './validate'
|
|
5
7
|
|
|
6
8
|
registerSignalHandler()
|
|
7
9
|
|
|
8
|
-
if (input.
|
|
10
|
+
if (input.validate) {
|
|
11
|
+
await CloudflareValidateExecutor.execute({ input })
|
|
12
|
+
} else if (input.remote) {
|
|
9
13
|
await CloudflareDeployExecutor.execute({ input })
|
|
10
14
|
} else {
|
|
11
15
|
await CloudflareConfigureExecutor.execute({ input })
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Input } from './input'
|
|
2
|
+
import { ResourceProcessor } from './resource-processor'
|
|
3
|
+
import { tryReadFile } from './utils/fs'
|
|
4
|
+
|
|
5
|
+
export class CloudflareValidateExecutor {
|
|
6
|
+
public static async execute({ input }: {
|
|
7
|
+
input: Input
|
|
8
|
+
}) {
|
|
9
|
+
const executor = new CloudflareValidateExecutor(input)
|
|
10
|
+
await executor.run()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
private constructor(
|
|
14
|
+
private readonly input: Input,
|
|
15
|
+
) {
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
private async run() {
|
|
19
|
+
const mismatches: string[] = []
|
|
20
|
+
|
|
21
|
+
const resourceProcessor = new ResourceProcessor(
|
|
22
|
+
{ applyResource: () => Promise.resolve(undefined) },
|
|
23
|
+
async (configPath, content) => {
|
|
24
|
+
const existing = await tryReadFile(configPath)
|
|
25
|
+
if (existing === undefined) {
|
|
26
|
+
mismatches.push(`${configPath}: file does not exist, run the generator first`)
|
|
27
|
+
} else if (existing !== content) {
|
|
28
|
+
mismatches.push(`${configPath}: content is outdated, run the generator to update`)
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
)
|
|
32
|
+
await resourceProcessor.run({ main: this.input.main, env: this.input.env })
|
|
33
|
+
|
|
34
|
+
if (mismatches.length > 0) {
|
|
35
|
+
console.error('Validation failed:')
|
|
36
|
+
for (const m of mismatches) {
|
|
37
|
+
console.error(` - ${m}`)
|
|
38
|
+
}
|
|
39
|
+
process.exit(1)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
console.log('Validation passed: all generated configs are up to date.')
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -40,7 +40,9 @@ export class Container implements BindableResource<ContainerState> {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
configureBinding(args: { config: Config; binding: string; state?: ContainerState; env: string }): Config {
|
|
43
|
-
const hasMigration = args.config?.migrations?.find(it
|
|
43
|
+
const hasMigration = args.config?.migrations?.find((it: { new_sqlite_classes?: string[] }) =>
|
|
44
|
+
it.new_sqlite_classes?.includes(this.options.className)
|
|
45
|
+
)
|
|
44
46
|
|
|
45
47
|
return {
|
|
46
48
|
...args.config,
|
package/src/resources/do.ts
CHANGED
|
@@ -24,7 +24,9 @@ export class DurableObject implements BindableResource<DurableObjectState> {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
configureBinding(args: { config: Config; state?: DurableObjectState; binding: string; env: string }): Config {
|
|
27
|
-
const hasMigration = args.config?.migrations?.find(it
|
|
27
|
+
const hasMigration = args.config?.migrations?.find((it: { new_sqlite_classes?: string[] }) =>
|
|
28
|
+
it.new_sqlite_classes?.includes(this.options.className)
|
|
29
|
+
)
|
|
28
30
|
|
|
29
31
|
return {
|
|
30
32
|
...args.config,
|
package/src/resources/worker.ts
CHANGED