-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
executable file
·219 lines (183 loc) · 4.69 KB
/
install.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/bin/bash
set -e
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
INSTALLER_PATH="/usr/local/bin/node_installer"
mkdir -p /usr/local/bin
cat > "$INSTALLER_PATH" << 'EOF'
#!/bin/bash
set -euo pipefail
readonly TMP_DIR=$( mktemp -d )
function clean_on_exit() {
rm -rf $TMP_DIR
}
function print_help() {
cat <<'eof'
Usage as root or with sudo:
node_installer 8.9.1 # this installs 8.9.1 version
node_installer v8.9.1 # this installs 8.9.1 version
node_installer v8.9.1 --verbose # enables set -x
node_installer v8.9.1 --yarn --npx # no-op options. Present for compatibility. Yarn and npx are always installed.
node_installer v8.9.1 --cache /path # stores fetched install files to cache location. Using those and not fetching if present.
node_installer clean # this cleans your system from older installations done via this script
node_installer help # prints this help
eof
}
function clean_previous_installations() {
echo "Cleaning previous node installations"
rm -f /usr/local/bin/node
rm -f /usr/local/bin/npm
rm -f /usr/local/bin/yarn /usr/local/bin/yarnpkg
rm -f /usr/local/bin/npx
rm -rf /usr/local/lib/node_modules
}
function download() {
local node=$1
local target=$2
echo "Downloading"
if `which wget > /dev/null` ; then
wget -q https://nodejs.org/dist/"$node"/"$target".tar.gz
elif `which curl > /dev/null` ; then
curl -sS -o "$target".tar.gz https://nodejs.org/dist/"$node"/"$target".tar.gz
else
echo "wget or curl command not found"
exit 4
fi || {
echo "node version not found"
echo "please provide existing version"
exit 5
}
}
function looks_like_version() {
[[ "$1" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+$ ]]
}
function handle_positional_argument() {
if [[ -z "${NODE:-}" ]] ; then
if looks_like_version "$1" ; then
NODE="$1"
else
echo "Does not look like version number: $1"
exit 1
fi
else
if looks_like_version "$1" ; then
echo "Provide only one node version: $NODE $1"
exit 1
else
echo "Unrecognized: $1"
exit 1
fi
fi
}
function main() {
[[ "$*" != *"--verbose"* ]] || set -x
trap clean_on_exit 0
if [[ "$1" == 'help' ]] || [[ -z "$1" ]] ; then
print_help
exit 0
fi
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
print_help
exit 1
fi
if [[ "$1" == clean ]] ; then
clean_previous_installations
exit 0
fi
# Defaults for options
local NODE INSTALL_FILES_CACHE
while [[ "$#" -ne 0 ]] ; do
case "$1" in
-c | --cache)
INSTALL_FILES_CACHE="$2"
[[ -n "${INSTALL_FILES_CACHE}" ]] || {
echo "Cache path is empty"
exit 1
}
echo "Setting cache folder: $INSTALL_FILES_CACHE"
shift 2
;;
-y | --yarn)
echo "Yarn is always installed. '--yarn' is deprecated."
shift
;;
-n | --npx)
echo "Npx is always installed. '--npx' is deprecated."
shift
;;
-v | --verbose)
echo "Verbose mode active."
shift
;;
*)
handle_positional_argument "$1"
shift
;;
esac
done
if [[ -z "${NODE}" ]] ; then
echo "Provide node version"
exit 1
fi
if ! [[ $NODE =~ ^v ]] ; then
NODE=v$NODE
fi
if [[ ! "$NODE" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] ; then
echo "Please provide existing node version in 'X.Y.Z' or 'vX.Y.Z' format"
exit 2
fi
readonly NODE
local TARGET=
case $(uname) in
"Linux")
TARGET="node-${NODE}-linux-x64"
;;
"Darwin")
TARGET="node-${NODE}-darwin-x64"
;;
*)
echo "System not supported"
exit 3
;;
esac
clean_previous_installations
if [[ -z "${INSTALL_FILES_CACHE:-}" ]] ; then
cd "$TMP_DIR"
download $NODE $TARGET
else
mkdir -p -- "$INSTALL_FILES_CACHE"
cd -- "$INSTALL_FILES_CACHE"
if [[ -f "$TARGET.tar.gz" ]] ; then
echo "Using $TARGET.tar.gz from cache"
else
echo "No $TARGET.tar.gz in cache"
rm -rf "$TARGET.tar.gz"
download $NODE $TARGET
fi
cp "$TARGET.tar.gz" "$TMP_DIR"
fi
echo "Installing"
cd "$TMP_DIR"
tar xf "$TARGET".tar.gz
rm -rf "$TARGET".tar.gz
cp -r "$TARGET"/bin/node /usr/local/bin/
mkdir -p /usr/local/lib
cp -r "$TARGET"/lib/node_modules /usr/local/lib/
cd /usr/local/bin
ln -s ../lib/node_modules/npm/bin/npm-cli.js npm
chmod +x node npm
echo "Linking npx"
rm -f npx
ln -s ../lib/node_modules/npm/bin/npx-cli.js npx
chmod +x npx
echo "Installing yarn"
npm install -g yarn >/dev/null
echo "Node version "$NODE" successfully installed"
}
main "$@"
EOF
chmod +x "$INSTALLER_PATH"
echo "node_installer successfully installed"