#!/bin/bash # --- Caricamento variabili d'ambiente --- if [ -f .env ]; then export $(grep -v '^#' .env | xargs) else echo "❌ Errore: File .env non trovato." exit 1 fi # --- Configurazione Target --- TARGET="contabo" echo "🎯 Target di deploy: $TARGET" # --- Validazione configurazione --- if [ -z "$VPS_HOST" ] || [ -z "$VPS_USER" ] || [ -z "$VPS_PATH" ]; then echo "❌ Errore: VPS_HOST, VPS_USER o VPS_PATH non definiti nel file .env" exit 1 fi # --- Aggiornamento Versione --- VERSION=$(date +'%Y.%m.%d.%H%M') echo "export const VERSION = '$VERSION';" > src/app/version.ts echo "🏷️ Versione aggiornata a: $VERSION" # --- Configurazione Email e API Parametriche --- node -e " const fs = require('fs'); const envEmail = process.env.CONTACT_EMAIL || 'info@canticristiani.it'; const apiUser = process.env.API_AUTH_USER || 'canti'; const apiPass = process.env.API_AUTH_PASS || 'antani2026'; ['src/environments/environment.ts', 'src/environments/environment.prod.ts'].forEach(file => { if (fs.existsSync(file)) { let content = fs.readFileSync(file, 'utf8'); content = content.replace(/contactEmail:\s*'[^']*'/g, \`contactEmail: '\${envEmail}'\`); content = content.replace(/apiAuthUser:\s*'[^']*'/g, \`apiAuthUser: '\${apiUser}'\`); content = content.replace(/apiAuthPass:\s*'[^']*'/g, \`apiAuthPass: '\${apiPass}'\`); fs.writeFileSync(file, content, 'utf8'); console.log(\`📧 Aggiornate variabili di ambiente in \${file}\`); } }); " # --- Build della PWA (base-href=/ per installare nella root) --- echo "📦 1/2 Build della PWA (Production) per ROOT (www.canticristiani.it) in corso..." npm run build -- --configuration=production --base-href=/ || { echo "❌ Errore nella build"; exit 1; } if [ ! -d "www" ]; then echo "❌ Errore: Cartella 'www' non trovata." exit 1 fi # --- Genera version.json per il polling PWA --- BUILD_TIMESTAMP=$(date +%s)000 echo "{\"version\":\"$VERSION\",\"buildTime\":$BUILD_TIMESTAMP}" > www/version.json echo "📄 Generato version.json: v$VERSION (timestamp: $BUILD_TIMESTAMP)" # --- Upload --- echo "🚀 Upload via SSH/rsync su Contabo ($VPS_HOST) in corso..." rsync -avz --delete --exclude 'api' www/ "$VPS_USER@$VPS_HOST:$VPS_PATH"