import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const WFRP4E_SCRIPTS = '/home/morr/work/foundryvtt/WFRP4e-FoundryVTT/scripts'; const FR_SCRIPTS = '/home/morr/work/foundryvtt/foundryvtt-wh4-lang-fr-fr/scripts'; const REVIEW_FILE = path.join(__dirname, 'scripts-to-review.json'); const OUTPUT_FILE = path.join(__dirname, 'scripts-replaced-list.md'); const scriptsToReview = JSON.parse(fs.readFileSync(REVIEW_FILE, 'utf-8')); console.log(`\n=== REMPLACEMENT DES SCRIPTS PAR LES VERSIONS SYSTÈME ===\n`); console.log(`Nombre de scripts à remplacer: ${scriptsToReview.length}\n`); let replaced = 0; let errors = []; const replacedList = []; scriptsToReview.forEach(({file, reason}, index) => { const wfrp4ePath = path.join(WFRP4E_SCRIPTS, file); const frPath = path.join(FR_SCRIPTS, file); const backupPath = frPath + '.fr-backup'; try { // Sauvegarder la version FR avec traductions if (fs.existsSync(frPath)) { fs.copyFileSync(frPath, backupPath); } // Remplacer par la version WFRP4E if (fs.existsSync(wfrp4ePath)) { fs.copyFileSync(wfrp4ePath, frPath); replaced++; // Extraire les textes traduits de la sauvegarde pour référence const frContent = fs.readFileSync(backupPath, 'utf-8'); const translations = extractTranslations(frContent); replacedList.push({ index: index + 1, file, translations }); console.log(`✓ [${index + 1}/${scriptsToReview.length}] ${file}`); } else { errors.push(`Source non trouvée: ${file}`); console.log(`✗ [${index + 1}/${scriptsToReview.length}] ${file} - Source non trouvée`); } } catch (error) { errors.push(`${file}: ${error.message}`); console.log(`✗ [${index + 1}/${scriptsToReview.length}] ${file} - Erreur: ${error.message}`); } }); function extractTranslations(content) { const translations = []; // Patterns pour extraire les textes en français const patterns = [ /ui\.notifications\.(info|warn|error|notify)\s*\(\s*["'`]([^"'`]+)["'`]/g, /this\.script\.(scriptNotification|scriptMessage|notification)\s*\(\s*["'`]([^"'`]+)["'`]/g, /["'`](Chargement|Impossible|Êtes-vous|Voulez-vous|créé|modifié|supprimé|Erreur)[^"'`]*["'`]/gi, /warhammer\.utility\.findAllItems\([^,]+,\s*["'`]([^"'`]+)["'`]/g ]; patterns.forEach(pattern => { const regex = new RegExp(pattern.source, pattern.flags); let match; while ((match = regex.exec(content)) !== null) { const text = match[2] || match[1]; if (text && text.length > 3 && /[a-zàâäéèêëïîôöùûüÿçœæ]/i.test(text)) { if (!translations.includes(text)) { translations.push(text); } } } }); return translations; } // Générer le rapport let report = `# Scripts remplacés par les versions système\n\n`; report += `Date: ${new Date().toISOString()}\n\n`; report += `## Résumé\n\n`; report += `- **Scripts remplacés**: ${replaced}\n`; report += `- **Erreurs**: ${errors.length}\n\n`; report += `Tous les scripts ont été remplacés par leurs versions du système WFRP4E.\n`; report += `Les versions françaises avec traductions sont sauvegardées avec l'extension \`.fr-backup\`.\n\n`; if (errors.length > 0) { report += `## Erreurs\n\n`; errors.forEach(error => { report += `- ${error}\n`; }); report += `\n`; } report += `## Scripts à revoir manuellement\n\n`; report += `Pour chaque script, vous devez :\n`; report += `1. Comparer la version actuelle (système) avec le backup (.fr-backup)\n`; report += `2. Identifier les textes à traduire\n`; report += `3. Appliquer les traductions nécessaires\n\n`; report += `| # | Fichier | Traductions trouvées |\n`; report += `|---|---------|----------------------|\n`; replacedList.forEach(({index, file, translations}) => { const translationsText = translations.length > 0 ? translations.slice(0, 3).join(', ') + (translations.length > 3 ? '...' : '') : 'Aucune'; report += `| ${index} | \`${file}\` | ${translationsText} |\n`; }); report += `\n## Commandes utiles\n\n`; report += `### Comparer un script avec son backup\n\n`; report += `\`\`\`bash\n`; report += `# Avec meld\n`; report += `meld scripts/ scripts/.fr-backup\n\n`; report += `# Avec diff\n`; report += `diff -u scripts/.fr-backup scripts/\n`; report += `\`\`\`\n\n`; report += `### Exemples de scripts\n\n`; if (replacedList.length > 0) { const example = replacedList[0]; report += `Premier script: \`${example.file}\`\n\n`; report += `\`\`\`bash\n`; report += `meld scripts/${example.file} scripts/${example.file}.fr-backup\n`; report += `\`\`\`\n\n`; if (example.translations.length > 0) { report += `Traductions à réappliquer:\n`; example.translations.forEach(t => { report += `- "${t}"\n`; }); } } fs.writeFileSync(OUTPUT_FILE, report, 'utf-8'); console.log(`\n=== RÉSUMÉ ===`); console.log(`Scripts remplacés: ${replaced}/${scriptsToReview.length}`); console.log(`Erreurs: ${errors.length}`); console.log(`\nBackups créés avec extension: .fr-backup`); console.log(`Liste détaillée: ${OUTPUT_FILE}`); console.log(`\n=== PROCHAINES ÉTAPES ===`); console.log(`1. Consulter: ${OUTPUT_FILE}`); console.log(`2. Pour chaque script, comparer avec son .fr-backup`); console.log(`3. Réappliquer les traductions nécessaires`); console.log(`4. Supprimer les .fr-backup une fois terminé\n`);