-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwnv
executable file
·170 lines (141 loc) · 4.11 KB
/
wnv
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
#!/bin/bash
########### what-node-version #################
#
# Script for automatically switch node versions based on the configured engine in the package.json
# Author: Luis Beu
#
####################################################
# print usage page
usage() {
cat <<EOF
Usage: wnv [NPPM OPTIONS...]
-h --help Show this usage summary and exit
NO_CACHE=1 wnv [NPPM OPTIONS...]
pulls the node version cache again
EOF
exit 0
}
# TODO: verbose and silence nvm
# if no args print usage
if [ $# -eq 0 ]; then
usage
fi
# loop over flags
while test $# -gt 0; do
case "$1" in
-h | --help)
usage
;;
*)
break
;;
esac
done
if [ ! -f /usr/local/opt/nvm/nvm.sh ]; then
echo "nvm not found please install it. brew install nvm"
exit 1
fi
# source nvm
source /usr/local/opt/nvm/nvm.sh
padding=10000
script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
version_dir="$script_dir/versions.txt"
if [[ ! -e 'package.json' ]]; then
npm "$@"
exit 0
fi
engines=$(jq '.engines.node' -r package.json)
# no configuration found dont switch version
if [[ -z $engines ]] || [[ $engines == "null" ]] ; then
npm "$@"
exit 0
fi
if [[ ! -e $version_dir ]]; then
NO_CACHE=1
fi
# get engine config from package.json
node_version=($(grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' <<<"$engines"))
# read versions new if file is older then 24h or no_cache is set
if [[ -n $NO_CACHE ]] || test "$(find "$version_dir" -mmin +1440)"; then
echo "Fetch node versions. This takes a little..."
# curl version list from nodejs dist index
version_list=($(curl --silent https://nodejs.org/dist/ | grep -o 'href=".*">' | sed 's/href="//;s/v//;s/\///;s/">//'))
for index in "${!version_list[@]}"; do
# test if it is version
if [[ "${version_list[index]}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
# split version into subversions
read -ra split <<<$(
IFS="."
echo ${version_list[index]}
)
# add padding for right sorting
for i in "${!split[@]}"; do
split[$i]=$((${split[$i]} + $padding))
done
# put array back together
version_list[$index]=$(printf "%s." "${split[@]}" | cut -d "." -f 1-${#split[@]})
else
# unset all that is not considered a version
unset -v 'version_list[$index]'
fi
done
# sort array
version_list=("${version_list[@]}")
IFS=$'\n' version_list=($(sort <<<"${version_list[*]}"))
unset IFS
for index in "${!version_list[@]}"; do
# split version into subversions
read -ra split <<<$(
IFS="."
echo ${version_list[index]}
)
# remove padding
for i in "${!split[@]}"; do
split[$i]=$((${split[$i]} - $padding))
done
# put array back together
version_list[$index]=$(printf "%s." "${split[@]}" | cut -d "." -f 1-${#split[@]})
done
# write to file to be a little faster
printf "%s\n" "${version_list[@]}" >"$version_dir"
else
# usign cached version
version_list=($(cat "$version_dir" | tr '\n' ' '))
fi
# check if specified version is a legit version
if [[ ! " ${version_list[*]} " =~ ${node_version[0]} ]]; then
echo "Unkonw node version specefied in package.json"
exit 1
fi
# check what to do and which version to use/install
case $engines in
*">"*)
node_versio_majorn=($(grep -Eo '^\d+' <<<"${node_version[0]}"))
nvm install "${node_versio_majorn[0]}"
npm "$@"
;;
*"<"*)
if [[ $engines == *"="* ]]; then
nvm install "${node_version[0]}"
npm "$@"
exit 0
fi
# get index from version
for index in "${!version_list[@]}"; do
[[ "${version_list[$index]}" = "${node_version[0]}" ]] && break
done
# use the version previous to specified
nvm install "${version_list[$index - 1]}"
npm "$@"
;;
*"="*)
nvm install "${node_version[0]}"
npm "$@"
;;
*)
nvm install "${node_version[0]}"
npm "$@"
;;
esac
# exit script successful
exit 0