forked from nanjingboy/nvmw
-
Notifications
You must be signed in to change notification settings - Fork 16
/
install.sh
197 lines (154 loc) · 3.83 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
NVM_VERSION="1.6.4"
NVM_VERSION_V="v${NVM_VERSION}"
TAR_NEEDS_WILDCARDS_OPT="$(tar --help | grep "\--wildcards")"
if [ -n "$TAR_NEEDS_WILDCARDS_OPT" ]; then
TAR_WILDCARDS_OPT="--wildcards"
else
TAR_WILDCARDS_OPT=""
fi
if [ -n "$NVM_TEST" ]; then
NVM_TGZ_URL="https://github.com/jchip/nvm/archive/${NVM_VERSION_V}.tar.gz"
else
NVM_TGZ_URL="https://registry.npmjs.org/@jchip/nvm/-/nvm-${NVM_VERSION}.tgz"
fi
if [ -z "${NVM_HOME}" ]; then
export NVM_HOME="$HOME/nvm"
fi
function fetch() {
curl=$(which curl)
if [ "$?" == "0" ]; then
curl --fail -L $1 -o $2
return $?
fi
wget=$(which wget)
if [ "$?" == "0" ]; then
wget "$1" --output-document="$2"
return $?
fi
echo "No curl or wget found"
exit 1
}
function tmpdir() {
if [ -n "$TMPDIR" ]; then
echo "$TMPDIR"
else
echo "/tmp"
fi
}
VERSIONS_TAB_FILE_URL="https://nodejs.org/dist/index.tab"
function getLtsVersionByTabFile() {
TAB_FILE="$(tmpdir)/nodejs.versions.tab"
fetch $VERSIONS_TAB_FILE_URL $TAB_FILE
local fv
fv=$(cut -f1,10 "$TAB_FILE" | tail -n +2 | egrep -v $'\t-$' | head -1 | cut -f1 | egrep -o 'v[0-9]+\.[0-9]+\.[0-9]+$')
if [ -n "$fv" ]; then
echo "$fv"
else
echo "v20.12.0"
fi
}
echo "Checking for latest node.js LTS from ${VERSIONS_TAB_FILE_URL}"
DEFAULT_NODE_VERSION=$(getLtsVersionByTabFile)
echo "Determined node.js LTS version to be $DEFAULT_NODE_VERSION"
function getOs() {
uname -s | tr "[:upper:]" "[:lower:]"
}
function getArch() {
case $(uname -m) in
x86_64)
echo "x64"
;;
i686 | i386)
echo "x86"
;;
*)
uname -m | tr "[:upper:]" "[:lower:]"
;;
esac
}
function fetchNodeJS() {
local version
version="$1"
local nodejsMirror
if [ -n "${NVM_NODEJS_ORG_MIRROR}" ]; then
nodejsMirror="${NVM_NODEJS_ORG_MIRROR}"
else
nodejsMirror="https://nodejs.org/dist"
fi
local tgzFile
tgzFile="node-${version}-$(getOs)-$(getArch).tar.gz"
nodejsBinUrl="${nodejsMirror}/${version}/${tgzFile}"
local cacheDir
cacheDir="${NVM_CACHE}/${version}"
mkdir -p "${cacheDir}"
local destTgzFile
destTgzFile="${cacheDir}/node.tgz"
if [ ! -f "${destTgzFile}" ]; then
echo "Fetching ${nodejsBinUrl}"
if ! fetch "${nodejsBinUrl}" "${destTgzFile}"; then
rm -rf "${cacheDir}"
fi
fi
if [ -f "${destTgzFile}" ]; then
tar ${TAR_WILDCARDS_OPT} -xzf "${destTgzFile}" --strip=2 --directory "${NVM_HOME}" "*/bin/node"
else
echo "Unable to fetch ${nodejsBinUrl}"
exit 1
fi
}
NVM_CACHE="${NVM_HOME}/cache"
NVM_NODE="${NVM_HOME}/nodejs"
NVM_NODE_BIN="${NVM_HOME}/node"
function installNvm() {
if [ ! -d "${NVM_CACHE}" ]; then
mkdir -p "${NVM_CACHE}"
fi
local nvmDestTgzFile
nvmDestTgzFile="${NVM_CACHE}/nvm-${NVM_VERSION_V}.tgz"
echo "Fetching ${NVM_TGZ_URL}"
fetch "${NVM_TGZ_URL}" "${nvmDestTgzFile}"
local nvm_files
nvm_files="$(tmpdir)/nvm_files.txt"
cat >${nvm_files}<<EOF
*/bin
*/dist
*/package.json
EOF
tar ${TAR_WILDCARDS_OPT} -xzf "${nvmDestTgzFile}" --directory="${NVM_HOME}" --strip=1 --files-from="$nvm_files"
}
fetchNodeJS "${DEFAULT_NODE_VERSION}"
if [ -f "test.sh" ]; then
source test.sh
else
installNvm
fi
source "${NVM_HOME}/bin/nvm.sh"
function setBashRc() {
BASH_RC="${HOME}/.bashrc"
BASH_PROFILE="${HOME}/.bash_profile"
local rcfile
if [ -f "${BASH_RC}" ]; then
rcfile="${BASH_RC}"
else
rcfile="${BASH_PROFILE}"
fi
if [ ! -f "${rcfile}" ]; then
touch "${rcfile}"
fi
${NVM_NODE_BIN} ${NVM_HOME}/bin/install_bashrc.js "${rcfile}"
}
# http://zsh.sourceforge.net/Intro/intro_3.html
function setZshRc() {
ZD=${HOME}
if [ -n "${ZDOTDIR}" ]; then
ZD=${ZDOTDIR}
fi
ZSH_RC="${ZD}/.zshrc"
if [ ! -f "${ZSH_RC}" ]; then
touch "${ZSH_RC}"
fi
${NVM_NODE_BIN} ${NVM_HOME}/bin/install_bashrc.js "${ZSH_RC}" zsh
}
setBashRc
setZshRc
nvm install $DEFAULT_NODE_VERSION