-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
228 lines (197 loc) · 6.25 KB
/
setup.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
#!/bin/bash
SETUP_VERSION="0.0.1"
PACKAGE_FILE="package.json"
INSTALL_LOG="setup.log"
OS=$(source /etc/os-release && echo "$NAME" | tr '[:upper:]' '[:lower:]')
NODE_VERSION="14.16.0"
### Terminal colour
RED=$(tput setaf 1)
GREEN=$(tput setaf 118)
YELLOW=$(tput setaf 11)
CYAN=$(tput setaf 14)
CLEAR=$(tput sgr0)
### Terminal colour
run_setup() {
case $1 in
"-i"|"--install")
if is_compatible; then
if [[ $OS == *"arch"* ]]; then
echo_info "Installing nvm, nodejs & npm for arch"
if install_node_arch ; then
echo_success "Installed."
else
echo_error "Couldn't install! Exiting." 1>&2
exit 1
fi
elif [[ $OS == *"debian"* ]] || [[ $OS == *"ubuntu"* ]] ; then
if install_ubuntu_debian ; then
echo_success "Installed."
else
echo_error "Couldn't install! Exiting." 1>&2
exit 1
fi
else
echo_error "Something went wrong during check"
fi
elif $? -eq 200 ; then
echo_warn "nvm installed but couldn't continue due to unidentified shell, please manually proceed. Exiting."
exit 1
else
echo_error "Something went wrong. Is your system compatible? Check $INSTALL_LOG"
exit 1
fi
;;
"-h"|"--help")
print_help
exit 0
;;
*)
echo_warn "No arguments supplied, proceeding with default setup"
install_npm_packages
exit 0
;;
esac
}
print_help() {
cat << EOF
${GREEN}USAGE: ./setup.sh <args> <options>${CLEAR}
${YELLOW}==== Args ====${CLEAR}
${YELLOW}-h${CLEAR} | ${YELLOW}--help${CLEAR} -> The current help menu
${YELLOW}-i${CLEAR} | ${YELLOW}--install${CLEAR} -> complete install of nvm, npm, nodejs
EOF
}
install_npm_packages() {
echo_info "Proceeding with npm package install"
echo_info "Checking if $PACKAGE_FILE exists"
if [ ! -f ./$PACKAGE_FILE ] ; then
echo_error "$PACKAGE_FILE does not exist! Cannot proceed with npm package requirement install"
exit 1
fi
echo_info "$PACKAGE_FILE exists, proceeding with npm install"
if npm install >> $INSTALL_LOG ; then
echo_success "npm packages installed."
else
echo_error "Couldn't run npm install. Exiting." 1>&2
exit 1
fi
}
install_ubuntu_debian() {
echo_info "Installing nvm"
if wget -q https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh && ./install.sh >> $INSTALL_LOG; then
echo_success "nvm install successful, proceeding with nodejs installation."
rm ./install.sh
if grep -q "bash" $SHELL ; then
echo_info "Sourcing .bashrc nvm.sh"
. ~/.bashrc
. ~/.nvm/nvm.sh
elif grep -q "zsh" $SHELL ; then
echo_info "Sourcing .zshrc and nvm.sh"
. ~/.zshrc
. ~/.nvm/nvm.sh
elif grep -q "ksh" $SHELL ; then
echo "Sourcing .profile nvm.sh"
. ~/.profile
. ~/.nvm/nvm.sh
else
echo_warn "Cannot identify current shell. Please source or spawn a new shell instance for nvm commands to take effect."
exit 1
fi
if nvm install $NODE_VERSION >>$INSTALL_LOG ; then
echo_success "Nodejs $NODE_VERSION has been installed via nvm"
echo_info "Checking for npm install"
if nvm install-latest-npm >>$INSTALL_LOG ; then
echo_success "nvm successfully installed npm from current nodejs version"
exit 0
else
echo_error "Unable to install npm through nvm" 1>&2
exit 1
fi
else
echo_error "Unable to run nvm install $NODE_VERSION" 1>&2
exit 1
fi
else
echo_error "Unable to wget nvm install.sh" 1>&2
exit 1
fi
}
install_node_arch() {
TEMP_DIR="/tmp/nvm-arch"
NVM_URL="https://github.com/nvm-sh/nvm"
echo_info "Creating working directory for nvm upstream $NVM_URL"
if mkdir "$TEMP_DIR" ; then
echo_success "Working directory created"
echo_info "Cloning nvm upstream url"
if git clone $NVM_URL "$TEMP_DIR" ; then
echo_success "nvm cloned into $TEMP_DIR, changing dir and making package"
if cd /tmp/nvm-arch && makepkg -si ; then
echo_success "NVM has been installed through the AUR."
else
echo_error "Unable to makepkg -si in /opt/nvm-arch" 1>&2
exit 1
fi
else
echo_error "Unable to pull nvm aur upstream ${CYAN}$NVM_URL${CLEAR}" 1>&2
exit 1
fi
else
echo_error "Unable to make temporary directory $TEMP_DIR" 1>&2
exit 1
fi
echo_info "Installing nodejs and npm"
if pacman -S nodejs npm ; then
echo_success "Nodejs and npm installed through pacman"
exit 0
else
echo_error "Unable to install nodejs and npm through pacman" 1>&2
exit 1
fi
}
is_compatible() {
if [[ $OS == *"ubuntu"* ]]; then
return 0
elif [[ $OS == *"debian"* ]]; then
return 0
elif [[ $OS == *"arch"* ]]; then
return 0
else
echo_error "$OS is not compatible. Exiting" >> $INSTALL_LOG
return 1
fi
}
echo_error() {
echo "[${RED}ERROR${CLEAR}] -> $*"
}
echo_warn() {
echo "[${YELLOW}WARN${CLEAR}] -> $*"
}
echo_info() {
echo "[${CYAN}INFO${CLEAR}] -> $*"
}
echo_success() {
echo "[${GREEN}SUCCESS${CLEAR}] -> $*"
}
prompt_yn() {
case "$1" in
[yY])
return 0
;;
[nN])
return 1
;;
*)
echo_error "[ERROR] -> Unrecognised input."
return 1
;;
esac
}
if ! [ $(id -u) = 0 ]; then
if [[ -x $0 ]]; then
run_setup $1
else
echo_error "$0 is not executable, please run chmod +x $0"
fi
else
echo_error "No need to run as root."
exit 1
fi