Files
kuber/lib/yaml.ts
T
2026-09-27 10:40:50 +00:00

49 lines
1.3 KiB
TypeScript

import z from "zod";
import { readdir } from "node:fs/promises";
import { join } from "node:path";
import type { ComposeSpecification } from "../schema/docker.d";
import { file, YAML } from "bun";
const ComposeFileNames = new Set([
"compose.yml",
"compose.yaml",
"docker-compose.yml",
"docker-compose.yaml",
]);
let composeSchema: Promise<z.ZodType<ComposeSpecification>> | undefined;
function getComposeSchema(): Promise<z.ZodType<ComposeSpecification>> {
if (!composeSchema) {
composeSchema = import("../schema/docker.ts")
.then(({ default: schema }) =>
z.fromJSONSchema(
schema as unknown as Parameters<typeof z.fromJSONSchema>[0],
) as z.ZodType<ComposeSpecification>,
)
.catch((error) => {
composeSchema = undefined;
throw error;
});
}
return composeSchema;
}
export function readCompose(path: string): Promise<ComposeSpecification> {
return file(path)
.text()
.then(YAML.parse)
.then(async (value) => (await getComposeSchema()).parse(value));
}
export async function resolveComposeFile(
path: string,
): Promise<string | undefined> {
const entries = await readdir(path, { withFileTypes: true });
return entries
.filter((entry) => entry.isFile() && ComposeFileNames.has(entry.name))
.map((entry) => join(path, entry.name))
.sort()[0];
}