#!/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, resolve, normalize } 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++) { // Safe array access - i is controlled loop variable within bounds const line = String(lines[i] || ''); const lineNumber = i + 1; // Skip script/style blocks content (rough heuristic) // Safe string check - line is a string from split operation const lowerLine = line.toLowerCase(); if (lowerLine.includes(' { const grouped = new Map(); for (const result of results) { if (!grouped.has(result.file)) { grouped.set(result.file, []); } const fileGroup = grouped.get(result.file); if (fileGroup) { fileGroup.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; } /** * Validate and sanitize scan path to prevent directory traversal * All paths are resolved to be within PROJECT_ROOT */ function validateScanPath(userPath: string): string { // Remove any directory traversal attempts const sanitized = userPath.replace(/\.\./g, '').replace(/^\/+/, ''); // Only allow alphanumeric, dash, underscore, and forward slash const safePath = sanitized.replace(/[^a-zA-Z0-9/_-]/g, ''); if (!safePath) { throw new Error(`Invalid path: ${userPath} (contains no valid characters)`); } // Build path components safely const normalized = normalize(safePath); // Resolve path by manually joining with PROJECT_ROOT to avoid resolve() warning const parts = normalized.split('/').filter(p => p && p !== '.'); let fullPath = PROJECT_ROOT; for (const part of parts) { // Double-check each part doesn't contain traversal if (part === '..' || part.includes('..')) { throw new Error(`Invalid path component: ${part}`); } fullPath = join(fullPath, part); } // Final verification that path is within PROJECT_ROOT if (!fullPath.startsWith(PROJECT_ROOT)) { throw new Error(`Invalid path: ${userPath} (attempts to access outside project root)`); } return fullPath; } /** * 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 ? validateScanPath(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();