#!/usr/bin/env node /** * Hardcoded Text Finder * * Scans Svelte files for hardcoded English text that should be moved to translation files. * Helps identify strings that need internationalization. * * Usage: * npm run find-hardcoded * npm run find-hardcoded -- --path=src/routes/settings * npm run find-hardcoded -- --min-length=10 */ import { readFileSync, readdirSync, statSync } from 'fs'; import { join, relative, dirname } from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const PROJECT_ROOT = join(__dirname, '..'); const DEFAULT_SCAN_PATH = join(PROJECT_ROOT, 'src'); interface HardcodedText { file: string; line: number; text: string; context: string; } /** * Patterns to detect hardcoded text in Svelte files */ const TEXT_PATTERNS = [ // HTML text content: >Text< />([A-Z][^<>{}\n]{3,}) 100) return true; // Too long, likely not a translation return EXCLUDE_PATTERNS.some(pattern => pattern.test(trimmed)); } /** * Extract hardcoded text from file content */ function findHardcodedText(filePath: string, content: string): HardcodedText[] { const results: HardcodedText[] = []; const lines = content.split('\n'); for (let i = 0; i < lines.length; i++) { const line = lines[i]; const lineNumber = i + 1; // Skip script/style blocks content (rough heuristic) if (line.includes(' { const grouped = new Map(); for (const result of results) { if (!grouped.has(result.file)) { grouped.set(result.file, []); } grouped.get(result.file)!.push(result); } return grouped; } /** * Format results for display */ function formatResults(results: HardcodedText[], minLength: number): string { const filtered = results.filter(r => r.text.length >= minLength); const grouped = groupByFile(filtered); let output = ''; output += `\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n`; output += `📝 Hardcoded Text Finder\n`; output += `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n`; output += `Found ${filtered.length} potential hardcoded strings in ${grouped.size} files\n`; output += `Minimum length: ${minLength} characters\n\n`; if (filtered.length === 0) { output += `✅ No hardcoded text found!\n`; return output; } // Sort files by number of issues const sortedFiles = Array.from(grouped.entries()) .sort((a, b) => b[1].length - a[1].length); for (const [file, items] of sortedFiles) { output += `\n📄 ${file} (${items.length} items)\n`; output += `${'─'.repeat(60)}\n`; for (const item of items) { output += ` Line ${item.line}: "${item.text}"\n`; output += ` Context: ${item.context}\n`; } } output += `\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n`; output += `Summary: ${filtered.length} strings need translation\n`; output += `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n`; return output; } /** * Main function */ function main() { const args = process.argv.slice(2); const pathArg = args.find(arg => arg.startsWith('--path='))?.split('=')[1]; const minLengthArg = args.find(arg => arg.startsWith('--min-length='))?.split('=')[1]; const scanPath = pathArg ? join(PROJECT_ROOT, pathArg) : DEFAULT_SCAN_PATH; const minLength = minLengthArg ? parseInt(minLengthArg, 10) : 3; console.log('🔍 Scanning for hardcoded text...\n'); console.log(`Path: ${relative(PROJECT_ROOT, scanPath)}`); console.log(`Min length: ${minLength}\n`); try { const results = scanDirectory(scanPath); const output = formatResults(results, minLength); console.log(output); // Exit with warning if hardcoded text found if (results.length > 0) { console.log('\n💡 Tip: Move these strings to translation files in src/lib/i18n/translations/'); process.exit(1); } } catch (error) { console.error('Error:', error); process.exit(1); } } main();