-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodevirtualenv
More file actions
executable file
·217 lines (170 loc) · 5.15 KB
/
nodevirtualenv
File metadata and controls
executable file
·217 lines (170 loc) · 5.15 KB
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
#! /bin/bash
# General system helpers being
PACKAGE_VERSION=1.0.0
printError() {
echo -e "\e[1;7;31m ERROR \e[0m\e[31m :: ${1}\e[0m"
}
printInfo() {
echo -e "\e[1;7;34m INFO \e[0m\e[34m ::\e[0m ${1}"
}
printSuccess() {
echo -e "\e[1;7;32m SUCCESS \e[0m\e[32m ::\e[0m ${1}"
}
printWarning() {
echo -e "\e[1;7;93m WARNING \e[0m ::\e[0m ${1}"
}
printErrorAndExit() {
if [[ -z $1 ]]; then
msg="No arguments provided. Invalid call"
else
msg="$1"
fi
printError "$msg"
exit 1
}
isFunctionFound() {
declare -f "$1" > /dev/null;
echo $?
}
# System helper functions end here
getArgs() {
i=0
arr=("$@")
for arg in "${arr[@]}"; do
if [[ $arg == "--"* ]]; then
args["$arg"]=1
elif [[ $arg == "-"* ]]; then
args["$arg"]=${arr[$((i+1))]}
fi
i=$((i+1))
done
}
printHelpAndExit() {
echo "Usage: nodevirtualenv [options]
* Installs a virtualenvironment at ${HOME}/.node-envs/<version>
* To run a command with the -c option, either a version(-v) or binary path (-p) option is required.
Options:
-v NodeJS version to install/use to run.
-p NodeJS installation path to use.
-c Script path to run with the requested version.
--bin Get the path for the NodeJS binary for the version specified.
--list List all locally installed NodeJS versions.
"
exit 0
}
listInstalledVersionsAndExit() {
rootPath="$HOME/.node-envs"
if [[ ! -d "$rootPath" ]]; then
printWarning "No NodeJS versions installed locally"
exit 0
else
count=$(ls $rootPath/*/bin/node | wc -l)
printSuccess "Found ${count} locally installed NodeJS installations"
versions=()
for version in $(ls "$rootPath"); do
bin="${rootPath}/${version}/bin/node"
if [[ -f $bin ]]; then
version=$($bin -v)
echo "* $version [$bin]"
fi
done
exit 0
fi
exit 0
}
printPackageVersionAndExit() {
echo "nodevirtualenv v${PACKAGE_VERSION}"
exit 0
}
declare -A args
houseKeep() {
if [[ ${#@} == 0 ]] || [[ $1 == '-h' ]] || [[ $1 == '--help' ]]; then
printHelpAndExit
fi
getArgs $@
if [[ ${args['--version']} ]]; then
printPackageVersionAndExit
fi
nodeBaseURL="https://nodejs.org/dist/"
version=${args['-v']}
envPath=${args['-p']}
cmd=${args['-c']}
binary=${args['--bin']}
ls=${args['--list']}
if [[ $ls ]]; then
listInstalledVersionsAndExit
fi
# Check if a version is provided and exit if not.
# Alternatively, if a path is provided, deduce the version downstream.
if [[ -z ${version} ]]; then
if [[ -z $envPath ]] && [[ -z $cmd ]]; then
printErrorAndExit "No version specified. Cannot proceed"
fi
fi
# If no environment path is provided, deduce a path from root env
# path and the version provided.
if [[ -z ${envPath} ]]; then
envPath="$HOME/.node-envs/${version}"
fi
if [[ $envPath != "$HOME/.node-envs"* ]]; then
envPath="$HOME/.node-envs/$envPath"
fi
binaryPath="$envPath/bin/node"
if [[ ! -f $binaryPath ]] && [[ ! -z ${binary} ]]; then
printErrorAndExit "Requested NodeJS version binary not found at ${binaryPath}"
fi
# If no version was provided by the commandline options, deduce
# one from the path's binary.
if [[ -z ${version} ]]; then
version=$($binaryPath -v)
version=${version:1:${#version}}
fi
# If a command is provided via the -c option, attempt to find the
# requested version's binary and run it via the same.
if [[ ! -z $cmd ]]; then
# If requested environment is not found, exit immediately
if [[ ! -d $envPath ]]; then
printErrorAndExit "NodeJS v${version} not found. Install before proceeding"
fi
printInfo "Using NodeJS v${version} to run ${cmd}"
# Run the requested script via the found binary
"${binaryPath}" "$(realpath $cmd)"
# If a --bin commandline argument is provided, return the binary path
elif [[ $binary == 1 ]]; then
echo "$binaryPath"
# If a version number/install path is provided, attempt to download
# the requested version from NodeJS distributions and install it to
# the requested path
else
file="/tmp/node-${version}.tar.gz"
printInfo "Installing NodeJS v${version} at ${envPath}"
url="${nodeBaseURL}v${version}/node-v${version}-linux-x64.tar.gz"
res=$(curl -s -l -w "%{http_code}" "$url" -o "$file" | tail -n 1)
# If a non HTTP200 status is received, exit with an error to check
# version availability against remote base path.
if [[ $res != 200 ]]; then
printErrorAndExit "Unable to fetch v${version}. Please check against ${nodeBaseURL}"
fi
# If the install path is not found, create it. Else, clear the
# current contents for a clean install.
if [[ ! -d $envPath ]]; then
mkdir -p "$envPath"
else
rm -r "$envPath"/*
fi
# Copy over the downloaded compressed binaries and uncompress them
# at the target location
cp "$file" "${envPath}"
cd "${envPath}"
tar -xf "$file"
mv node*/* .
# Clean up the installation path and exit with a success message.
rm -rdf "$(pwd)/node-v${version}-linux-x64/"
rm "$file"
printSuccess "Installed NodeJS v${version} to ${envPath}"
fi
}
main() {
houseKeep $@
}
main $@