Skip to content
Myr

install.sh

This file

Path

install.sh

536 lines · 17.6 KiB · Shell

Last changed by

[Fix] `install.sh`: start a new line before appending to a profile that does not end with onelazergbefore adoptione45c2df

History of this file

Take it

1#!/usr/bin/env bash23{ # this ensures the entire script is downloaded #45nvm_has() {6  type "$1" > /dev/null 2>&17}89# resolves like `command "${1}"` does: only executables on the PATH,10# ignoring shell functions and aliases11nvm_has_executable() {12  (13    # `|| true` so that shells with errexit-style options (eg, zsh's ERR_RETURN)14    # do not abort the subshell when the name is not an alias or a function15    unalias "${1-}" 2>/dev/null || true16    unset -f "${1-}" 2>/dev/null || true17    command -v "${1-}" > /dev/null 2>&118  )19}2021nvm_echo() {22  command printf %s\\n "$*" 2>/dev/null23}2425if [ -z "${BASH_VERSION}" ] || [ -n "${ZSH_VERSION}" ]; then26  # shellcheck disable=SC201627  nvm_echo >&2 'Error: the install instructions explicitly say to pipe the install script to `bash`; please follow them'28  exit 129fi3031nvm_grep() {32  GREP_OPTIONS='' command grep "$@"33}3435nvm_default_install_dir() {36  [ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm"37}3839nvm_install_dir() {40  if [ -n "$NVM_DIR" ]; then41    printf %s "${NVM_DIR}"42  else43    nvm_default_install_dir44  fi45}4647nvm_latest_version() {48  nvm_echo "v0.40.8"49}5051nvm_profile_is_bash_or_zsh() {52  local TEST_PROFILE53  TEST_PROFILE="${1-}"54  case "${TEST_PROFILE-}" in55    *"/.bashrc" | *"/.bash_profile" | *"/.zshrc" | *"/.zprofile")56      return57    ;;58    *)59      return 160    ;;61  esac62}6364#65# Outputs the location to NVM depending on:66# * The availability of $NVM_SOURCE67# * The presence of $NVM_INSTALL_GITHUB_REPO68# * The method used ("script" or "git" in the script, defaults to "git")69# NVM_SOURCE always takes precedence unless the method is "script-nvm-exec"70#71nvm_source() {72  local NVM_GITHUB_REPO73  NVM_GITHUB_REPO="${NVM_INSTALL_GITHUB_REPO:-nvm-sh/nvm}"74  if [ "${NVM_GITHUB_REPO}" != 'nvm-sh/nvm' ]; then75    { nvm_echo >&2 "$(cat)" ; } << EOF76@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@77@    WARNING: REMOTE REPO IDENTIFICATION HAS CHANGED!     @78@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@79IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!8081The default repository for this install is \`nvm-sh/nvm\`,82but the environment variable \`\$NVM_INSTALL_GITHUB_REPO\` is83currently set to \`${NVM_GITHUB_REPO}\`.8485If this is not intentional, interrupt this installation and86verify your environment variables.87EOF88  fi89  local NVM_VERSION90  NVM_VERSION="${NVM_INSTALL_VERSION:-$(nvm_latest_version)}"91  local NVM_METHOD92  NVM_METHOD="$1"93  local NVM_SOURCE_URL94  NVM_SOURCE_URL="$NVM_SOURCE"95  if [ "_$NVM_METHOD" = "_script-nvm-exec" ]; then96    NVM_SOURCE_URL="https://raw.githubusercontent.com/${NVM_GITHUB_REPO}/${NVM_VERSION}/nvm-exec"97  elif [ "_$NVM_METHOD" = "_script-nvm-bash-completion" ]; then98    NVM_SOURCE_URL="https://raw.githubusercontent.com/${NVM_GITHUB_REPO}/${NVM_VERSION}/bash_completion"99  elif [ -z "$NVM_SOURCE_URL" ]; then100    if [ "_$NVM_METHOD" = "_script" ]; then101      NVM_SOURCE_URL="https://raw.githubusercontent.com/${NVM_GITHUB_REPO}/${NVM_VERSION}/nvm.sh"102    elif [ "_$NVM_METHOD" = "_git" ] || [ -z "$NVM_METHOD" ]; then103      NVM_SOURCE_URL="https://github.com/${NVM_GITHUB_REPO}.git"104    else105      nvm_echo >&2 "Unexpected value \"$NVM_METHOD\" for \$NVM_METHOD"106      return 1107    fi108  fi109  nvm_echo "$NVM_SOURCE_URL"110}111112#113# Node.js version to install114#115nvm_node_version() {116  nvm_echo "$NODE_VERSION"117}118119nvm_download() {120  if nvm_has_executable "curl"; then121    command curl --fail --compressed -q "$@"122  elif nvm_has_executable "wget"; then123    # Emulate curl with wget124    local NVM_DOWNLOAD_WGET_COUNT125    NVM_DOWNLOAD_WGET_COUNT=$#126    local NVM_DOWNLOAD_WGET_SKIP127    NVM_DOWNLOAD_WGET_SKIP=0128    local NVM_DOWNLOAD_WGET_ARG129    for NVM_DOWNLOAD_WGET_ARG in "$@"; do130      if [ "${NVM_DOWNLOAD_WGET_SKIP}" = '1' ]; then131        NVM_DOWNLOAD_WGET_SKIP=0132        continue133      fi134      case "${NVM_DOWNLOAD_WGET_ARG}" in135        '--progress-bar') set -- "$@" '--progress=bar' ;;136        '--compressed') : ;;137        '--fail') : ;;138        '-L') : ;;139        '-I') set -- "$@" '--server-response' ;;140        '-s') set -- "$@" '-q' ;;141        '-sS') set -- "$@" '-nv' ;;142        '-o') set -- "$@" '-O' ;;143        '-C') NVM_DOWNLOAD_WGET_SKIP=1; set -- "$@" '-c' ;;144        *) set -- "$@" "${NVM_DOWNLOAD_WGET_ARG}" ;;145      esac146    done147    shift "${NVM_DOWNLOAD_WGET_COUNT}"148    command wget "$@"149  fi150}151152install_nvm_from_git() {153  local INSTALL_DIR154  INSTALL_DIR="$(nvm_install_dir)"155  local NVM_VERSION156  NVM_VERSION="${NVM_INSTALL_VERSION:-$(nvm_latest_version)}"157  if [ -n "${NVM_INSTALL_VERSION:-}" ]; then158    # Check if version is an existing ref159    if command git ls-remote "$(nvm_source "git")" "$NVM_VERSION" | nvm_grep -q "$NVM_VERSION" ; then160      :161    # Check if version is an existing changeset162    elif ! nvm_download -o /dev/null "$(nvm_source "script-nvm-exec")"; then163      nvm_echo >&2 "Failed to find '$NVM_VERSION' version."164      exit 1165    fi166  fi167168  local fetch_error169  if [ -d "$INSTALL_DIR/.git" ]; then170    # Updating repo171    nvm_echo "=> nvm is already installed in $INSTALL_DIR, trying to update using git"172    command printf '\r=> '173    fetch_error="Failed to update nvm with $NVM_VERSION, run 'git fetch' in $INSTALL_DIR yourself."174  else175    fetch_error="Failed to fetch origin with $NVM_VERSION. Please report this!"176    nvm_echo "=> Downloading nvm from git to '$INSTALL_DIR'"177    command printf '\r=> '178    mkdir -p "${INSTALL_DIR}" || {179      nvm_echo >&2 "Failed to create directory '${INSTALL_DIR}'"180      exit 2181    }182    if [ "$(ls -A "${INSTALL_DIR}")" ]; then183      # Initializing repo184      command git init "${INSTALL_DIR}" || {185        nvm_echo >&2 'Failed to initialize nvm repo. Please report this!'186        exit 2187      }188      command git --git-dir="${INSTALL_DIR}/.git" remote add origin "$(nvm_source)" 2> /dev/null \189        || command git --git-dir="${INSTALL_DIR}/.git" remote set-url origin "$(nvm_source)" || {190        nvm_echo >&2 'Failed to add remote "origin" (or set the URL). Please report this!'191        exit 2192      }193    else194      # Cloning repo195      command git clone -o origin "$(nvm_source)" --depth=1 "${INSTALL_DIR}" || {196        nvm_echo >&2 'Failed to clone nvm repo. Please report this!'197        exit 2198      }199    fi200  fi201  # Try to fetch tag202  if command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch origin tag "$NVM_VERSION" --depth=1 2>/dev/null; then203    :204  # Fetch given version205  elif ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch origin "$NVM_VERSION" --depth=1; then206    nvm_echo >&2 "$fetch_error"207    exit 1208  fi209  command git -c advice.detachedHead=false --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" checkout -f --quiet FETCH_HEAD || {210    nvm_echo >&2 "Failed to checkout the given version $NVM_VERSION. Please report this!"211    exit 2212  }213  if [ -n "$(command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" show-ref refs/heads/master)" ]; then214    if command git --no-pager --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch --quiet 2>/dev/null; then215      command git --no-pager --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch --quiet -D master >/dev/null 2>&1216    else217      nvm_echo >&2 "Your version of git is out of date. Please update it!"218      command git --no-pager --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch -D master >/dev/null 2>&1219    fi220  fi221222  nvm_echo "=> Compressing and cleaning up git repository"223  if ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" reflog expire --expire=now --all; then224    nvm_echo >&2 "Your version of git is out of date. Please update it!"225  fi226  if ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" gc --auto --aggressive --prune=now ; then227    nvm_echo >&2 "Your version of git is out of date. Please update it!"228  fi229  return230}231232#233# Automatically install Node.js234#235nvm_install_node() {236  local NODE_VERSION_LOCAL237  NODE_VERSION_LOCAL="$(nvm_node_version)"238239  if [ -z "$NODE_VERSION_LOCAL" ]; then240    return 0241  fi242243  nvm_echo "=> Installing Node.js version $NODE_VERSION_LOCAL"244  nvm install "$NODE_VERSION_LOCAL"245  local CURRENT_NVM_NODE246247  CURRENT_NVM_NODE="$(nvm_version current)"248  if [ "$(nvm_version "$NODE_VERSION_LOCAL")" = "$CURRENT_NVM_NODE" ]; then249    nvm_echo "=> Node.js version $NODE_VERSION_LOCAL has been successfully installed"250  else251    nvm_echo >&2 "Failed to install Node.js $NODE_VERSION_LOCAL"252  fi253}254255install_nvm_as_script() {256  local INSTALL_DIR257  INSTALL_DIR="$(nvm_install_dir)"258  local NVM_SOURCE_LOCAL259  NVM_SOURCE_LOCAL="$(nvm_source script)"260  local NVM_EXEC_SOURCE261  NVM_EXEC_SOURCE="$(nvm_source script-nvm-exec)"262  local NVM_BASH_COMPLETION_SOURCE263  NVM_BASH_COMPLETION_SOURCE="$(nvm_source script-nvm-bash-completion)"264265  # Downloading to $INSTALL_DIR266  mkdir -p "$INSTALL_DIR" || {267    nvm_echo >&2 "Failed to create directory '$INSTALL_DIR'"268    return 1269  }270  if [ -f "$INSTALL_DIR/nvm.sh" ]; then271    nvm_echo "=> nvm is already installed in $INSTALL_DIR, trying to update the script"272  else273    nvm_echo "=> Downloading nvm as script to '$INSTALL_DIR'"274  fi275  nvm_download -s "$NVM_SOURCE_LOCAL" -o "$INSTALL_DIR/nvm.sh" || {276    nvm_echo >&2 "Failed to download '$NVM_SOURCE_LOCAL'"277    return 1278  } &279  nvm_download -s "$NVM_EXEC_SOURCE" -o "$INSTALL_DIR/nvm-exec" || {280    nvm_echo >&2 "Failed to download '$NVM_EXEC_SOURCE'"281    return 2282  } &283  nvm_download -s "$NVM_BASH_COMPLETION_SOURCE" -o "$INSTALL_DIR/bash_completion" || {284    nvm_echo >&2 "Failed to download '$NVM_BASH_COMPLETION_SOURCE'"285    return 2286  } &287  for job in $(jobs -p | command sort)288  do289    wait "$job" || return $?290  done291  chmod a+x "$INSTALL_DIR/nvm-exec" || {292    nvm_echo >&2 "Failed to mark '$INSTALL_DIR/nvm-exec' as executable"293    return 3294  }295}296297nvm_try_profile() {298  if [ -z "${1-}" ] || [ ! -f "${1}" ]; then299    return 1300  fi301  nvm_echo "${1}"302}303304#305# Detect profile file if not specified as environment variable306# (eg: PROFILE=~/.myprofile)307# The echo'ed path is guaranteed to be an existing file308# Otherwise, an empty string is returned309#310nvm_detect_profile() {311  if [ "${PROFILE-}" = '/dev/null' ]; then312    # the user has specifically requested NOT to have nvm touch their profile313    return314  fi315316  if [ -n "${PROFILE}" ] && [ -f "${PROFILE}" ]; then317    nvm_echo "${PROFILE}"318    return319  fi320321  local DETECTED_PROFILE322  DETECTED_PROFILE=''323324  if [ "${SHELL#*bash}" != "$SHELL" ]; then325    if [ -f "$HOME/.bashrc" ]; then326      DETECTED_PROFILE="$HOME/.bashrc"327    elif [ -f "$HOME/.bash_profile" ]; then328      DETECTED_PROFILE="$HOME/.bash_profile"329    fi330  elif [ "${SHELL#*zsh}" != "$SHELL" ]; then331    if [ -f "${ZDOTDIR:-${HOME}}/.zshrc" ]; then332      DETECTED_PROFILE="${ZDOTDIR:-${HOME}}/.zshrc"333    elif [ -f "${ZDOTDIR:-${HOME}}/.zprofile" ]; then334      DETECTED_PROFILE="${ZDOTDIR:-${HOME}}/.zprofile"335    fi336  fi337338  if [ -z "$DETECTED_PROFILE" ]; then339    for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".zprofile" ".zshrc"340    do341      if DETECTED_PROFILE="$(nvm_try_profile "${ZDOTDIR:-${HOME}}/${EACH_PROFILE}")"; then342        break343      fi344    done345  fi346347  if [ -n "$DETECTED_PROFILE" ]; then348    nvm_echo "$DETECTED_PROFILE"349  fi350}351352#353# Check whether the user has any globally-installed npm modules in their system354# Node, and warn them if so.355#356nvm_check_global_modules() {357  local NPM_COMMAND358  NPM_COMMAND="$(command -v npm 2>/dev/null)" || return 0359  [ -n "${NVM_DIR}" ] && [ -z "${NPM_COMMAND%%"$NVM_DIR"/*}" ] && return 0360361  local NPM_VERSION362  NPM_VERSION="$(npm --version)"363  NPM_VERSION="${NPM_VERSION:--1}"364  [ "${NPM_VERSION%%[!-0-9]*}" -gt 0 ] || return 0365366  local NPM_GLOBAL_MODULES367  NPM_GLOBAL_MODULES="$(368    npm list -g --depth=0 |369    command sed -e '/ npm@/d' -e '/ (empty)$/d'370  )"371372  local MODULE_COUNT373  MODULE_COUNT="$(374    command printf %s\\n "$NPM_GLOBAL_MODULES" |375    command sed -ne '1!p' |                     # Remove the first line376    wc -l | command tr -d ' '                   # Count entries377  )"378379  if [ "${MODULE_COUNT}" != '0' ]; then380    # shellcheck disable=SC2016381    nvm_echo '=> You currently have modules installed globally with `npm`. These will no'382    # shellcheck disable=SC2016383    nvm_echo '=> longer be linked to the active version of Node when you install a new node'384    # shellcheck disable=SC2016385    nvm_echo '=> with `nvm`; and they may (depending on how you construct your `$PATH`)'386    # shellcheck disable=SC2016387    nvm_echo '=> override the binaries of modules installed with `nvm`:'388    nvm_echo389390    command printf %s\\n "$NPM_GLOBAL_MODULES"391    nvm_echo '=> If you wish to uninstall them at a later point (or re-install them under your'392    # shellcheck disable=SC2016393    nvm_echo '=> `nvm` node installs), you can remove them from the system Node as follows:'394    nvm_echo395    nvm_echo '     $ nvm use system'396    nvm_echo '     $ npm uninstall -g a_module'397    nvm_echo398  fi399}400401nvm_do_install() {402  if [ -n "${NVM_DIR-}" ] && ! [ -d "${NVM_DIR}" ]; then403    if [ -e "${NVM_DIR}" ]; then404      nvm_echo >&2 "File \"${NVM_DIR}\" has the same name as installation directory."405      exit 1406    fi407408    if [ "${NVM_DIR}" = "$(nvm_default_install_dir)" ]; then409      mkdir "${NVM_DIR}" || {410        nvm_echo >&2 "Failed to create directory '${NVM_DIR}'"411        exit 2412      }413    else414      nvm_echo >&2 "You have \$NVM_DIR set to \"${NVM_DIR}\", but that directory does not exist. Check your profile files and environment."415      exit 1416    fi417  fi418  # Disable the optional which check, https://www.shellcheck.net/wiki/SC2230419  # shellcheck disable=SC2230420  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' ]; then421    nvm_echo >&2 'You may be on a Mac, and need to install the Xcode Command Line Developer Tools.'422    # shellcheck disable=SC2016423    nvm_echo >&2 'If so, run `xcode-select --install` and try again. If not, please report this!'424    exit 1425  fi426  if [ -z "${METHOD}" ]; then427    # Autodetect install method428    if nvm_has git; then429      install_nvm_from_git430    elif nvm_has_executable curl || nvm_has_executable wget; then431      install_nvm_as_script432    else433      nvm_echo >&2 'You need git, curl, or wget to install nvm'434      exit 1435    fi436  elif [ "${METHOD}" = 'git' ]; then437    if ! nvm_has git; then438      nvm_echo >&2 "You need git to install nvm"439      exit 1440    fi441    install_nvm_from_git442  elif [ "${METHOD}" = 'script' ]; then443    if ! nvm_has_executable curl && ! nvm_has_executable wget; then444      nvm_echo >&2 "You need curl or wget to install nvm"445      exit 1446    fi447    install_nvm_as_script448  else449    nvm_echo >&2 "The environment variable \$METHOD is set to \"${METHOD}\", which is not recognized as a valid installation method."450    exit 1451  fi452453  nvm_echo454455  local NVM_PROFILE456  NVM_PROFILE="$(nvm_detect_profile)"457  local PROFILE_INSTALL_DIR458  PROFILE_INSTALL_DIR="$(nvm_install_dir | command sed "s:^$HOME:\$HOME:")"459460  SOURCE_STR="\\nexport NVM_DIR=\"${PROFILE_INSTALL_DIR}\"\\n[ -s \"\$NVM_DIR/nvm.sh\" ] && \\. \"\$NVM_DIR/nvm.sh\"  # This loads nvm\\n"461462  # shellcheck disable=SC2016463  COMPLETION_STR='[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion\n'464  BASH_OR_ZSH=false465466  if [ "${PROFILE-}" = '/dev/null' ] ; then467    # the user has specifically requested NOT to have nvm touch their profile468    echo469  elif [ -z "${NVM_PROFILE-}" ] ; then470    local TRIED_PROFILE471    if [ -n "${PROFILE}" ]; then472      TRIED_PROFILE="${PROFILE} (as defined in \$PROFILE), "473    fi474    nvm_echo "=> Profile not found. Tried ${TRIED_PROFILE-}~/.bashrc, ~/.bash_profile, ~/.zprofile, ~/.zshrc, and ~/.profile."475    nvm_echo "=> Create one of them and run this script again"476    nvm_echo "   OR"477    nvm_echo "=> Append the following lines to the correct file yourself:"478    command printf '%b' "${SOURCE_STR}"479    nvm_echo480  else481    if nvm_profile_is_bash_or_zsh "${NVM_PROFILE-}"; then482      BASH_OR_ZSH=true483    fi484    if ! command grep -qc '/nvm.sh' "$NVM_PROFILE"; then485      nvm_echo "=> Appending nvm source string to $NVM_PROFILE"486      command printf '%b' "${SOURCE_STR}" >> "$NVM_PROFILE"487    else488      nvm_echo "=> nvm source string already in ${NVM_PROFILE}"489    fi490    # shellcheck disable=SC2016491    if ${BASH_OR_ZSH} && ! command grep -qc '$NVM_DIR/bash_completion' "$NVM_PROFILE"; then492      nvm_echo "=> Appending bash_completion source string to $NVM_PROFILE"493      if [ -n "$(command tail -c 1 "$NVM_PROFILE")" ]; then494        nvm_echo >> "$NVM_PROFILE"495      fi496      command printf '%b' "$COMPLETION_STR" >> "$NVM_PROFILE"497    else498      nvm_echo "=> bash_completion source string already in ${NVM_PROFILE}"499    fi500  fi501  if ${BASH_OR_ZSH} && [ -z "${NVM_PROFILE-}" ] ; then502    nvm_echo "=> Please also append the following lines to the correct file if you are using bash/zsh shell:"503    command printf "${COMPLETION_STR}"504  fi505506  # Source nvm507  # shellcheck source=/dev/null508  \. "$(nvm_install_dir)/nvm.sh"509510  nvm_check_global_modules511512  nvm_install_node513514  nvm_reset515516  nvm_echo "=> Close and reopen your terminal to start using nvm or run the following to use it now:"517  command printf '%b' "${SOURCE_STR}"518  if ${BASH_OR_ZSH} ; then519    command printf "${COMPLETION_STR}"520  fi521}522523#524# Unsets the various functions defined525# during the execution of the install script526#527nvm_reset() {528  unset -f nvm_has nvm_has_executable nvm_install_dir nvm_latest_version nvm_profile_is_bash_or_zsh \529    nvm_source nvm_node_version nvm_download install_nvm_from_git nvm_install_node \530    install_nvm_as_script nvm_try_profile nvm_detect_profile nvm_check_global_modules \531    nvm_do_install nvm_reset nvm_default_install_dir nvm_grep532}533534[ "_$NVM_ENV" = "_testing" ] || nvm_do_install535536} # this ensures the entire script is downloaded #