`check-duplicates`: Checks for duplicate keys in translation files
()
| 432 | |
| 433 | /** `check-duplicates`: Checks for duplicate keys in translation files */ |
| 434 | async function checkDuplicates() { |
| 435 | console.log('Checking for duplicate translation keys...\n'); |
| 436 | |
| 437 | const allLocales = getAvailableLocales(); |
| 438 | let foundDuplicates = false; |
| 439 | |
| 440 | for (const locale of allLocales) { |
| 441 | console.log(`Checking ${locale}.ts...`); |
| 442 | |
| 443 | const filePath = path.join(RESOURCES_DIR, `${locale}.ts`); |
| 444 | const content = fs.readFileSync(filePath, 'utf8'); |
| 445 | |
| 446 | // Find all string keys in the file (e.g., "key": "value") |
| 447 | // This regex looks for quoted keys in object notation |
| 448 | const keyPattern = /["']([a-zA-Z0-9_.]+)["']\s*:/g; |
| 449 | const keys = []; |
| 450 | let match; |
| 451 | |
| 452 | while ((match = keyPattern.exec(content)) !== null) { |
| 453 | keys.push(match[1]); |
| 454 | } |
| 455 | |
| 456 | // Find duplicates |
| 457 | const keyCounts = {}; |
| 458 | keys.forEach(key => { |
| 459 | keyCounts[key] = (keyCounts[key] || 0) + 1; |
| 460 | }); |
| 461 | |
| 462 | const duplicates = Object.entries(keyCounts) |
| 463 | .filter(([_, count]) => count > 1) |
| 464 | .map(([key, count]) => ({ key, count })); |
| 465 | |
| 466 | if (duplicates.length > 0) { |
| 467 | foundDuplicates = true; |
| 468 | console.log(` ❌ Found ${duplicates.length} duplicate key(s):`); |
| 469 | duplicates.forEach(({ key, count }) => { |
| 470 | console.log(` - "${key}" appears ${count} times`); |
| 471 | }); |
| 472 | } else { |
| 473 | console.log(` ✅ No duplicates found`); |
| 474 | } |
| 475 | console.log(''); |
| 476 | } |
| 477 | |
| 478 | if (foundDuplicates) { |
| 479 | console.error('\n❌ Duplicate keys found! This will cause issues.'); |
| 480 | console.error('Please remove duplicate keys from the translation files.'); |
| 481 | process.exit(1); |
| 482 | } else { |
| 483 | console.log('✅ No duplicate keys found in any locale!'); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | /** `status`: Shows a summary of translation status without failing. */ |
| 488 | async function status() { |
no test coverage detected