Repository navigation
Expand file tree
/
Copy pathcheck-npm.sh
More file actions
executable file
·94 lines (82 loc) · 2.81 KB
/
Copy pathcheck-npm.sh
File metadata and controls
executable file
·94 lines (82 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env bash
set -euo pipefail
if command -v npm &> /dev/null; then
echo "✓ npm is installed: $(npm --version)"
exit 0
fi
echo "❌ npm is not installed!"
echo ""
# Determine the install command based on Linux distro
INSTALL_CMD=""
if command -v apt-get &> /dev/null; then
INSTALL_CMD="sudo apt-get update && sudo apt-get install -y nodejs npm"
DISTRO="Ubuntu/Debian"
elif command -v dnf &> /dev/null; then
INSTALL_CMD="sudo dnf install -y nodejs npm"
DISTRO="Azure Linux/Fedora"
fi
if [[ -z "$INSTALL_CMD" ]]; then
echo "❌ Unable to auto-install npm on this Linux distribution."
echo "Please install Node.js and npm from: https://nodejs.org/"
exit 1
fi
echo "Detected: $DISTRO"
echo "Install command: $INSTALL_CMD"
echo ""
# Check if we're in an interactive terminal
if [[ -t 0 ]]; then
# Interactive - ask for confirmation
read -p "Install npm automatically? [y/N] " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "❌ Installation cancelled. Please install npm manually:"
echo " $INSTALL_CMD"
exit 1
fi
else
# Non-interactive (CI/automation) - install automatically
echo "🤖 Non-interactive terminal detected - installing automatically..."
fi
echo "📦 Installing npm..."
# Retry logic for dpkg lock errors
MAX_RETRIES=5
RETRY_DELAY=60
attempt=1
while [ $attempt -le $MAX_RETRIES ]; do
if [ $attempt -gt 1 ]; then
echo "⏳ Attempt $attempt of $MAX_RETRIES (waiting ${RETRY_DELAY}s for dpkg lock to be released)..."
sleep $RETRY_DELAY
fi
if eval "$INSTALL_CMD" 2>&1 | tee /tmp/npm_install.log; then
echo ""
echo "✅ npm installed successfully: $(npm --version)"
rm -f /tmp/npm_install.log
exit 0
else
# Check if the error is related to dpkg lock
if grep -qE "Could not get lock|Unable to acquire the dpkg frontend lock|dpkg frontend lock" /tmp/npm_install.log; then
if [ $attempt -lt $MAX_RETRIES ]; then
echo "⚠️ dpkg lock is held by another process, retrying..."
else
echo ""
echo "❌ Installation failed after $MAX_RETRIES attempts."
echo "Another process is holding the dpkg lock. Please wait and try again, or install manually:"
echo " $INSTALL_CMD"
rm -f /tmp/npm_install.log
exit 1
fi
else
# Different error, don't retry
echo ""
echo "❌ Installation failed. Please install manually:"
echo " $INSTALL_CMD"
rm -f /tmp/npm_install.log
exit 1
fi
fi
((attempt++))
done
# Should not reach here, but just in case
echo "❌ Installation failed after $MAX_RETRIES attempts."
rm -f /tmp/npm_install.log
exit 1