#!/usr/bin/env node /* * Inietta i tag PWA negli di tutte le pagine HTML sotto public/. * Idempotente: salta i file che contengono gia' il marker . * Normalizza il viewport mantenendo lo zoom abilitato (WCAG 1.4.4 / AGID). * Esegue solo modifiche additive e mirate; tutto e' git-tracked => reversibile. * * Uso: node scripts/inject-pwa-head.mjs */ import fs from 'node:fs'; import path from 'node:path'; const ROOT = path.resolve(process.cwd(), 'public'); const EXCLUDE_BASENAMES = new Set(['offline.html']); const MARKER = ''; const VIEWPORT = ''; const BLOCK = [ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' ].join('\n'); function walk(dir) { let out = []; for (const e of fs.readdirSync(dir, { withFileTypes: true })) { const full = path.join(dir, e.name); if (e.isDirectory()) out = out.concat(walk(full)); else if (e.isFile() && e.name.toLowerCase().endsWith('.html')) out.push(full); } return out; } const files = walk(ROOT).filter((f) => { const b = path.basename(f); if (EXCLUDE_BASENAMES.has(b)) return false; if (/copy/i.test(b)) return false; return true; }); let modified = 0, already = 0, nohead = 0, viewportFixed = 0; const anomalies = []; for (const f of files) { let html = fs.readFileSync(f, 'utf8'); if (html.includes(MARKER)) { already++; continue; } if (!/<\/head>/i.test(html)) { nohead++; anomalies.push('NO : ' + path.relative(process.cwd(), f)); continue; } // Normalizza il viewport (zoom SEMPRE consentito: no user-scalable=no / maximum-scale) const vp = html.match(/]*>/i); if (vp) { if (!/viewport-fit=cover/i.test(vp[0])) { html = html.replace(vp[0], VIEWPORT); viewportFixed++; } } const inject = (vp ? '' : (' ' + VIEWPORT + '\n')) + BLOCK + '\n'; // replace con funzione per evitare interpretazione di pattern '$' nel testo iniettato html = html.replace(/<\/head>/i, () => inject + ''); fs.writeFileSync(f, html); modified++; } console.log('HTML trovati:', files.length); console.log('Modificati:', modified, '| gia col marker:', already, '| senza :', nohead, '| viewport normalizzati:', viewportFixed); if (anomalies.length) { console.log('Anomalie:'); anomalies.forEach((a) => console.log(' -', a)); }