import { ReporterPlugin, PluginContext, AnalysisResult, Logger } from '../../types'; import { promises as fs } from 'fs'; import * as path from 'path'; export class FileReporter implements ReporterPlugin { readonly name = 'file-reporter'; readonly version = '1.0.0'; readonly description = 'Saves analysis reports to files'; private logger!: Logger; private outputDir: string = './reports'; async initialize(context: PluginContext): Promise { this.logger = context.logger; this.outputDir = context.config.outputDir || './reports'; this.logger.debug('File reporter initialized'); } async report(result: AnalysisResult, output: string): Promise { try { // Ensure output directory exists await fs.mkdir(this.outputDir, { recursive: true }); // Generate filename with timestamp const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const format = this.detectFormat(output); const filename = `translation-analysis-${timestamp}.${format}`; const filepath = path.join(this.outputDir, filename); // Write the report await fs.writeFile(filepath, output, 'utf-8'); this.logger.info(`Report saved to: ${filepath}`); // Also create a latest report link const latestPath = path.join(this.outputDir, `latest.${format}`); await fs.writeFile(latestPath, output, 'utf-8'); // Generate index file for multiple reports await this.generateIndexFile(result); } catch (error) { this.logger.error(`Failed to save report: ${error}`); throw error; } } private detectFormat(output: string): string { if (output.trim().startsWith(' { try { const files = await fs.readdir(this.outputDir); const reportFiles = files.filter(f => f.startsWith('translation-analysis-') && f !== 'index.html'); const indexHtml = ` Translation Analysis Reports

📊 Translation Analysis Reports

Generated reports for project: ${result.config.srcPath}

    ${reportFiles.reverse().map((file, index) => { const timestamp = file.match(/translation-analysis-(.+)\./)?.[1]?.replace(/-/g, ':') || ''; const date = new Date(timestamp).toLocaleString(); const isLatest = index === 0; return `
  • ${file} ${isLatest ? 'Latest' : ''}
    Generated: ${date}
  • `; }).join('')}

Quick Links

📄 Latest HTML Report

📊 Latest JSON Report

📝 Latest Text Report

`; await fs.writeFile(path.join(this.outputDir, 'index.html'), indexHtml, 'utf-8'); } catch (error) { this.logger.warn(`Failed to generate index file: ${error}`); } } }