// engine/registry/smithery-generator.ts — SPEC-273: Smithery YAML generator
// Generates smithery.yaml for publishing MCP servers to smithery.ai marketplace.
import { writeFileSync } from 'node:fs';
import { join } from 'node:path';
/**
 * Escapes a YAML string value, wrapping in quotes when necessary.
 */
function yamlEscapeValue(value) {
    const needsQuotes = /[:#[\]{},&*?|<>=!%@`'"\n\r\t]/.test(value) || value.trim() !== value;
    if (needsQuotes) {
        return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
    }
    return value;
}
/**
 * Serializes a string array into YAML block sequence lines.
 */
function yamlStringList(items, indent) {
    return items.map((item) => `${indent}- ${yamlEscapeValue(item)}`).join('\n');
}
/**
 * Generates a smithery.yaml string from the given options.
 *
 * @param options  Configuration for the Smithery registry entry.
 * @returns YAML string suitable for writing to disk.
 */
export function generateSmitheryYaml(options) {
    const lines = [
        '# smithery.yaml — MCP server registry entry for smithery.ai',
        '# Auto-generated by Planu (SPEC-273). Do not edit manually.',
        '',
        `name: ${yamlEscapeValue(options.name)}`,
        `description: ${yamlEscapeValue(options.description)}`,
        `version: ${yamlEscapeValue(options.version)}`,
        `author: ${yamlEscapeValue(options.author)}`,
        `repository: ${yamlEscapeValue(options.repository)}`,
    ];
    if (options.homepage !== undefined) {
        lines.push(`homepage: ${yamlEscapeValue(options.homepage)}`);
    }
    if (options.license !== undefined) {
        lines.push(`license: ${yamlEscapeValue(options.license)}`);
    }
    lines.push('');
    lines.push('tools:');
    lines.push(yamlStringList(options.tools, '  '));
    lines.push('');
    lines.push('categories:');
    lines.push(yamlStringList(options.categories, '  '));
    return lines.join('\n') + '\n';
}
/**
 * Writes smithery.yaml to the given project directory.
 *
 * @param projectPath  Absolute path to the project root.
 * @param options      Smithery registry options.
 */
export function writeSmitheryYaml(projectPath, options) {
    const filePath = join(projectPath, 'smithery.yaml');
    const content = generateSmitheryYaml(options);
    /* v8 ignore start */
    try {
        writeFileSync(filePath, content, 'utf-8');
    }
    catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        throw new Error(`Failed to write smithery.yaml to ${filePath}: ${msg}`, { cause: err });
    }
    /* v8 ignore stop */
}
//# sourceMappingURL=smithery-generator.js.map