import fs from 'fs/promises';
import path from 'path';
import { FileInfo } from '../utils/fileScanner.js';
/**
* Generates LLM-optimized markdown with full code content and line numbers
*/
export class FullCodeGenerator {
/**
* Generate complete markdown document with full file contents
*/
async generate(files: FileInfo[], basePath: string): Promise<string> {
const sections: string[] = [];
// Header
sections.push('# Full Codebase Content\n');
sections.push(`**Base Path:** \`${basePath}\`\n`);
sections.push(`**Total Files:** ${files.length}\n`);
sections.push('---\n');
// Process each file
for (const file of files) {
sections.push(await this.generateFileSection(file));
}
return sections.join('\n');
}
private async generateFileSection(file: FileInfo): Promise<string> {
const sections: string[] = [];
const separator = '================================================';
sections.push(separator);
sections.push(`File: ${file.relativePath}`);
sections.push(separator);
try {
// Read file content
const content = await fs.readFile(file.absolutePath, 'utf-8');
// Add as a code block with appropriate language
const ext = path.extname(file.relativePath);
const language = this.getLanguageForExtension(ext);
sections.push('```' + language);
sections.push(content);
sections.push('```\n');
} catch (error) {
sections.push('```text');
sections.push('*Failed to read file content*');
sections.push('```\n');
}
return sections.join('\n');
}
private getLanguageForExtension(ext: string): string {
const map: Record<string, string> = {
'.js': 'javascript',
'.jsx': 'javascript',
'.ts': 'typescript',
'.tsx': 'typescript',
'.py': 'python',
'.java': 'java',
'.go': 'go',
'.rs': 'rust',
'.c': 'c',
'.cpp': 'cpp',
'.php': 'php',
'.rb': 'ruby',
'.cs': 'csharp',
'.swift': 'swift',
'.kt': 'kotlin',
'.scala': 'scala',
'.r': 'r',
'.mjs': 'javascript',
'.cjs': 'javascript'
};
return map[ext] || 'text';
}
}