import { loadConfig } from "c12" import path from "node:path" import z from "zod" import { findUnambiguousConfigDirMatch, type FindConfigDirMatchesOptions, } from "./config-dir" /** Schema for the project identifier stored in a Automate.ax config file. */ const projectConfigSchema = z.object({ projectId: z.string(), }) /** * Loads and validates a project's Automate.ax configuration file. * * @param configPath - Absolute path to the configuration file. * @throws When the file cannot be loaded or its contents are invalid. */ export async function loadProjectConfig(configPath: string) { const { config } = await loadConfig>({ configFile: path.basename(configPath), configFileRequired: true, cwd: path.dirname(configPath), }) return projectConfigSchema.parse(config) } /** * Loads the nearby project config when directory discovery is unambiguous. * * @param options - Config directory search options. */ export async function findProjectConfig( options: FindConfigDirMatchesOptions = {}, ) { const match = await findUnambiguousConfigDirMatch(options) return match ? loadProjectConfig(match.configPath) : undefined }