-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·357 lines (302 loc) · 10.2 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env bash
set -Eeuo pipefail
declare -r PREFIX=/usr/nodejs
UNAME=$(uname -a) || die "uname -a failed."
declare -r BIN="${PREFIX}/bin/node"
declare -r NPM="${PREFIX}/bin/npm"
declare -r PNPM="${PREFIX}/bin/pnpm"
declare -r YARN="${PREFIX}/bin/yarn"
INSTALL_VERSION="${1-latest}"
if ! [[ "${TMPDIR-}" ]]; then
export TMPDIR="/tmp"
fi
declare -r TMP_VERSION="$TMPDIR/nodejs-version-$INSTALL_VERSION.txt"
declare -r TMP_INDEX="$TMPDIR/nodejs-versions-list.txt"
function msg() {
echo -e "$*" >&2
}
function die() {
msg "$@"
exit 1
}
function check_system() {
echo "$UNAME" | grep -iq "${1}" 2>/dev/null
}
function command_exists() {
local -r PATH="$PATH:$PREFIX/bin"
command -v "$1" &>/dev/null
}
function do_system_check() {
command_exists wget || die "command 'wget' not found, please install it"
command_exists dirname || die "command 'dirname' not found, please install coreutils"
command_exists tar || die "command 'tar' not found, please install it"
command_exists gzip || die "command 'gzip' not found, please install it"
if command_exists node && [[ "$(command -v node)" != "$BIN" ]]; then
msg "\e[38;5;9mAnother node.js installed at $(command -v node)!\e[0m"
msg " this will cause error!"
exit 1
fi
if wget --help 2>&1 | grep -q -- 'wget2'; then
msg "using wget2"
function _wget() {
wget --continue --quiet --progress=bar --force-progress "$1" -O "$2"
}
elif wget --help 2>&1 | grep -q -- '--show-progress'; then
msg "using legacy wget"
function _wget() {
wget --continue --quiet --show-progress --progress=bar:force:noscroll "$1" -O "$2"
}
else
msg "using busybox wget"
function _wget() {
wget -c -q "$1" -O "$2"
}
fi
}
function download_file() {
local url="$1" temp
temp="$DOWNLOAD/$(basename "${url}")"
local save_at=${2-"$temp"}
msg "Download file from $url:"
msg " to: $save_at"
if [[ -e $save_at ]]; then
msg " use cached file."
else
_wget "$url" "${save_at}.downloading" || die "Cannot download."
mv "${save_at}.downloading" "${save_at}"
msg " saved at ${save_at}"
fi
if [[ -z ${2-""} ]]; then
echo "$save_at"
fi
}
function replace_line() {
local FILE="$1" KEY="$2" RESULT="$3" OLD MID
OLD=$(<"$FILE")
MID=$(echo "$OLD" | sed -E "s#^${KEY}[ =].+\$#__REPLACE_LINE__#g")
if [[ $MID == "$OLD" ]]; then
local NEW="$OLD
$RESULT
"
else
local NEW=${MID/__REPLACE_LINE__/"$RESULT"}
fi
if [[ $OLD != "$NEW" ]]; then
msg "modify file '$FILE'\n \e[2m$RESULT\e[0m"
echo "$NEW" >"$FILE"
fi
}
function rebuild_global_packages() {
local ITEMS=()
local i
local j
cd "$1"
for i in */; do
i=${i%/}
if echo "$i" | grep -qE '^@' &>/dev/null; then
for j in "$i"/*/; do
j=${j%/}
if [ ! -L "$j" ]; then
ITEMS+=("$j")
fi
done
elif [ ! -L "$i" ]; then
ITEMS+=("$i")
fi
done
$NPM rebuild "${ITEMS[@]}" || {
msg -e "\e[38;5;11mGlobal packages not rebuilt. This may or may not cause error.\e[0m"
msg " the command is: $NPM rebuild ${ITEMS[*]}"
}
}
function install_nodejs() {
msg "fetch version '$INSTALL_VERSION': "
if [[ $INSTALL_VERSION == "latest" ]]; then
download_file "https://nodejs.org/dist/latest/" "${TMP_VERSION}"
elif [[ $INSTALL_VERSION -gt 0 ]]; then
download_file "https://nodejs.org/dist/" "${TMP_INDEX}"
LATEST_SUB_VERSION=$(grep -Eo ">v${INSTALL_VERSION}\.[0-9]+\.[0-9]+/<" "${TMP_INDEX}" | grep -Eo "v${INSTALL_VERSION}\.[0-9]+\.[0-9]+" | sort --version-sort | tail -n1) \
|| die "not found version $INSTALL_VERSION. (file has saved at '$TMP_INDEX')"
msg "latest version of v$INSTALL_VERSION is $LATEST_SUB_VERSION"
download_file "https://nodejs.org/dist/$LATEST_SUB_VERSION/" "${TMP_VERSION}"
INSTALL_VERSION="$LATEST_SUB_VERSION"
else
die "requested install version ($INSTALL_VERSION) is not valid."
fi
msg " -> ok."
if check_system darwin; then
PACKAGE_TAG="darwin"
elif check_system cygwin; then
PACKAGE_TAG="win"
elif check_system linux; then
PACKAGE_TAG="linux"
else
die "only support: Darwin(Mac OS), Cygwin, Linux (RHEL & WSL)\n * windows use install.ps1"
fi
msg " * system name: ${PACKAGE_TAG}"
NODE_PACKAGE=$(grep -Eo 'href="node-v[0-9.]+-'${PACKAGE_TAG}'-x64.tar.xz"' "$TMP_VERSION" | sed 's/^href="//; s/"$//') \
|| die "failed to detect nodejs version from downloaded html. (file has saved at '$TMP_VERSION')"
msg " * package name: ${NODE_PACKAGE}"
if [[ -e $BIN ]]; then
msg "old nodejs exists."
if echo "${NODE_PACKAGE}" | grep -q "$($BIN -v)"; then
rm "$TMP_VERSION" || true
msg "official node.js not updated:"
msg " current version: $($BIN -v)"
if [[ ${FORCE+found} != found ]] || [[ ${FORCE} != yes ]]; then
msg "set environment 'FORCE=yes' to reinstall"
return
fi
msg " FORCE UPDATE!"
else
msg "official node.js updated:"
msg " current version: $($BIN -v)"
fi
fi
msg "Installing NodeJS..."
NODEJS_ZIP_FILE=$(download_file "https://nodejs.org/dist/$INSTALL_VERSION/${NODE_PACKAGE}")
msg " extracting file:"
tar xf "$NODEJS_ZIP_FILE" --strip-components=1 -C "$PREFIX" || die " -> \e[38;5;9mfailed\e[0m."
msg " -> ok."
if [[ -e $BIN ]]; then
V=$($BIN -v 2>&1) || die "emmmmmm... binary file '$BIN' is not executable. that's weird."
msg " * node.js: $V"
else
die "error... something wrong... no '$BIN' after extract."
fi
}
function create_nodejs_profile() {
msg "Creating profile..."
{
echo "### Generated file, DO NOT MODIFY"
echo "_NODE_JS_INSTALL_PREFIX='$PREFIX'"
cat <<-'DATA'
if ! echo ":$PATH:" | grep -q "$_NODE_JS_INSTALL_PREFIX/bin" ; then
export PATH="$PATH:./node_modules/.bin:./common/temp/bin:$_NODE_JS_INSTALL_PREFIX/bin"
fi
unset _NODE_JS_INSTALL_PREFIX
DATA
echo
} >/etc/profile.d/nodejs.sh
msg "Loading profile..."
source /etc/profile.d/nodejs.sh
}
function timing_registry() {
local REG="$1"
local http_proxy='' https_proxy='' all_proxy='' HTTP_PROXY='' HTTPS_PROXY='' ALL_PROXY=''
local ts tt
nslookup "$REG" &>/dev/null
ts=$(date +%s%N)
curl "https://$REG/" &>/dev/null || true
curl "https://$REG/debug/package.json" &>/dev/null || true
tt=$(($(date +%s%N) - ts))
echo "Timing: [$tt] $REG" >&2
echo "$tt"
}
function update_config() {
mkdir -p "$PREFIX/etc" || true
[[ -e "$PREFIX/etc/yarnrc" ]] || touch "$PREFIX/etc/yarnrc" || true
[[ -e "$PREFIX/etc/npmrc" ]] || touch "$PREFIX/etc/npmrc" || true
replace_line "$PREFIX/etc/yarnrc" 'global-folder' 'global-folder "/usr/nodejs/lib"'
replace_line "$PREFIX/etc/npmrc" 'prefix' "prefix=$PREFIX"
replace_line "$PREFIX/etc/npmrc" 'global-dir' "global-dir=$PREFIX/lib/pnpm-global"
replace_line "$PREFIX/etc/npmrc" 'global-bin-dir' "global-bin-dir=$PREFIX/bin"
replace_line "$PREFIX/etc/npmrc" 'access' "access=public"
replace_line "$PREFIX/etc/npmrc" 'always-auth' 'always-auth=false'
replace_line "$PREFIX/etc/npmrc" 'fetch-retries' 'fetch-retries=1000'
replace_line "$PREFIX/etc/npmrc" 'network-concurrency' 'network-concurrency=3'
replace_line "$PREFIX/etc/npmrc" 'prefer-offline' 'prefer-offline=true'
set_registy
set_cache_path
}
function set_registy() {
export npm_config_registry='https://registry.npmjs.org'
if ! grep -qE '\bregistry\s*=' "$PREFIX/etc/npmrc"; then
CHINA=$(timing_registry registry.npmmirror.com)
ORIGINAL=$(timing_registry registry.npmjs.org)
if [[ $CHINA -le $ORIGINAL ]]; then
echo "Using TaoBao npm mirror: npmmirror.com"
replace_line "$PREFIX/etc/npmrc" 'registry' "registry=https://registry.npmmirror.com"
export npm_config_registry='https://registry.npmmirror.com'
else
replace_line "$PREFIX/etc/npmrc" 'registry' "registry=https://registry.npmjs.org"
fi
replace_line "$PREFIX/etc/npmrc" 'noproxy' "noproxy=registry.npmmirror.com,cdn.npmmirror.com,npmmirror.com"
else
reg=$(grep -E '^registry\s*=' "$PREFIX/etc/npmrc" | tr '=' ' ' | awk '{print $2}')
if [[ $reg == http* ]]; then
echo "Using Config npm registry: $reg"
export npm_config_registry="${reg%/}"
fi
fi
}
function set_cache_path() {
if [[ ${SYSTEM_COMMON_CACHE+found} == found ]]; then
echo "Reset cache folder(s) to $SYSTEM_COMMON_CACHE"
replace_line "$PREFIX/etc/npmrc" 'cache' "cache=$SYSTEM_COMMON_CACHE/npm"
replace_line "$PREFIX/etc/yarnrc" 'cache-folder' "cache-folder \"$SYSTEM_COMMON_CACHE/yarn\""
echo "export JSPM_GLOBAL_PATH='$SYSTEM_COMMON_CACHE/jspm'" >>/etc/profile.d/nodejs.sh
fi
}
### curl https://get.pnpm.io/install.sh
_fetch() {
curl -fsSL "$1"
}
download_using_pnpm() {
local http_proxy='' https_proxy='' all_proxy='' HTTP_PROXY='' HTTPS_PROXY='' ALL_PROXY=''
msg " metadata: $npm_config_registry/pnpm"
version_json="$(_fetch "$npm_config_registry/pnpm")" || die "Download Error!"
version="$(printf '%s' "${version_json}" | jq -r '."dist-tags".latest')"
msg " version: $version"
archive_url="$(printf '%s' "${version_json}" | jq --arg version "$version" -r '.versions[$version].dist.tarball')"
msg " tarball: $archive_url"
curl -fL "$archive_url" >"pnpm.tgz.download"
mv "pnpm.tgz.download" "pnpm.tgz"
tar xf "pnpm.tgz"
"$BIN" ./package/bin/pnpm.cjs -g add pnpm@latest npm@latest yarn@latest || die "failed execute temp file"
}
function install_package_managers() {
TMPD=$(mktemp -d)
pushd "$TMPD" >/dev/null || die "temp dir not found!"
msg "Installing package managers..."
rm -rf "$PREFIX/bin/npm" "$PREFIX/bin/npx" "$PREFIX/lib/node_modules"
download_using_pnpm
echo -n " - pnpm: "
"$PNPM" --version
echo -n " - npm: "
"$NPM" --version
echo -n " - yarn: "
"$YARN" --version
popd >/dev/null || die "???"
rm -rf "$TMPD"
}
function install_other_packages() {
msg "Installing other package managers..."
"$PNPM" -g add unipm @microsoft/rush
}
if command_exists id && [[ "$(id -u)" -ne 0 ]]; then
msg "not privileged user."
if command_exists sudo; then
msg "re-invoke with sudo... (will fail if password is required from commandline)"
exec sudo bash
fi
fi
mkdir -p "$PREFIX" || die "Can not create directory at '$PREFIX'"
if [[ ${SYSTEM_COMMON_CACHE+found} == found ]]; then
declare -r DOWNLOAD="${SYSTEM_COMMON_CACHE}/Download"
msg "download dir is $DOWNLOAD"
else
declare -r DOWNLOAD="${TMPDIR-"/tmp"}"
msg "temp download dir is $DOWNLOAD"
fi
(
mkdir -p "$DOWNLOAD"
cd "$DOWNLOAD"
touch .test && rm .test
) || die "System temp direcotry '$DOWNLOAD' is not writable."
do_system_check
install_nodejs
create_nodejs_profile
update_config
install_package_managers
install_other_packages