This repository has been archived by the owner on Mar 24, 2020. It is now read-only.
forked from jasongin/nvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvs.sh
183 lines (159 loc) · 6.04 KB
/
nvs.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
# NVS (Node Version Switcher)
# Implemented as a POSIX-compliant function.
# To use, source this file from your profile.
# Inspired by NVM (https://github.com/creationix/nvm)
# and other node version switching tools.
# This shell script merely bootstraps node.exe if necessary, then forwards
# arguments to the main nvs.js script.
# Try to locate the NVS_ROOT path, where the nvs scripts are installed.
if [ -n "${BASH_SOURCE}" ]; then
export NVS_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null && \pwd)"
else
if [ -n "${(%):-%x}" ]; then # zsh script source
export NVS_ROOT="$(cd "$(dirname "${(%):-%x}")" > /dev/null && \pwd)"
fi 2>/dev/null
if [ -n "${NVS_HOME}" -a -z ${NVS_ROOT} ]; then
export NVS_ROOT="${NVS_HOME}"
fi
fi
# Parse the OS name and architecture from `uname`.
export NVS_OS="$(uname | sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/')"
# When running inside Git bash on Windows, `uname` reports "MINGW64_NT".
case $NVS_OS in mingw64_nt*)
export NVS_OS="win"
esac
nvs() {
# The NVS_HOME path may be overridden in the environment.
if [ -z "${NVS_HOME}" ]; then
export NVS_HOME="${NVS_ROOT}"
fi
# Generate 32 bits of randomness, to avoid clashing with concurrent executions.
export NVS_POSTSCRIPT="${NVS_HOME}/nvs_tmp_$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -f1 -d" ").sh"
local NODE_EXE="node"
if [ "${NVS_OS}" = "win" ]; then
NODE_EXE="node.exe"
fi
local NODE_PATH="${NVS_HOME}/cache/${NODE_EXE}"
if [ ! -f "${NODE_PATH}" ]; then
# Parse the bootstrap parameters from defaults.json. This isn't real JSON parsing so
# its extremely limited, but defaults.json should not be edited by the user anyway.
local NODE_VERSION="$(grep '"bootstrap" *:' "${NVS_ROOT}/defaults.json" | sed -e 's/.*: *"//' -e 's/"[^\n]*//' -e 's/.*\///')"
local NODE_REMOTE="$(grep '"bootstrap" *:' "${NVS_ROOT}/defaults.json" | sed -e 's/.*: *"//' -e 's/"[^\n]*//' -e 's/\/.*//')"
local NODE_BASE_URI="$(grep "\"${NODE_REMOTE}\" *:" "${NVS_ROOT}/defaults.json" | sed -e 's/.*: *"//' -e 's/"[^\n]*//')"
local NODE_ARCHIVE_EXT=".tar.gz"
local TAR_FLAGS="-zxvf"
if [ "${NVS_OS}" = "win" ]; then
NODE_ARCHIVE_EXT=".7z"
elif [ "${NVS_USE_XZ}" = "1" ]; then
NODE_ARCHIVE_EXT=".tar.xz"
TAR_FLAGS="-Jxvf"
fi
# Download a node binary to use to bootstrap the NVS script.
local NODE_ARCH="$(uname -m | sed 's/x86_64/x64/' | sed 's/i686/x86/')"
local NODE_FULLNAME="node-v${NODE_VERSION}-${NVS_OS}-${NODE_ARCH}"
local NODE_URI="${NODE_BASE_URI}v${NODE_VERSION}/${NODE_FULLNAME}${NODE_ARCHIVE_EXT}"
local NODE_ARCHIVE="${NVS_HOME}/cache/${NODE_FULLNAME}${NODE_ARCHIVE_EXT}"
if [ ! -d "${NVS_HOME}/cache" ]; then
command mkdir -p "${NVS_HOME}/cache"
fi
echo "Downloading bootstrap node from ${NODE_URI}"
if type noglob > /dev/null 2>&1; then
noglob curl -L -# "${NODE_URI}" -o "${NODE_ARCHIVE}"
else
curl -L -# "${NODE_URI}" -o "${NODE_ARCHIVE}"
fi
if [ "${NVS_OS}" = "win" ]; then
"${NVS_ROOT}/tools/7-Zip/7zr.exe" e "-o${NVS_HOME}/cache" -y "${NODE_ARCHIVE}" "${NODE_FULLNAME}/${NODE_EXE}" > /dev/null 2>&1
else
tar $TAR_FLAGS "${NODE_ARCHIVE}" -C "${NVS_HOME}/cache" "${NODE_FULLNAME}/bin/${NODE_EXE}" > /dev/null 2>&1
mv "${NVS_HOME}/cache/${NODE_FULLNAME}/bin/${NODE_EXE}" "${NVS_HOME}/cache/${NODE_EXE}" > /dev/null 2>& 1
rm -r "${NVS_HOME}/cache/${NODE_FULLNAME}" > /dev/null 2>& 1
fi
if [ ! -f "${NODE_PATH}" ]; then
echo "Failed to download bootstrap node binary."
return 1
fi
echo ""
fi
local EXIT_CODE
# Check if invoked as a CD function that enables auto-switching.
case "$@" in
"cd")
# Find the nearest .node-version file in current or parent directories
local DIR=$PWD
while [ "$DIR" != "" -a ! -e "$DIR/.node-version" ]; do
if [ "$DIR" = "/" ]; then
DIR=
else
DIR=$(dirname "$DIR")
fi
done
# If it's different from the last auto-switched directory, then switch.
if [ "$DIR" != "$NVS_AUTO_DIRECTORY" ]; then
command "${NODE_PATH}" "${NVS_ROOT}/lib/main.js" auto
EXIT_CODE=$?
fi
export NVS_AUTO_DIRECTORY=$DIR
;;
*)
# Forward args to the main JavaScript file.
command "${NODE_PATH}" "${NVS_ROOT}/lib/main.js" "$@"
EXIT_CODE=$?
;;
esac
# Call the post-invocation script if it is present, then delete it.
# This allows the invocation to potentially modify the caller's environment (e.g. PATH)
if [ -f "${NVS_POSTSCRIPT}" ]; then
. "${NVS_POSTSCRIPT}"
rm "${NVS_POSTSCRIPT}"
unset NVS_POSTSCRIPT
fi
return $EXIT_CODE
}
nvsudo() {
# Forward the current version path to the sudo environment.
local NVS_CURRENT=`nvs which`
if [ -n "${NVS_CURRENT}" ]; then
NVS_CURRENT=`dirname "${NVS_CURRENT}"`
fi
sudo "NVS_CURRENT=${NVS_CURRENT}" "${NVS_ROOT}/nvs" $*
}
if [ ! "${NVS_OS}" = "win" ]; then
# Check if `tar` has xz support. Look for a minimum libarchive or gnutar version.
if [ -z "${NVS_USE_XZ}" ]; then
export LIBARCHIVE_VER="$(tar --version | sed -n "s/.*libarchive \([0-9][0-9]*\(\.[0-9][0-9]*\)*\).*/\1/p")"
if [ -n "${LIBARCHIVE_VER}" ]; then
LIBARCHIVE_VER="$(printf "%.3d%.3d%.3d" $(echo "${LIBARCHIVE_VER}" | sed "s/\\./ /g"))"
if [ $LIBARCHIVE_VER -ge 002008000 ]; then
export NVS_USE_XZ=1
else
export NVS_USE_XZ=0
fi
else
LIBARCHIVE_VER="$(tar --version | sed -n "s/.*(GNU tar) \([0-9][0-9]*\(\.[0-9][0-9]*\)*\).*/\1/p")"
if [ -n "${LIBARCHIVE_VER}" ]; then
LIBARCHIVE_VER="$(printf "%.3d%.3d%.3d" $(echo "${LIBARCHIVE_VER}" | sed "s/\\./ /g"))"
if [ $LIBARCHIVE_VER -ge 001022000 ]; then
export NVS_USE_XZ=1
else
export NVS_USE_XZ=0
fi
fi
fi
unset LIBARCHIVE_VER
fi
fi
# If some version is linked as the default, begin by using that version.
if [ -d "${NVS_HOME}/default" ]; then
if [ -f "${NVS_HOME}/default/bin/node" ]; then
export PATH="${NVS_HOME}/default/bin:${PATH}"
unset NPM_CONFIG_PREFIX
elif [ -f "${NVS_HOME}/default/node" ]; then
export PATH="${NVS_HOME}/default:${PATH}"
unset NPM_CONFIG_PREFIX
fi
fi
# If sourced with parameters, invoke the function now with those parameters.
if [ -n "$*" -a -z "${NVS_EXECUTE}" ]; then
nvs $*
fi