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> | undefined; function getComposeSchema(): Promise> { if (!composeSchema) { composeSchema = import("../schema/docker.ts") .then(({ default: schema }) => z.fromJSONSchema( schema as unknown as Parameters[0], ) as z.ZodType, ) .catch((error) => { composeSchema = undefined; throw error; }); } return composeSchema; } export function readCompose(path: string): Promise { return file(path) .text() .then(YAML.parse) .then(async (value) => (await getComposeSchema()).parse(value)); } export async function resolveComposeFile( path: string, ): Promise { 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]; }