github.com/m3db/m3@v1.5.0/scripts/install_nvm.sh (about) 1 #!/usr/bin/env bash 2 3 # SOURCE: https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh 4 5 { # this ensures the entire script is downloaded # 6 7 nvm_has() { 8 type "$1" > /dev/null 2>&1 9 } 10 11 nvm_install_dir() { 12 if [ -n "$NVM_DIR" ]; then 13 printf %s "${NVM_DIR}" 14 elif [ -n "$XDG_CONFIG_HOME" ]; then 15 printf %s "${XDG_CONFIG_HOME/nvm}" 16 else 17 printf %s "$HOME/.nvm" 18 fi 19 } 20 21 nvm_latest_version() { 22 echo "v0.34.0" 23 } 24 25 nvm_profile_is_bash_or_zsh() { 26 local TEST_PROFILE 27 TEST_PROFILE="${1-}" 28 case "${TEST_PROFILE-}" in 29 *"/.bashrc" | *"/.bash_profile" | *"/.zshrc") 30 return 31 ;; 32 *) 33 return 1 34 ;; 35 esac 36 } 37 38 # 39 # Outputs the location to NVM depending on: 40 # * The availability of $NVM_SOURCE 41 # * The method used ("script" or "git" in the script, defaults to "git") 42 # NVM_SOURCE always takes precedence unless the method is "script-nvm-exec" 43 # 44 nvm_source() { 45 local NVM_METHOD 46 NVM_METHOD="$1" 47 local NVM_SOURCE_URL 48 NVM_SOURCE_URL="$NVM_SOURCE" 49 if [ "_$NVM_METHOD" = "_script-nvm-exec" ]; then 50 NVM_SOURCE_URL="https://raw.githubusercontent.com/creationix/nvm/$(nvm_latest_version)/nvm-exec" 51 elif [ "_$NVM_METHOD" = "_script-nvm-bash-completion" ]; then 52 NVM_SOURCE_URL="https://raw.githubusercontent.com/creationix/nvm/$(nvm_latest_version)/bash_completion" 53 elif [ -z "$NVM_SOURCE_URL" ]; then 54 if [ "_$NVM_METHOD" = "_script" ]; then 55 NVM_SOURCE_URL="https://raw.githubusercontent.com/creationix/nvm/$(nvm_latest_version)/nvm.sh" 56 elif [ "_$NVM_METHOD" = "_git" ] || [ -z "$NVM_METHOD" ]; then 57 NVM_SOURCE_URL="https://github.com/creationix/nvm.git" 58 else 59 echo >&2 "Unexpected value \"$NVM_METHOD\" for \$NVM_METHOD" 60 return 1 61 fi 62 fi 63 echo "$NVM_SOURCE_URL" 64 } 65 66 # 67 # Node.js version to install 68 # 69 nvm_node_version() { 70 echo "$NODE_VERSION" 71 } 72 73 nvm_download() { 74 if nvm_has "curl"; then 75 curl --compressed -q "$@" 76 elif nvm_has "wget"; then 77 # Emulate curl with wget 78 ARGS=$(echo "$*" | command sed -e 's/--progress-bar /--progress=bar /' \ 79 -e 's/-L //' \ 80 -e 's/--compressed //' \ 81 -e 's/-I /--server-response /' \ 82 -e 's/-s /-q /' \ 83 -e 's/-o /-O /' \ 84 -e 's/-C - /-c /') 85 # shellcheck disable=SC2086 86 eval wget $ARGS 87 fi 88 } 89 90 install_nvm_from_git() { 91 local INSTALL_DIR 92 INSTALL_DIR="$(nvm_install_dir)" 93 94 if [ -d "$INSTALL_DIR/.git" ]; then 95 echo "=> nvm is already installed in $INSTALL_DIR, trying to update using git" 96 command printf '\r=> ' 97 command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch origin tag "$(nvm_latest_version)" --depth=1 2> /dev/null || { 98 echo >&2 "Failed to update nvm, run 'git fetch' in $INSTALL_DIR yourself." 99 exit 1 100 } 101 else 102 # Cloning to $INSTALL_DIR 103 echo "=> Downloading nvm from git to '$INSTALL_DIR'" 104 command printf '\r=> ' 105 mkdir -p "${INSTALL_DIR}" 106 if [ "$(ls -A "${INSTALL_DIR}")" ]; then 107 command git init "${INSTALL_DIR}" || { 108 echo >&2 'Failed to initialize nvm repo. Please report this!' 109 exit 2 110 } 111 command git --git-dir="${INSTALL_DIR}/.git" remote add origin "$(nvm_source)" 2> /dev/null \ 112 || command git --git-dir="${INSTALL_DIR}/.git" remote set-url origin "$(nvm_source)" || { 113 echo >&2 'Failed to add remote "origin" (or set the URL). Please report this!' 114 exit 2 115 } 116 command git --git-dir="${INSTALL_DIR}/.git" fetch origin tag "$(nvm_latest_version)" --depth=1 || { 117 echo >&2 'Failed to fetch origin with tags. Please report this!' 118 exit 2 119 } 120 else 121 command git -c advice.detachedHead=false clone "$(nvm_source)" -b "$(nvm_latest_version)" --depth=1 "${INSTALL_DIR}" || { 122 echo >&2 'Failed to clone nvm repo. Please report this!' 123 exit 2 124 } 125 fi 126 fi 127 command git -c advice.detachedHead=false --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" checkout -f --quiet "$(nvm_latest_version)" 128 if [ -n "$(command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" show-ref refs/heads/master)" ]; then 129 if command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch --quiet 2>/dev/null; then 130 command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch --quiet -D master >/dev/null 2>&1 131 else 132 echo >&2 "Your version of git is out of date. Please update it!" 133 command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch -D master >/dev/null 2>&1 134 fi 135 fi 136 137 echo "=> Compressing and cleaning up git repository" 138 if ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" reflog expire --expire=now --all; then 139 echo >&2 "Your version of git is out of date. Please update it!" 140 fi 141 if ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" gc --auto --aggressive --prune=now ; then 142 echo >&2 "Your version of git is out of date. Please update it!" 143 fi 144 return 145 } 146 147 # 148 # Automatically install Node.js 149 # 150 nvm_install_node() { 151 local NODE_VERSION_LOCAL 152 NODE_VERSION_LOCAL="$(nvm_node_version)" 153 154 if [ -z "$NODE_VERSION_LOCAL" ]; then 155 return 0 156 fi 157 158 echo "=> Installing Node.js version $NODE_VERSION_LOCAL" 159 nvm install "$NODE_VERSION_LOCAL" 160 local CURRENT_NVM_NODE 161 162 CURRENT_NVM_NODE="$(nvm_version current)" 163 if [ "$(nvm_version "$NODE_VERSION_LOCAL")" == "$CURRENT_NVM_NODE" ]; then 164 echo "=> Node.js version $NODE_VERSION_LOCAL has been successfully installed" 165 else 166 echo >&2 "Failed to install Node.js $NODE_VERSION_LOCAL" 167 fi 168 } 169 170 install_nvm_as_script() { 171 local INSTALL_DIR 172 INSTALL_DIR="$(nvm_install_dir)" 173 local NVM_SOURCE_LOCAL 174 NVM_SOURCE_LOCAL="$(nvm_source script)" 175 local NVM_EXEC_SOURCE 176 NVM_EXEC_SOURCE="$(nvm_source script-nvm-exec)" 177 local NVM_BASH_COMPLETION_SOURCE 178 NVM_BASH_COMPLETION_SOURCE="$(nvm_source script-nvm-bash-completion)" 179 180 # Downloading to $INSTALL_DIR 181 mkdir -p "$INSTALL_DIR" 182 if [ -f "$INSTALL_DIR/nvm.sh" ]; then 183 echo "=> nvm is already installed in $INSTALL_DIR, trying to update the script" 184 else 185 echo "=> Downloading nvm as script to '$INSTALL_DIR'" 186 fi 187 nvm_download -s "$NVM_SOURCE_LOCAL" -o "$INSTALL_DIR/nvm.sh" || { 188 echo >&2 "Failed to download '$NVM_SOURCE_LOCAL'" 189 return 1 190 } & 191 nvm_download -s "$NVM_EXEC_SOURCE" -o "$INSTALL_DIR/nvm-exec" || { 192 echo >&2 "Failed to download '$NVM_EXEC_SOURCE'" 193 return 2 194 } & 195 nvm_download -s "$NVM_BASH_COMPLETION_SOURCE" -o "$INSTALL_DIR/bash_completion" || { 196 echo >&2 "Failed to download '$NVM_BASH_COMPLETION_SOURCE'" 197 return 2 198 } & 199 for job in $(jobs -p | command sort) 200 do 201 wait "$job" || return $? 202 done 203 chmod a+x "$INSTALL_DIR/nvm-exec" || { 204 echo >&2 "Failed to mark '$INSTALL_DIR/nvm-exec' as executable" 205 return 3 206 } 207 } 208 209 nvm_try_profile() { 210 if [ -z "${1-}" ] || [ ! -f "${1}" ]; then 211 return 1 212 fi 213 echo "${1}" 214 } 215 216 # 217 # Detect profile file if not specified as environment variable 218 # (eg: PROFILE=~/.myprofile) 219 # The echo'ed path is guaranteed to be an existing file 220 # Otherwise, an empty string is returned 221 # 222 nvm_detect_profile() { 223 if [ "${PROFILE-}" = '/dev/null' ]; then 224 # the user has specifically requested NOT to have nvm touch their profile 225 return 226 fi 227 228 if [ -n "${PROFILE}" ] && [ -f "${PROFILE}" ]; then 229 echo "${PROFILE}" 230 return 231 fi 232 233 local DETECTED_PROFILE 234 DETECTED_PROFILE='' 235 236 if [ -n "${BASH_VERSION-}" ]; then 237 if [ -f "$HOME/.bashrc" ]; then 238 DETECTED_PROFILE="$HOME/.bashrc" 239 elif [ -f "$HOME/.bash_profile" ]; then 240 DETECTED_PROFILE="$HOME/.bash_profile" 241 fi 242 elif [ -n "${ZSH_VERSION-}" ]; then 243 DETECTED_PROFILE="$HOME/.zshrc" 244 fi 245 246 if [ -z "$DETECTED_PROFILE" ]; then 247 for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".zshrc" 248 do 249 if DETECTED_PROFILE="$(nvm_try_profile "${HOME}/${EACH_PROFILE}")"; then 250 break 251 fi 252 done 253 fi 254 255 if [ -n "$DETECTED_PROFILE" ]; then 256 echo "$DETECTED_PROFILE" 257 fi 258 } 259 260 # 261 # Check whether the user has any globally-installed npm modules in their system 262 # Node, and warn them if so. 263 # 264 nvm_check_global_modules() { 265 command -v npm >/dev/null 2>&1 || return 0 266 267 local NPM_VERSION 268 NPM_VERSION="$(npm --version)" 269 NPM_VERSION="${NPM_VERSION:--1}" 270 [ "${NPM_VERSION%%[!-0-9]*}" -gt 0 ] || return 0 271 272 local NPM_GLOBAL_MODULES 273 NPM_GLOBAL_MODULES="$( 274 npm list -g --depth=0 | 275 command sed -e '/ npm@/d' -e '/ (empty)$/d' 276 )" 277 278 local MODULE_COUNT 279 MODULE_COUNT="$( 280 command printf %s\\n "$NPM_GLOBAL_MODULES" | 281 command sed -ne '1!p' | # Remove the first line 282 wc -l | command tr -d ' ' # Count entries 283 )" 284 285 if [ "${MODULE_COUNT}" != '0' ]; then 286 # shellcheck disable=SC2016 287 echo '=> You currently have modules installed globally with `npm`. These will no' 288 # shellcheck disable=SC2016 289 echo '=> longer be linked to the active version of Node when you install a new node' 290 # shellcheck disable=SC2016 291 echo '=> with `nvm`; and they may (depending on how you construct your `$PATH`)' 292 # shellcheck disable=SC2016 293 echo '=> override the binaries of modules installed with `nvm`:' 294 echo 295 296 command printf %s\\n "$NPM_GLOBAL_MODULES" 297 echo '=> If you wish to uninstall them at a later point (or re-install them under your' 298 # shellcheck disable=SC2016 299 echo '=> `nvm` Nodes), you can remove them from the system Node as follows:' 300 echo 301 echo ' $ nvm use system' 302 echo ' $ npm uninstall -g a_module' 303 echo 304 fi 305 } 306 307 nvm_do_install() { 308 if [ -n "${NVM_DIR-}" ] && ! [ -d "${NVM_DIR}" ]; then 309 echo >&2 "You have \$NVM_DIR set to \"${NVM_DIR}\", but that directory does not exist. Check your profile files and environment." 310 exit 1 311 fi 312 if [ -z "${METHOD}" ]; then 313 # Autodetect install method 314 if nvm_has git; then 315 install_nvm_from_git 316 elif nvm_has nvm_download; then 317 install_nvm_as_script 318 else 319 echo >&2 'You need git, curl, or wget to install nvm' 320 exit 1 321 fi 322 elif [ "${METHOD}" = 'git' ]; then 323 if ! nvm_has git; then 324 echo >&2 "You need git to install nvm" 325 exit 1 326 fi 327 install_nvm_from_git 328 elif [ "${METHOD}" = 'script' ]; then 329 if ! nvm_has nvm_download; then 330 echo >&2 "You need curl or wget to install nvm" 331 exit 1 332 fi 333 install_nvm_as_script 334 else 335 echo >&2 "The environment variable \$METHOD is set to \"${METHOD}\", which is not recognized as a valid installation method." 336 exit 1 337 fi 338 339 echo 340 341 local NVM_PROFILE 342 NVM_PROFILE="$(nvm_detect_profile)" 343 local PROFILE_INSTALL_DIR 344 PROFILE_INSTALL_DIR="$(nvm_install_dir | command sed "s:^$HOME:\$HOME:")" 345 346 SOURCE_STR="\\nexport NVM_DIR=\"${PROFILE_INSTALL_DIR}\"\\n[ -s \"\$NVM_DIR/nvm.sh\" ] && \\. \"\$NVM_DIR/nvm.sh\" # This loads nvm\\n" 347 348 # shellcheck disable=SC2016 349 COMPLETION_STR='[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion\n' 350 BASH_OR_ZSH=false 351 352 if [ -z "${NVM_PROFILE-}" ] ; then 353 local TRIED_PROFILE 354 if [ -n "${PROFILE}" ]; then 355 TRIED_PROFILE="${NVM_PROFILE} (as defined in \$PROFILE), " 356 fi 357 echo "=> Profile not found. Tried ${TRIED_PROFILE-}~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile." 358 echo "=> Create one of them and run this script again" 359 echo " OR" 360 echo "=> Append the following lines to the correct file yourself:" 361 command printf "${SOURCE_STR}" 362 echo 363 else 364 if nvm_profile_is_bash_or_zsh "${NVM_PROFILE-}"; then 365 BASH_OR_ZSH=true 366 fi 367 if ! command grep -qc '/nvm.sh' "$NVM_PROFILE"; then 368 echo "=> Appending nvm source string to $NVM_PROFILE" 369 command printf "${SOURCE_STR}" >> "$NVM_PROFILE" 370 else 371 echo "=> nvm source string already in ${NVM_PROFILE}" 372 fi 373 # shellcheck disable=SC2016 374 if ${BASH_OR_ZSH} && ! command grep -qc '$NVM_DIR/bash_completion' "$NVM_PROFILE"; then 375 echo "=> Appending bash_completion source string to $NVM_PROFILE" 376 command printf "$COMPLETION_STR" >> "$NVM_PROFILE" 377 else 378 echo "=> bash_completion source string already in ${NVM_PROFILE}" 379 fi 380 fi 381 if ${BASH_OR_ZSH} && [ -z "${NVM_PROFILE-}" ] ; then 382 echo "=> Please also append the following lines to the if you are using bash/zsh shell:" 383 command printf "${COMPLETION_STR}" 384 fi 385 386 # Source nvm 387 # shellcheck source=/dev/null 388 \. "$(nvm_install_dir)/nvm.sh" 389 390 nvm_check_global_modules 391 392 nvm_install_node 393 394 nvm_reset 395 396 echo "=> Close and reopen your terminal to start using nvm or run the following to use it now:" 397 command printf "${SOURCE_STR}" 398 if ${BASH_OR_ZSH} ; then 399 command printf "${COMPLETION_STR}" 400 fi 401 } 402 403 # 404 # Unsets the various functions defined 405 # during the execution of the install script 406 # 407 nvm_reset() { 408 unset -f nvm_has nvm_install_dir nvm_latest_version nvm_profile_is_bash_or_zsh \ 409 nvm_source nvm_node_version nvm_download install_nvm_from_git nvm_install_node \ 410 install_nvm_as_script nvm_try_profile nvm_detect_profile nvm_check_global_modules \ 411 nvm_do_install nvm_reset 412 } 413 414 [ "_$NVM_ENV" = "_testing" ] || nvm_do_install 415 416 } # this ensures the entire script is downloaded #