#!/usr/bin/env node
/**
* tools/upgrade-gws-refonte.js
*
* Script de correction et d'optimisation avancée :
* 1. Empêche le downgrade accidentel via l'API live (vérification Semver).
* 2. Résout le conflit de routage SPA sur les ancres internes #spa-gws-*.
* 3. Ajoute un helper universel de presse-papier avec fallback document.execCommand.
* 4. Anime la pastille cyan pulsante dans la barre de navigation.
* 5. Intègre une barre de sous-navigation rapide dans le sas SPA #gws.
* 6. Ajoute le bouton de téléchargement du paquet .gws sur la fiche #app-weather-studio.
* 7. Ajoute les données structurées SoftwareApplication JSON-LD dans gws/index.html.
* 8. Nettoie sitemap.xml pour se conformer strictement aux standards des moteurs de recherche (pas d'ancres #).
* 9. Synchronise intégralement _Site et Actual_Website.
* 10. Re-génère les archives Brotli 11 et Gzip 9.
*/
const fs = require('fs');
const path = require('path');
const SITE_DIR = path.resolve(__dirname, '..');
const ACTUAL_DIR = path.resolve(SITE_DIR, '../Actual_Website');
console.log('[UPGRADE] Début de la mise à jour...');
function replaceWithEol(source, searchStr, replaceStr, eol) {
const normalizedSource = source.replace(/\r\n/g, '\n');
const normalizedSearch = searchStr.replace(/\r\n/g, '\n');
const normalizedReplace = replaceStr.replace(/\r\n/g, '\n');
if (!normalizedSource.includes(normalizedSearch)) {
return null;
}
const result = normalizedSource.replace(normalizedSearch, normalizedReplace);
return eol === '\r\n' ? result.replace(/\n/g, '\r\n') : result;
}
// =========================================================================
// 1. MISE À JOUR DE _Site/gws/index.html
// =========================================================================
const GWS_HTML_PATH = path.join(SITE_DIR, 'gws/index.html');
let gwsHtml = fs.readFileSync(GWS_HTML_PATH, 'utf8');
const gwsEol = gwsHtml.includes('\r\n') ? '\r\n' : '\n';
// A. Ajout du Schema.org SoftwareApplication JSON-LD si pas déjà présent
if (!gwsHtml.includes('"SoftwareApplication"')) {
const jsonLdGws = `
`;
gwsHtml = gwsHtml.replace('[\s\S]*?checkLatestVersion\(\);\s*\}\)\(\);\s*<\/script>/;
const newGwsScript = ``;
if (oldGwsScriptRegex.test(gwsHtml)) {
gwsHtml = gwsHtml.replace(oldGwsScriptRegex, newGwsScript);
console.log('✓ gws/index.html : Script interactif mis à jour avec Semver, copy helper et fix encodage.');
}
fs.writeFileSync(GWS_HTML_PATH, gwsHtml, 'utf8');
// =========================================================================
// 2. MISE À JOUR DE _Site/index.html
// =========================================================================
const INDEX_HTML_PATH = path.join(SITE_DIR, 'index.html');
let indexHtml = fs.readFileSync(INDEX_HTML_PATH, 'utf8');
const indexEol = indexHtml.includes('\r\n') ? '\r\n' : '\n';
// A. Routeur SPA go() et ancres spa-gws-*
const oldGoRouteCheck = ` var targetScroll=null;
if(route==='news' || route==='carnet'){ targetScroll='news'; route='realisations'; }
if(route==='application') route='apps';
if(route==='telechargement') route='gws';
if(legalTab) route='termes';`;
const newGoRouteCheck = ` var targetScroll=null;
if(route==='news' || route==='carnet'){ targetScroll='news'; route='realisations'; }
if(route==='application') route='apps';
if(route==='telechargement') route='gws';
if(route && route.indexOf('spa-gws-') === 0){ targetScroll=route; route='gws'; }
if(legalTab) route='termes';`;
const resGo = replaceWithEol(indexHtml, oldGoRouteCheck, newGoRouteCheck, indexEol);
if (resGo) {
indexHtml = resGo;
console.log('✓ index.html : Routeur SPA adapté pour les ancres spa-gws-*.');
} else {
console.log('ℹ Routeur SPA déjà à jour ou déjà patché.');
}
// B. Click delegation pour liens d'ancres internes
const oldClickDelegation = `document.addEventListener('click',function(e){
var a=e.target.closest('a[data-spa]'); if(!a) return;
e.preventDefault(); location.hash=a.getAttribute('data-spa');
});`;
const newClickDelegation = `document.addEventListener('click',function(e){
var aSpa=e.target.closest('a[data-spa]');
if(aSpa){
e.preventDefault();
location.hash=aSpa.getAttribute('data-spa');
return;
}
var anchor=e.target.closest('a[href^="#"]');
if(anchor){
var href=anchor.getAttribute('href');
if(href && href.length > 1){
var targetId=href.substring(1);
if(targetId && !ROUTES.includes(targetId) && !LEGAL_TABS.hasOwnProperty(targetId)){
var targetEl=document.getElementById(targetId);
if(targetEl){
e.preventDefault();
targetEl.scrollIntoView({behavior:'smooth'});
}
}
}
}
});`;
const resClick = replaceWithEol(indexHtml, oldClickDelegation, newClickDelegation, indexEol);
if (resClick) {
indexHtml = resClick;
console.log('✓ index.html : Gestionnaire de clics adapté pour les ancres internes.');
} else {
console.log('ℹ Gestionnaire de clics déjà à jour ou déjà patché.');
}
fs.writeFileSync(INDEX_HTML_PATH, indexHtml, 'utf8');
// =========================================================================
// 3. MISE À JOUR DE sitemap.xml (SEO strict sans fragments d'URL)
// =========================================================================
const SITEMAP_PATH = path.join(SITE_DIR, 'sitemap.xml');
const cleanSitemap = `
https://gastinel.me/
2026-09-23
weekly
1.0
https://gastinel.me/assets/img/dji-fly-20250919-194610-0004-1758304172909-photo.webp
Horizon flamboyant au crépuscule dans le massif du Vercors
Prise de vue aérienne au drone par Théo Gastinel
https://gastinel.me/gws/
2026-09-23
weekly
0.95
https://gastinel.me/assets/apps/gastinel-weather-studio/studio.webp
Gastinel Weather Studio — Station logicielle d'analyse microclimatique
Interface de comparaison multi-séries thermiques et TAF du Vercors
`;
fs.writeFileSync(SITEMAP_PATH, cleanSitemap, 'utf8');
console.log('✓ sitemap.xml : Nettoyé selon les standards stricts Google/Bing (URLs canoniques sans fragments).');
// =========================================================================
// 4. SYNCHRONISATION VERS Actual_Website
// =========================================================================
if (fs.existsSync(ACTUAL_DIR)) {
console.log(`[SYNCHRO] Synchronisation vers ${ACTUAL_DIR}...`);
fs.writeFileSync(path.join(ACTUAL_DIR, 'index.html'), indexHtml, 'utf8');
const actualGwsDir = path.join(ACTUAL_DIR, 'gws');
if (!fs.existsSync(actualGwsDir)) fs.mkdirSync(actualGwsDir, { recursive: true });
fs.writeFileSync(path.join(actualGwsDir, 'index.html'), gwsHtml, 'utf8');
// Copie sitemap & llms
fs.writeFileSync(path.join(ACTUAL_DIR, 'sitemap.xml'), cleanSitemap, 'utf8');
const llmsContent = fs.readFileSync(path.join(SITE_DIR, 'llms.txt'), 'utf8');
fs.writeFileSync(path.join(ACTUAL_DIR, 'llms.txt'), llmsContent, 'utf8');
console.log('✓ Actual_Website synchronisé avec succès.');
}
// =========================================================================
// 5. RECOMPRESSION BROTLI 11 & GZIP 9
// =========================================================================
console.log('[COMPRESSION] Lancement de tools/precompress.js...');
require('./precompress.js');
console.log('🎉 TOUTES LES ÉTAPES ONT ÉTÉ EXÉCUTÉES AVEC SUCCÈS !');