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