Add codegen
Some checks failed
Lint / lint (push) Failing after 10s

This commit is contained in:
Julien Valverdé
2026-02-03 13:33:13 +01:00
parent fb2ae4c3ce
commit 6a9388e5a1
5 changed files with 154 additions and 1 deletions

View File

@@ -38,7 +38,9 @@
"clean:modules": "rm -rf node_modules"
},
"devDependencies": {
"@effect/platform-bun": "^0.87.1",
"@effect/platform-node": "^0.104.1",
"openapi-to-effect": "^0.9.3",
"tsx": "^4.21.0",
"undici": "^7.19.0"
},

View File

@@ -0,0 +1,60 @@
import { FetchHttpClient, FileSystem, HttpClient, Path } from "@effect/platform"
import { BunContext, BunRuntime } from "@effect/platform-bun"
import { YAML } from "bun"
import { Effect, pipe } from "effect"
import { run } from "openapi-to-effect"
const versions = ["v1.53"]
const outputDirPath = "../src/gen"
const downloadDockerOpenApiSpec = Effect.fn("downloadDockerOpenApiSpec")(function*(version: string) {
const client = yield* HttpClient.HttpClient
const fs = yield* FileSystem.FileSystem
const outputFilePath = yield* fs.makeTempFileScoped()
const response = yield* client.get(`https://docs.docker.com/reference/api/engine/version/${ version }.yaml`)
const json = pipe(yield* response.text, YAML.parse, JSON.stringify)
yield* fs.writeFileString(outputFilePath, json)
return outputFilePath
})
const generate = Effect.fn("generate")(function*(
version: string,
specFilePath: string,
tempOutputDirPath: string,
) {
const path = yield* Path.Path
const fs = yield* FileSystem.FileSystem
const outputDirPath = path.join(tempOutputDirPath, version)
yield* fs.makeDirectory(outputDirPath)
run(["gen", specFilePath, outputDirPath])
})
Effect.gen(function*() {
const path = yield* Path.Path
const fs = yield* FileSystem.FileSystem
const tempOutputDirPath = yield* fs.makeTempDirectoryScoped()
yield* Effect.all(
versions.map(version => Effect.andThen(
downloadDockerOpenApiSpec(version),
specFilePath => generate(version, specFilePath, tempOutputDirPath),
))
)
const outputDirFiles = yield* fs.readDirectory(outputDirPath)
yield* Effect.all(outputDirFiles.map(p => fs.remove(p, { recursive: true })))
const tempOutputDirFiles = yield* fs.readDirectory(tempOutputDirPath)
yield* Effect.all(tempOutputDirFiles.map(p => fs.rename(p, path.join(outputDirPath, path.basename(p)))))
}).pipe(
Effect.provide([
FetchHttpClient.layer,
BunContext.layer,
]),
Effect.scoped,
BunRuntime.runMain,
)

View File

@@ -0,0 +1,3 @@
export interface Client {
}

View File

@@ -34,5 +34,5 @@
]
},
"include": ["./src"]
"include": ["./src", "./scripts"]
}