-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnode-preparation.sh
executable file
·65 lines (59 loc) · 1.83 KB
/
node-preparation.sh
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
#!/bin/bash
set -eu
_validate_major_version() {
local major_version="${1:-}"
if [[ -z "$major_version" ]]; then
echo "Major version parameter is missing"
return 1
fi
if [[ ! "$major_version" =~ ^[0-9]+$ ]]; then
echo "Invalid major version parameter"
return 1
fi
return 0
}
is_node_major_version_installed() {
local major_version
local installed_version
major_version="$1"
_validate_major_version "$major_version" || return 1
installed_version=$(node -v 2>/dev/null | cut -d'.' -f1 | tr -d 'v')
if [[ -z "$installed_version" ]]; then
installed_version="0"
fi
if [ "$installed_version" -eq "$major_version" ]; then
return 0
fi
return 1
}
install_node_major_version() {
local major_version
major_version="$1"
_validate_major_version "$major_version" || return 1
apt-get update
apt-get install -y ca-certificates gnupg
mkdir -p /etc/apt/keyrings
curl -fsSL --retry 10 --retry-delay 10 https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$major_version.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
apt-get update
apt-get install nodejs -y
if is_node_major_version_installed "$major_version"; then
return 0
fi
return 1
}
ensure_node_major_version_installed() {
local major_version
major_version="$1"
_validate_major_version "$major_version" || return 1
if is_node_major_version_installed "$major_version"; then
echo "Node.js major version $major_version is already installed"
return 0
elif install_node_major_version "$major_version"; then
echo "Successfully installed Node.js major version $major_version"
return 0
fi
echo "Failed to install Node.js major version $major_version"
return 1
}
"$@"