import { promises as fs } from "fs";
import { parse } from "yaml";
import { join, dirname, resolve } from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const DEFAULT_CONFIG = join(__dirname, "..", "..", "config", "repos.yaml");
let cachedConfig = null;
let cachedPath = null;
function getConfigPath() {
if (process.env.MCP_CONFIG) {
return resolve(process.env.MCP_CONFIG);
}
return DEFAULT_CONFIG;
}
export async function loadConfig(customPath) {
const path = customPath ? resolve(customPath) : getConfigPath();
if (cachedConfig && cachedPath === path)
return cachedConfig;
try {
const content = await fs.readFile(path, "utf-8");
cachedConfig = parse(content);
cachedPath = path;
return cachedConfig;
}
catch (error) {
throw new Error(`Failed to load config from ${path}: ${error}`);
}
}
export function clearConfigCache() {
cachedConfig = null;
cachedPath = null;
}
export async function getRepoConfig(repoName) {
const config = await loadConfig();
const repo = config.repositories[repoName];
if (!repo) {
throw new Error(`Repository ${repoName} not found in config`);
}
return repo;
}
export async function listRepos(includeDisabled = false) {
const config = await loadConfig();
return Object.entries(config.repositories)
.filter(([_, repo]) => includeDisabled || repo.enabled !== false)
.map(([name]) => name);
}
export async function listActiveRepos() {
return listRepos(false);
}
export function isRepoEnabled(repo) {
return repo.enabled !== false;
}