Files
effect-docker/packages/effect-docker/scripts/codegen.ts
Julien Valverdé 933537803f
Some checks failed
Lint / lint (push) Failing after 10s
Fix
2026-02-03 14:11:47 +01:00

60 lines
2.0 KiB
TypeScript

import { FetchHttpClient, FileSystem, HttpClient, Path } from "@effect/platform"
import { BunContext, BunRuntime } from "@effect/platform-bun"
import { Effect } 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://converter.swagger.io/api/convert?url=https://docs.docker.com/reference/api/engine/version/${ version }.yaml`)
const json = JSON.stringify({ ...(yield* response.json) as object, openapi: "3.1.0" })
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,
)