-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild_node_check.sh
executable file
·197 lines (162 loc) · 4.81 KB
/
build_node_check.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
#!/bin/bash
echo "Checking installation pre-requisites..."
[ -s "$HOME/.bashrc" ] && \. "$HOME/.bashrc"
nvm_default_install_dir() {
[ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm"
}
nvm_install_dir() {
if [ -n "$NVM_DIR" ]; then
printf %s "${NVM_DIR}"
else
nvm_default_install_dir
fi
}
current_node_version() {
local version=0
if [ "$(command -v node)" ]; then
version=$(node --version 2>&1)
if [[ $version =~ "No such file" ]]; then
version=-1
else
local pattern='([0-9]+)'
[[ $version =~ $pattern ]]
version="${BASH_REMATCH[1]}"
fi
fi
printf %s "$version"
}
current_npm_version() {
local version=0
if [ "$(command -v npm)" ]; then
version=$(npm --version 2>&1)
if [[ $version =~ "No such file" ]]; then
version=-1
else
local pattern='([0-9]+)'
[[ $version =~ $pattern ]]
version="${BASH_REMATCH[1]}"
fi
fi
printf %s "$version"
}
# shellcheck disable=SC2155
export NVM_DIR="$(nvm_install_dir)"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
bashrcBackup=
originalPath="$PATH"
install_nvm() {
echo "Installing nvm (Node Version Manager)"
# Be prepared to back out nvm installation in case it fails
bashrcBackup="$HOME/.bashrc".bak
export NVM_DIR=
cp "$HOME/.bashrc" "$bashrcBackup"
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.39.0/install.sh | bash
# shellcheck disable=SC2155
export NVM_DIR="$(nvm_install_dir)"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
}
# Sync version changes with build.ts
sadVersion=10
minVersion=14
maxVersion=18
absMaxVersion=20
free=$(free || echo "4194304")
pattern='([0-9]+)'
[[ $free =~ $pattern ]]
free="${BASH_REMATCH[1]}"
# If less than 2G RAM, go with Node 12 instead
if (( free < 1500000 ));then
minVersion=12
maxVersion=12
absMaxVersion=12
elif (( free > 8000000 ));then
minVersion=14
maxVersion=22
absMaxVersion=999
fi
version="$(current_node_version)"
origVersion="$version"
if (( version > absMaxVersion )) && [ ! -s "$NVM_DIR/nvm.sh" ]; then
install_nvm
fi
if [ "$version" -ne "$maxVersion" ] && [ -s "$NVM_DIR/nvm.sh" ]; then
if [ "$(nvm use "v$maxVersion")" ]; then
nvm use "v$maxVersion"
nvm alias default "$maxVersion"
else
echo "Installing Node.js v$maxVersion via nvm. This could take several minutes..."
nvm install v"$maxVersion" && nvm use v"$maxVersion" && nvm alias default "$maxVersion"
fi
version="$(current_node_version)"
fi
if (( version < minVersion )); then
echo "Installing Node.js. This could take several minutes..."
sudo apt-get update
curl -sL https://deb.nodesource.com/setup_"$minVersion".x | sudo bash -
sudo apt-get install -y nodejs
version="$(current_node_version)"
fi
# Check version again. Version 10 can annoyingly supersede version 12 with apt-get.
if (( version < minVersion )); then
if [ ! -s "$NVM_DIR/nvm.sh" ]; then
install_nvm
fi
echo "Installing Node.js v$minVersion via nvm. This could take several minutes..."
nvm install v"$minVersion" && nvm use v"$minVersion" && nvm alias default "$minVersion"
version="$(current_node_version)"
fi
# Are we good yet? Make sure nvm didn't make things worse.
if (( version < 0 )); then
echo "Using nvm failed. Removing nvm."
if [ -f "$bashrcBackup" ]; then
rm "$HOME/.bashrc"
mv "$bashrcBackup" "$HOME/.bashrc"
fi
if [ ! -d "$NVM_DIR" ]; then
rm -rf "$NVM_DIR"
fi
[ -d "$HOME/.nvm" ] rm -rf "$HOME/.nvm"
export PATH="$originalPath"
version="$(current_node_version)"
re="[^:]*nvm[^:]*:(.*)"
if (( version < 0 )) && [[ "$PATH" =~ $re ]]; then
export PATH="${BASH_REMATCH[1]}"
version="$(current_node_version)"
fi
fi
if (( version < sadVersion )); then
echo "Failed to install minimal version of Node.js"
echo "failed" > node_path.txt
exit;
fi
if [ ! "$(command -v npm)" ]; then
echo "Installing npm as a separate step"
sudo apt-get install -y npm
fi
if [ ! -f ".first-time-install" ] || [ ! -d "node_modules/@tubular/util" ] || [ "$origVersion" -ne "$version" ]; then
echo "Installing npm packages. This process can be very slow!"
# Best to wipe out all of node_modules and start from scratch.
if [ "$origVersion" -ne "$version" ]; then
# shellcheck disable=SC2164
cd server
rm package-lock.json
rm -rf node_modules
npm i
# shellcheck disable=SC2103
cd ..
rm package-lock.json
rm -rf node_modules
fi
npm i
touch .first-time-install
fi
npm_version="$(current_npm_version)"
if (( npm_version < 7 )); then
echo "Updating npm to at least version 7"
sudo npm i -g "npm@>=7"
fi
pattern='^(.*\/\.nvm\/[^:]*):'
[[ $PATH =~ $pattern ]]
echo "${BASH_REMATCH[1]}" > node_path.txt