Files
Claude 0d0bebf708 fix: correzioni della revisione finale (testi, visuale, brief, codice)
- testi: «lo Studio» uniforme, «Centro autorizzato CAF», ripetizioni tolte
  nei servizi e in «Lo studio», valori senza superlativi, meta corrette,
  testo della mappa di §10.2, numeri e indirizzi con spazi non separabili
- visuale: breadcrumb, anello della banda CTA, card dei professionisti,
  pager e sidebar dei servizi su mobile, pagine legali a colonna singola,
  pulsante e-mail su una riga, card sedi allineate
- dati: fatti della sede di Asti da studio.ts/testi.ts, sede.telefono
- 404 e conferma senza canonical; avviso mappa senza JavaScript
- controlli: CSP per direttive (endpoint), anno da SOURCE_DATE_EPOCH,
  URL del sito controllati sulla build locale, import file:// e tar da
  stdin per Windows, .gitattributes, porta libera corretta
- modulo: pattern del telefono valido, avviso visibile, Esc sul
  sottomenu aperto al passaggio del mouse; contact.php con -f, senza
  mbstring obbligatorio, fuso Europe/Rome
- release: note --shell-safe, ripiego REST automatico, RELEASE_TOKEN;
  deploy di esempio senza esclusione di contact.php; nginx /privacy/
- DECISIONS D-040–D-044, TODO C4 e riferimenti di riga aggiornati

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016zJ9ZWGKvoKoxYwbmnRKdJ
2026-10-16 00:13:01 +00:00

71 lines
2.2 KiB
JavaScript

/**
* Avvia `astro preview` su una porta libera e restituisce l'URL base e una funzione di stop.
* Usato dagli script che devono navigare il sito compilato (link, screenshot).
*/
import { spawn } from 'node:child_process';
import { existsSync } from 'node:fs';
import { createServer } from 'node:net';
import { resolve } from 'node:path';
export const root = resolve(import.meta.dirname, '..', '..');
async function freePort(preferred) {
const tryPort = (port) =>
new Promise((ok) => {
const srv = createServer();
srv.once('error', () => ok(null));
// con la porta 0 il sistema assegna una porta libera: va letta da address()
srv.listen(port, '127.0.0.1', () => {
const assigned = srv.address().port;
srv.close(() => ok(assigned));
});
});
return (await tryPort(preferred)) ?? (await tryPort(0));
}
export async function startPreview({ port = 4322 } = {}) {
if (!existsSync(resolve(root, 'dist', 'index.html'))) {
throw new Error('Cartella dist/ assente: esegui prima `npm run build`.');
}
const p = await freePort(port);
const astroBin = resolve(root, 'node_modules', 'astro', 'bin', 'astro.mjs');
const child = spawn(
process.execPath,
[astroBin, 'preview', '--host', '127.0.0.1', '--port', String(p), '--ignore-lock'],
{
cwd: root,
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env, ASTRO_TELEMETRY_DISABLED: '1' },
},
);
let output = '';
child.stdout.on('data', (d) => (output += d));
child.stderr.on('data', (d) => (output += d));
const base = `http://127.0.0.1:${p}`;
const deadline = Date.now() + 30_000;
for (;;) {
try {
const res = await fetch(`${base}/`);
if (res.ok) break;
} catch {
// server non ancora pronto
}
if (child.exitCode !== null || Date.now() > deadline) {
child.kill();
throw new Error(`Impossibile avviare astro preview:\n${output}`);
}
await new Promise((r) => setTimeout(r, 250));
}
return {
base,
stop: () =>
new Promise((ok) => {
if (child.exitCode !== null) return ok();
child.once('exit', () => ok());
child.kill('SIGTERM');
}),
};
}