From 34df89c1ac833d098023808df524d244bd719559 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Thu, 4 Dec 2025 17:01:51 +0100 Subject: [PATCH] Update deploy-to-opt.sh to enhance npm cache management by excluding .npm and .npm-cache directories, and setting a custom npm cache directory during dependency installation and client build. --- deploy-to-opt.sh | 20 ++++++++++++++--- fix-npm-cache.sh | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) create mode 100755 fix-npm-cache.sh diff --git a/deploy-to-opt.sh b/deploy-to-opt.sh index 549d0f7..8b1b250 100755 --- a/deploy-to-opt.sh +++ b/deploy-to-opt.sh @@ -38,7 +38,7 @@ if [ ! -d "$TARGET_DIR" ]; then echo "✓ Verzeichnis erstellt" fi -# Kopiere Dateien (ausschließlich node_modules, .git, dist, logs) +# Kopiere Dateien (ausschließlich node_modules, .git, dist, logs, npm cache) echo "Kopiere Dateien nach $TARGET_DIR..." rsync -av --progress \ --exclude 'node_modules' \ @@ -48,6 +48,8 @@ rsync -av --progress \ --exclude 'docroot/dist' \ --exclude 'logs' \ --exclude '.env' \ + --exclude '.npm-cache' \ + --exclude '.npm' \ "$SOURCE_DIR/" "$TARGET_DIR/" echo "✓ Dateien kopiert" @@ -62,10 +64,22 @@ echo "" echo "Führe Installation in $TARGET_DIR durch..." cd "$TARGET_DIR" +# Repariere npm Cache für www-data falls nötig +echo "" +echo "Prüfe npm Cache-Berechtigungen..." +if [ -d "/var/www/.npm" ]; then + chown -R $USER:$GROUP "/var/www/.npm" 2>/dev/null || true +fi + +# Setze npm Cache auf ein Verzeichnis im App-Verzeichnis +NPM_CACHE_DIR="$TARGET_DIR/.npm-cache" +mkdir -p "$NPM_CACHE_DIR" +chown -R $USER:$GROUP "$NPM_CACHE_DIR" + # Installiere Dependencies echo "" echo "Installiere Dependencies..." -sudo -u $USER bash -c "cd '$TARGET_DIR' && npm run install:all" +sudo -u $USER bash -c "cd '$TARGET_DIR' && npm config set cache '$NPM_CACHE_DIR' && npm run install:all" if [ $? -ne 0 ]; then echo "FEHLER: Installation der Dependencies fehlgeschlagen!" @@ -77,7 +91,7 @@ echo "✓ Dependencies installiert" # Baue Client echo "" echo "Baue Client für Production..." -sudo -u $USER bash -c "cd '$TARGET_DIR' && npm run build" +sudo -u $USER bash -c "cd '$TARGET_DIR' && npm config set cache '$NPM_CACHE_DIR' && cd client && npm config set cache '$NPM_CACHE_DIR' && cd .. && npm run build" if [ $? -ne 0 ]; then echo "FEHLER: Build fehlgeschlagen!" diff --git a/fix-npm-cache.sh b/fix-npm-cache.sh new file mode 100755 index 0000000..6fcf96d --- /dev/null +++ b/fix-npm-cache.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +# Fix npm Cache-Berechtigungen für /opt/ypchat + +set -e + +TARGET_DIR="/opt/ypchat" +USER="www-data" +GROUP="www-data" + +echo "==========================================" +echo "npm Cache-Berechtigungen reparieren" +echo "==========================================" + +# Prüfe ob als root ausgeführt +if [ "$EUID" -ne 0 ]; then + echo "FEHLER: Dieses Skript muss als root ausgeführt werden!" + echo "Bitte führe aus: sudo ./fix-npm-cache.sh" + exit 1 +fi + +# Erstelle lokales npm Cache-Verzeichnis +NPM_CACHE_DIR="$TARGET_DIR/.npm-cache" +echo "Erstelle lokales npm Cache-Verzeichnis: $NPM_CACHE_DIR" +mkdir -p "$NPM_CACHE_DIR" +chown -R $USER:$GROUP "$NPM_CACHE_DIR" +echo "✓ Cache-Verzeichnis erstellt" + +# Setze npm Cache für root (falls nötig) +echo "Setze npm Cache-Konfiguration..." +npm config set cache "$NPM_CACHE_DIR" --global 2>/dev/null || true + +# Setze npm Cache für www-data +echo "Setze npm Cache für $USER..." +sudo -u $USER bash -c "npm config set cache '$NPM_CACHE_DIR'" +sudo -u $USER bash -c "cd '$TARGET_DIR/client' && npm config set cache '$NPM_CACHE_DIR'" + +echo "✓ npm Cache konfiguriert" + +# Optional: Repariere /var/www/.npm falls vorhanden +if [ -d "/var/www/.npm" ]; then + echo "Repariere /var/www/.npm Berechtigungen..." + chown -R $USER:$GROUP "/var/www/.npm" 2>/dev/null || true + echo "✓ /var/www/.npm repariert" +else + echo "ℹ /var/www/.npm existiert nicht (ok, wird nicht benötigt)" +fi + +echo "" +echo "==========================================" +echo "npm Cache repariert!" +echo "==========================================" +echo "" +echo "Du kannst jetzt die Installation fortsetzen:" +echo " cd $TARGET_DIR" +echo " sudo -u $USER npm run install:all" +echo "" +