import fs from 'fs'; import { ensureDir } from 'fs-extra'; import { z } from 'zod'; import { CLI_CONFIG_FILE, CONFIG_DIR } from './constants.js'; interface MintlifyConfig { telemetryEnabled?: boolean; subdomain?: string; dateFrom?: string; dateTo?: string; publishPromptShown?: string[]; agentSites?: Record; } const agentSiteMetadataSchema = z.object({ registrationId: z.string(), subdomain: z.string() }); export function getAgentSiteMetadata(key: string) { const parsed = agentSiteMetadataSchema.safeParse(readConfig().agentSites?.[key]); return parsed.success ? parsed.data : undefined; } export async function setAgentSiteMetadata( key: string, site: z.infer ) { await writeConfig({ agentSites: { ...readConfig().agentSites, [key]: site } }); } function readConfig(): MintlifyConfig { try { const raw = fs.readFileSync(CLI_CONFIG_FILE, 'utf-8'); return JSON.parse(raw) as MintlifyConfig; } catch { return {}; } } async function writeConfig(updates: Partial): Promise { await ensureDir(CONFIG_DIR); const existing = readConfig(); fs.writeFileSync(CLI_CONFIG_FILE, JSON.stringify({ ...existing, ...updates }, null, 2)); } export function isTelemetryEnabled(): boolean { if (process.env.CLI_TEST_MODE === 'true') return false; if (process.env.MINTLIFY_TELEMETRY_DISABLED === '1') return false; if (process.env.DO_NOT_TRACK === '1') return false; return readConfig().telemetryEnabled !== false; } export async function setTelemetryEnabled(enabled: boolean): Promise { await writeConfig({ telemetryEnabled: enabled }); } export function getConfigValue(key: string): string | undefined { const config = readConfig(); return config[key as keyof MintlifyConfig] as string | undefined; } export async function setConfigValue(key: string, value: string): Promise { await writeConfig({ [key]: value }); } export async function clearConfigValue(key: string): Promise { await writeConfig({ [key]: undefined }); } export function hasShownPublishPrompt(dir: string): boolean { return readConfig().publishPromptShown?.includes(dir) ?? false; } export async function markPublishPromptShown(dir: string): Promise { await writeConfig({ publishPromptShown: [...(readConfig().publishPromptShown ?? []), dir] }); }