-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-nodejs.sh
123 lines (115 loc) · 2.89 KB
/
setup-nodejs.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
#!/bin/sh
__sp_init_global_vars() {
__sp_version=$(__sp_default_version)
__sp_installation_base_dir=$HOME/opt
# Reset OPTIND, if getopts was used before
OPTIND=1
}
__sp_reset_custom_vars_and_funcs() {
unset $(declare | grep '^__sp_' | tr '=' ' ' | cut -f1 -d ' ')
# Reset OPTIND for future use of getopts
OPTIND=1
}
__sp_set_vars_from_opts() {
while getopts v: opt; do
case $opt in
v) __sp_version=$OPTARG
;;
esac
done
}
__sp_abort() {
__sp_restore_exported_vars
return 0
}
__sp_local_installation_file_path() {
echo /tmp/$(__sp_installation_file)
}
__sp_remote_installation_file_exists() {
curl -sIf $(__sp_download_url) >/dev/null
}
__sp_download_installation_file() {
echo "Download installation file"
curl $(__sp_download_url) -o $(__sp_local_installation_file_path)
}
__sp_install() {
echo "Install version: $__sp_version"
if [ ! -f $(__sp_local_installation_file_path) ]; then
echo "Local installation file not found: $(__sp_local_installation_file_path). Try, download new one"
if __sp_remote_installation_file_exists; then
__sp_download_installation_file
else
echo "ERROR: No remote installation file found. Abort"
__sp_abort
fi
fi
__sp_install_installation_file
}
__sp_main() {
__sp_init_global_vars
__sp_set_vars_from_opts ${@}
if ! __sp_is_installed; then
echo "Start installation"
__sp_restore_exported_vars
__sp_export_vars
__sp_install || __sp_abort
fi
__sp_print_success_message
__sp_reset_custom_vars_and_funcs
}
__sp_default_version() {
echo v20.14.0
}
__sp_export_vars() {
echo "Add $(__sp_installation_path) to PATH"
__SP_NODEJS_ORIGINAL_PATH="${PATH}"
export PATH="$(__sp_installation_path):${PATH}"
}
__sp_restore_exported_vars() {
if [ -v __SP_NODEJS_ORIGINAL_PATH ]; then
export PATH="${__SP_NODEJS_ORIGINAL_PATH}"
unset __SP_NODEJS_ORIGINAL_PATH
fi
}
__sp_installation_path() {
case "$(uname -s)" in
CYGWIN*|MINGW*|MSYS*)
echo $__sp_installation_base_dir/node-$__sp_version-win-x64
;;
*)
echo $__sp_installation_base_dir/node-$__sp_version-linux-x64/bin
;;
esac
}
__sp_is_installed() {
node --version 2>/dev/null &&
(node --version 2>&1 | grep $__sp_version)
}
__sp_installation_file() {
case "$(uname -s)" in
CYGWIN*|MINGW*|MSYS*)
echo node-$__sp_version-win-x64.zip
;;
*)
echo node-$__sp_version-linux-x64.tar.xz
;;
esac
}
__sp_install_installation_file() {
local trgt_dir=$(dirname $(__sp_installation_path))
case "$(uname -s)" in
CYGWIN*|MINGW*|MSYS*)
unzip -oq $(__sp_local_installation_file_path) -d $trgt_dir
;;
*)
tar Jxf $(__sp_local_installation_file_path) -C $__sp_installation_base_dir
;;
esac
}
__sp_download_url() {
echo https://nodejs.org/dist/$__sp_version/$(__sp_installation_file)
}
__sp_print_success_message() {
node -v
}
__sp_main ${@}