เป็น script ที่เขียนให้ไล่ดูใน workspace แล้ว build npm ( JavaScript ) ใหม่ถ้า code มีการเปลี่ยนแปลง
cd-npm-all-projects.sh
#!/bin/bash
set -e
# Record the start time
START_TIME=$(date +%s)
echo "=========================================="
echo "📦 Scanning for NPM / Frontend Projects..."
echo "=========================================="
ROOT_DIR="$(pwd)"
FAILED_LOG="$ROOT_DIR/.failed_npm_projects.log"
WARNINGS_LOG="$ROOT_DIR/.npm_build_warnings.log"
ERRORS_LOG="$ROOT_DIR/.npm_build_errors.log"
# Clear temporary log files before starting
> "$FAILED_LOG"
> "$WARNINGS_LOG"
> "$ERRORS_LOG"
# Function to log warnings
add_warning() {
local msg="$1"
echo "⚠️ $msg"
echo "$msg" >> "$WARNINGS_LOG"
}
# Encapsulate build logic in a function to allow retries
process_project() {
local dir="$1"
local is_retry="$2"
# Use absolute path to prevent directory traversal issues during retry
cd "$ROOT_DIR/$dir" || return 1
# Check if it is an NPM project
if [ -f "package.json" ]; then
if [ "$is_retry" == "true" ]; then
echo "🔄 [Retry] Checking Frontend in '$dir'..."
else
echo "🔍 Checking Frontend in '$dir'..."
fi
# Case 1: node_modules is missing
if [ ! -d "node_modules" ]; then
add_warning "[$dir] node_modules missing. Installing dependencies..."
npm install --legacy-peer-deps > .npm-install.log 2>&1 &
NPM_PID=$!
while kill -0 $NPM_PID 2>/dev/null; do sleep 5; echo "⏳ [$dir] NPM Installing..."; done
wait $NPM_PID
if [ $? -ne 0 ]; then
cat .npm-install.log
rm -f .npm-install.log
if [ "$is_retry" == "true" ]; then
echo "[$dir] NPM Install failed during retry." >> "$ERRORS_LOG"
else
echo "$dir" >> "$FAILED_LOG"
fi
return 1
fi
rm -f .npm-install.log
echo "✅ [$dir] NPM Install Complete."
# Case 2: package.json is newer than node_modules folder
elif [ "package.json" -nt "node_modules" ]; then
add_warning "[$dir] package.json updated. Updating dependencies..."
npm install --legacy-peer-deps > .npm-update.log 2>&1 &
NPM_PID=$!
while kill -0 $NPM_PID 2>/dev/null; do sleep 5; echo "⏳ [$dir] NPM Updating..."; done
wait $NPM_PID
if [ $? -ne 0 ]; then
cat .npm-update.log
rm -f .npm-update.log
if [ "$is_retry" == "true" ]; then
echo "[$dir] NPM Update failed during retry." >> "$ERRORS_LOG"
else
echo "$dir" >> "$FAILED_LOG"
fi
return 1
fi
rm -f .npm-update.log
echo "✅ [$dir] NPM Update Complete."
# Case 3: Up to date
else
[ "$is_retry" != "true" ] && echo "✅ [$dir] Node dependencies up to date."
fi
fi
return 0
}
pids=()
# Phase 1: Run all projects in parallel
for dir in */; do
(
process_project "$dir" "false"
) &
pids+=($!)
done
# Wait for all parallel tasks in the first phase to complete
if [ ${#pids[@]} -gt 0 ]; then
echo "⏳ Waiting for NPM tasks to finish..."
wait "${pids[@]}" || true
fi
# Phase 2: Retry failed projects sequentially
if [ -s "$FAILED_LOG" ]; then
echo "=========================================="
echo "🔄 Retrying Failed NPM Projects..."
echo "=========================================="
sort -u "$FAILED_LOG" | while read -r failed_dir; do
if [ -n "$failed_dir" ]; then
process_project "$failed_dir" "true"
fi
done
fi
# ==========================================
# Calculate Execution Time
# ==========================================
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
MINS=$((DURATION / 60))
SECS=$((DURATION % 60))
# ==========================================
# Build Summary
# ==========================================
echo "=========================================="
echo "📊 NPM BUILD SUMMARY"
echo "=========================================="
echo "⏱️ TOTAL TIME: ${MINS}m ${SECS}s"
echo "------------------------------------------"
HAS_ERRORS=false
if [ -s "$ERRORS_LOG" ]; then
HAS_ERRORS=true
echo "❌ ERRORS FOUND (Projects that failed even after retry):"
cat "$ERRORS_LOG"
echo "------------------------------------------"
fi
HAS_WARNINGS=false
if [ -s "$WARNINGS_LOG" ]; then
HAS_WARNINGS=true
echo "⚠️ WARNINGS / ACTIONS TAKEN:"
cat "$WARNINGS_LOG"
echo "------------------------------------------"
fi
echo "=========================================="
# Evaluate final status and exit code
if [ "$HAS_ERRORS" = true ]; then
echo "❌ NPM BUILD FAILED (See errors above)"
EXIT_STATUS=1
elif [ "$HAS_WARNINGS" = true ]; then
echo "✅ NPM BUILD SUCCESS WITH WARNINGS"
EXIT_STATUS=0
else
echo "✅ NPM BUILD SUCCESS"
EXIT_STATUS=0
fi
echo "=========================================="
# Clean up temporary log files
rm -f "$FAILED_LOG" "$WARNINGS_LOG" "$ERRORS_LOG"
exit $EXIT_STATUS
วิธีใช้คือ เรียกใน workspace หรือ รวมกับ script ตัวอื่น ๆ
อ่านเพิ่มเติม