// tools/init-project/conventions-writer.ts — SPEC-530: Generate conventions.md
// with project-specific conventions detected by init_project
import { writeFile, mkdir, access } from 'node:fs/promises';
import { join } from 'node:path';
import { assertEnglishOnlyArtifactText } from '../../engine/spec-language/english-only.js';
const CONVENTIONS_MD_PATH = '.claude/rules/conventions.md';
function buildConventionsContent(knowledge) {
    const stackItems = knowledge.stack;
    const language = knowledge.language;
    const framework = knowledge.framework;
    const lines = [
        '# Project Conventions',
        '',
        'Auto-generated by `init_project`. Update as conventions evolve.',
        '',
        '## Language & Stack',
        '',
        `- Primary language: **${language}**`,
    ];
    if (framework) {
        lines.push(`- Framework: **${framework}**`);
    }
    if (stackItems.length > 0) {
        lines.push(`- Detected stack: ${stackItems.slice(0, 5).join(', ')}`);
    }
    lines.push('', '## Naming Conventions', '', '- Variables/functions: `camelCase`', '- Types/interfaces: `PascalCase`', '- Files: `kebab-case`', '- Constants: `SCREAMING_SNAKE_CASE`', '', '## File Structure', '');
    if (knowledge.projectPath) {
        lines.push(`- Project root: \`${knowledge.projectPath}\``);
    }
    lines.push('- Source: `src/`', '- Tests: `tests/` (mirrors `src/` structure)', '- Specs: `planu/specs/`', '', '## Code Style', '', '- Prefer explicit return types on all functions', '- No magic numbers — use named constants', '- Error handling: return `ToolResult` with `isError: true`, never throw from handlers', '', '## Spec Conventions', '', '- One spec per feature/bugfix', '- ACs must be testable: GIVEN/WHEN/THEN or measurable condition', '- spec.md always in English', '- Technical planning belongs in `spec.md` under `## Technical`; do not create `technical.md`');
    return lines.join('\n') + '\n';
}
/**
 * SPEC-530: Write .claude/rules/conventions.md if missing.
 * Generates project-specific conventions from detected knowledge.
 * @returns true if file was created, false if it already existed.
 */
export async function generateConventionsMdIfMissing(projectPath, knowledge) {
    const conventionsPath = join(projectPath, CONVENTIONS_MD_PATH);
    try {
        await access(conventionsPath);
        return false;
    }
    catch {
        const rulesDir = join(projectPath, '.claude/rules');
        await mkdir(rulesDir, { recursive: true });
        const content = buildConventionsContent(knowledge);
        assertEnglishOnlyArtifactText(content, 'rule');
        await writeFile(conventionsPath, content, 'utf-8');
        return true;
    }
}
//# sourceMappingURL=conventions-writer.js.map