#!/bin/bash # Create .env.example templates from existing .env files # Removes actual secrets and replaces with placeholders set -euo pipefail # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' log_info() { echo -e "${BLUE}[INFO]${NC} $1"; } log_success() { echo -e "${GREEN}[✓]${NC} $1"; } log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; } log_error() { echo -e "${RED}[✗]${NC} $1"; } PROJECT_ROOT="${PROJECT_ROOT:-/home/intlc/projects}" DRY_RUN="${DRY_RUN:-true}" # Patterns to replace with placeholders declare -A SECRET_PATTERNS=( ["PRIVATE_KEY"]="your-private-key-here" ["API_KEY"]="your-api-key-here" ["API_TOKEN"]="your-api-token-here" ["SECRET"]="your-secret-here" ["PASSWORD"]="your-password-here" ["TOKEN"]="your-token-here" ["CLOUDFLARE_API_TOKEN"]="your-cloudflare-api-token" ["CLOUDFLARE_API_KEY"]="your-cloudflare-api-key" ["CLOUDFLARE_TUNNEL_TOKEN"]="your-cloudflare-tunnel-token" ["CLOUDFLARE_ORIGIN_CA_KEY"]="your-cloudflare-origin-ca-key" ["NPM_PASSWORD"]="your-npm-password" ["DATABASE_URL"]="postgresql://user:password@host:port/database" ["JWT_SECRET"]="your-jwt-secret-here" ) echo "═══════════════════════════════════════════════════════════" echo " Create .env.example Templates" echo "═══════════════════════════════════════════════════════════" echo "" log_info "Mode: $([ "$DRY_RUN" = "true" ] && echo "DRY RUN" || echo "LIVE")" echo "" # Find all .env files ENV_FILES=$(find "$PROJECT_ROOT" -type f -name ".env" ! -name "*.example" ! -path "*/node_modules/*" ! -path "*/.git/*" 2>/dev/null) CREATED=0 SKIPPED=0 while IFS= read -r env_file; do if [ -z "$env_file" ]; then continue fi example_file="${env_file}.example" # Skip if .example already exists and is newer if [ -f "$example_file" ] && [ "$example_file" -nt "$env_file" ]; then log_info "Skipping $env_file (example file is newer)" SKIPPED=$((SKIPPED + 1)) continue fi log_info "Processing: $env_file" if [ "$DRY_RUN" = "false" ]; then # Create .env.example by copying and sanitizing cp "$env_file" "$example_file" # Replace secrets with placeholders for pattern in "${!SECRET_PATTERNS[@]}"; do placeholder="${SECRET_PATTERNS[$pattern]}" # Handle different formats: KEY=value, KEY="value", KEY='value' sed -i "s/^${pattern}=.*/${pattern}=${placeholder}/" "$example_file" sed -i "s/^${pattern}=\".*\"/${pattern}=\"${placeholder}\"/" "$example_file" sed -i "s/^${pattern}='.*'/${pattern}='${placeholder}'/" "$example_file" done # Add header comment { echo "# Environment Variables Template" echo "# Copy this file to .env and fill in your actual values" echo "# DO NOT commit .env files to version control" echo "#" echo "" cat "$example_file" } > "${example_file}.tmp" mv "${example_file}.tmp" "$example_file" log_success " Created: $example_file" CREATED=$((CREATED + 1)) else log_info " Would create: $example_file" CREATED=$((CREATED + 1)) fi done <<< "$ENV_FILES" echo "" echo "═══════════════════════════════════════════════════════════" echo " Summary" echo "═══════════════════════════════════════════════════════════" echo "" if [ "$DRY_RUN" = "true" ]; then log_info "DRY RUN complete. Would create $CREATED template(s)" log_info "To create templates, run:" log_info " DRY_RUN=false $0" else log_success "Created $CREATED .env.example template(s)" log_info "Skipped $SKIPPED file(s) (already up to date)" fi echo ""