#!/usr/bin/env bash # Deploy frontend: build and atomic publish to OpenResty site directory with timestamped backup # Usage: # SITE_DIR=/data/1panel/1panel/apps/openresty/openresty/www/sites/www.daoyou.dev/index ./scripts/deploy_frontend.sh # Notes: # - SITE_DIR can be customized by environment variable. Default to /data/... path provided. set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd)" FRONTEND_DIR="$ROOT_DIR/frontend" DEFAULT_SITE_DIR="/data/1panel/1panel/apps/openresty/openresty/www/sites/www.daoyou.dev/index" SITE_DIR="${SITE_DIR:-$DEFAULT_SITE_DIR}" cd "$FRONTEND_DIR" echo "[frontend] Building production assets..." npm run build TS="$(date +%Y%m%d%H%M%S)" BASE_DIR="$(dirname "$SITE_DIR")" BACKUP_DIR="$BASE_DIR/index$TS" # Backup existing site dir if exists if [ -d "$SITE_DIR" ] || [ -f "$SITE_DIR" ]; then echo "[frontend] Backing up existing site to: $BACKUP_DIR" mv "$SITE_DIR" "$BACKUP_DIR" fi # Recreate directory mkdir -p "$SITE_DIR" # Publish dist cp -r "$FRONTEND_DIR/dist"/* "$SITE_DIR"/ echo "[frontend] Published to: $SITE_DIR" echo "[frontend] Backup: $BACKUP_DIR (if existed)"