Koen Punt | 3c4bf80 | 2012-10-30 00:03:01 +0100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Koen Punt | 0787a55 | 2013-12-22 17:46:26 +0100 | [diff] [blame] | 3 | set -e |
| 4 | |
Jordan Harband | 7e26996 | 2014-07-05 13:42:44 -0700 | [diff] [blame] | 5 | nvm_has() { |
Koen Punt | 3000226 | 2014-03-15 04:32:35 +0100 | [diff] [blame] | 6 | type "$1" > /dev/null 2>&1 |
Koen Punt | 3000226 | 2014-03-15 04:32:35 +0100 | [diff] [blame] | 7 | } |
Koen Punt | d2422a6 | 2013-12-22 18:23:59 +0100 | [diff] [blame] | 8 | |
Koen Punt | 3000226 | 2014-03-15 04:32:35 +0100 | [diff] [blame] | 9 | if [ -z "$NVM_DIR" ]; then |
Koen Punt | d2422a6 | 2013-12-22 18:23:59 +0100 | [diff] [blame] | 10 | NVM_DIR="$HOME/.nvm" |
| 11 | fi |
Koen Punt | 3c4bf80 | 2012-10-30 00:03:01 +0100 | [diff] [blame] | 12 | |
Jordan Harband | 8e45afb | 2014-12-28 15:57:18 -0800 | [diff] [blame] | 13 | nvm_latest_version() { |
Jordan Harband | 0f1f3ed | 2015-02-02 20:26:00 -0800 | [diff] [blame] | 14 | echo "v0.23.3" |
Jordan Harband | 8e45afb | 2014-12-28 15:57:18 -0800 | [diff] [blame] | 15 | } |
| 16 | |
Xavier Cambar | 516e553 | 2014-10-31 13:37:59 +0100 | [diff] [blame] | 17 | # |
| 18 | # Outputs the location to NVM depending on: |
| 19 | # * The availability of $NVM_SOURCE |
| 20 | # * The method used ("script" or "git" in the script, defaults to "git") |
| 21 | # NVM_SOURCE always takes precedence |
| 22 | # |
| 23 | nvm_source() { |
| 24 | local NVM_METHOD |
| 25 | NVM_METHOD="$1" |
| 26 | if [ -z "$NVM_SOURCE" ]; then |
| 27 | local NVM_SOURCE |
Jordan Harband | fd2fb24 | 2014-12-28 15:54:09 -0800 | [diff] [blame] | 28 | if [ "_$NVM_METHOD" = "_script" ]; then |
Jordan Harband | 8e45afb | 2014-12-28 15:57:18 -0800 | [diff] [blame] | 29 | NVM_SOURCE="https://raw.githubusercontent.com/creationix/nvm/$(nvm_latest_version)/nvm.sh" |
Jordan Harband | fd2fb24 | 2014-12-28 15:54:09 -0800 | [diff] [blame] | 30 | elif [ "_$NVM_METHOD" = "_script-nvm-exec" ]; then |
Jordan Harband | 8e45afb | 2014-12-28 15:57:18 -0800 | [diff] [blame] | 31 | NVM_SOURCE="https://raw.githubusercontent.com/creationix/nvm/$(nvm_latest_version)/nvm-exec" |
Jordan Harband | fd2fb24 | 2014-12-28 15:54:09 -0800 | [diff] [blame] | 32 | elif [ "_$NVM_METHOD" = "_git" ] || [ -z "$NVM_METHOD" ]; then |
| 33 | NVM_SOURCE="https://github.com/creationix/nvm.git" |
| 34 | else |
| 35 | echo >&2 "Unexpected value \"$NVM_METHOD\" for \$NVM_METHOD" |
| 36 | return 1 |
| 37 | fi |
Xavier Cambar | 516e553 | 2014-10-31 13:37:59 +0100 | [diff] [blame] | 38 | fi |
| 39 | echo "$NVM_SOURCE" |
| 40 | return 0 |
| 41 | } |
| 42 | |
Jordan Harband | 63f72b3 | 2014-07-07 15:40:59 -0700 | [diff] [blame] | 43 | nvm_download() { |
Jordan Harband | 74cc1eb | 2014-07-07 10:04:20 -0700 | [diff] [blame] | 44 | if nvm_has "curl"; then |
Jordan Harband | 25c0be1 | 2014-07-05 13:51:13 -0700 | [diff] [blame] | 45 | curl $* |
Jordan Harband | 74cc1eb | 2014-07-07 10:04:20 -0700 | [diff] [blame] | 46 | elif nvm_has "wget"; then |
Koen Punt | 5342b6a | 2014-03-26 09:29:05 +0100 | [diff] [blame] | 47 | # Emulate curl with wget |
Jordan Harband | 3fc82d6 | 2015-01-09 01:50:05 -0800 | [diff] [blame] | 48 | ARGS=$(echo "$*" | command sed -e 's/--progress-bar /--progress=bar /' \ |
Koen Punt | 9c2127c | 2014-07-18 16:18:17 +0200 | [diff] [blame] | 49 | -e 's/-L //' \ |
Jordan Harband | 708ac80 | 2014-08-15 20:21:46 -0700 | [diff] [blame] | 50 | -e 's/-I /--server-response /' \ |
Koen Punt | 9c2127c | 2014-07-18 16:18:17 +0200 | [diff] [blame] | 51 | -e 's/-s /-q /' \ |
| 52 | -e 's/-o /-O /' \ |
| 53 | -e 's/-C - /-c /') |
Jordan Harband | 74cc1eb | 2014-07-07 10:04:20 -0700 | [diff] [blame] | 54 | wget $ARGS |
Koen Punt | 3000226 | 2014-03-15 04:32:35 +0100 | [diff] [blame] | 55 | fi |
Jordan Harband | 74cc1eb | 2014-07-07 10:04:20 -0700 | [diff] [blame] | 56 | } |
Dennis Dryden | 34a0676 | 2013-06-07 23:42:28 +0100 | [diff] [blame] | 57 | |
Jordan Harband | cce5df3 | 2014-07-05 13:44:00 -0700 | [diff] [blame] | 58 | install_nvm_from_git() { |
Koen Punt | 3000226 | 2014-03-15 04:32:35 +0100 | [diff] [blame] | 59 | if [ -d "$NVM_DIR/.git" ]; then |
Jordan Harband | 2d9494a | 2015-01-22 10:21:04 -0800 | [diff] [blame] | 60 | echo "=> nvm is already installed in $NVM_DIR, trying to update using git" |
Jordan Harband | b6f1c15 | 2014-06-26 10:26:57 -0700 | [diff] [blame] | 61 | printf "\r=> " |
Jordan Harband | bf794ff | 2015-01-11 11:53:24 -0800 | [diff] [blame] | 62 | cd "$NVM_DIR" && (command git fetch 2> /dev/null || { |
Jordan Harband | 55d892a | 2014-07-17 00:19:01 -0700 | [diff] [blame] | 63 | echo >&2 "Failed to update nvm, run 'git fetch' in $NVM_DIR yourself." && exit 1 |
Jordan Harband | e0537ce | 2014-07-05 13:47:22 -0700 | [diff] [blame] | 64 | }) |
Koen Punt | 3000226 | 2014-03-15 04:32:35 +0100 | [diff] [blame] | 65 | else |
| 66 | # Cloning to $NVM_DIR |
Koen Punt | 6ed93f4 | 2014-03-26 09:08:37 +0100 | [diff] [blame] | 67 | echo "=> Downloading nvm from git to '$NVM_DIR'" |
Jordan Harband | b6f1c15 | 2014-06-26 10:26:57 -0700 | [diff] [blame] | 68 | printf "\r=> " |
Koen Punt | 3000226 | 2014-03-15 04:32:35 +0100 | [diff] [blame] | 69 | mkdir -p "$NVM_DIR" |
Jordan Harband | bf794ff | 2015-01-11 11:53:24 -0800 | [diff] [blame] | 70 | command git clone "$(nvm_source git)" "$NVM_DIR" |
Koen Punt | 3000226 | 2014-03-15 04:32:35 +0100 | [diff] [blame] | 71 | fi |
Jordan Harband | bf794ff | 2015-01-11 11:53:24 -0800 | [diff] [blame] | 72 | cd "$NVM_DIR" && command git checkout --quiet $(nvm_latest_version) && command git branch --quiet -D master >/dev/null 2>&1 |
Jordan Harband | 5ad00f1 | 2014-09-25 22:10:48 -0700 | [diff] [blame] | 73 | return |
Koen Punt | 3000226 | 2014-03-15 04:32:35 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Jordan Harband | cce5df3 | 2014-07-05 13:44:00 -0700 | [diff] [blame] | 76 | install_nvm_as_script() { |
Xavier Cambar | 516e553 | 2014-10-31 13:37:59 +0100 | [diff] [blame] | 77 | local NVM_SOURCE |
Jordan Harband | fd2fb24 | 2014-12-28 15:54:09 -0800 | [diff] [blame] | 78 | NVM_SOURCE=$(nvm_source script) |
Jordan Harband | 689c52c | 2014-11-22 10:31:42 -0800 | [diff] [blame] | 79 | local NVM_EXEC_SOURCE |
Jordan Harband | fd2fb24 | 2014-12-28 15:54:09 -0800 | [diff] [blame] | 80 | NVM_EXEC_SOURCE=$(nvm_source script-nvm-exec) |
Koen Punt | 3000226 | 2014-03-15 04:32:35 +0100 | [diff] [blame] | 81 | |
| 82 | # Downloading to $NVM_DIR |
Koen Punt | d2422a6 | 2013-12-22 18:23:59 +0100 | [diff] [blame] | 83 | mkdir -p "$NVM_DIR" |
Koen Punt | 3000226 | 2014-03-15 04:32:35 +0100 | [diff] [blame] | 84 | if [ -d "$NVM_DIR/nvm.sh" ]; then |
Jordan Harband | 2d9494a | 2015-01-22 10:21:04 -0800 | [diff] [blame] | 85 | echo "=> nvm is already installed in $NVM_DIR, trying to update the script" |
Koen Punt | 3000226 | 2014-03-15 04:32:35 +0100 | [diff] [blame] | 86 | else |
Koen Punt | 6ed93f4 | 2014-03-26 09:08:37 +0100 | [diff] [blame] | 87 | echo "=> Downloading nvm as script to '$NVM_DIR'" |
Koen Punt | 3000226 | 2014-03-15 04:32:35 +0100 | [diff] [blame] | 88 | fi |
Jordan Harband | 5904d41 | 2014-11-22 10:29:48 -0800 | [diff] [blame] | 89 | nvm_download -s "$NVM_SOURCE" -o "$NVM_DIR/nvm.sh" || { |
Jordan Harband | 689c52c | 2014-11-22 10:31:42 -0800 | [diff] [blame] | 90 | echo >&2 "Failed to download '$NVM_SOURCE'" |
Koen Punt | 3000226 | 2014-03-15 04:32:35 +0100 | [diff] [blame] | 91 | return 1 |
| 92 | } |
Jordan Harband | 689c52c | 2014-11-22 10:31:42 -0800 | [diff] [blame] | 93 | nvm_download -s "$NVM_EXEC_SOURCE" -o "$NVM_DIR/nvm-exec" || { |
| 94 | echo >&2 "Failed to download '$NVM_EXEC_SOURCE'" |
| 95 | return 2 |
| 96 | } |
| 97 | chmod a+x "$NVM_DIR/nvm-exec" || { |
| 98 | echo >&2 "Failed to mark '$NVM_DIR/nvm-exec' as executable" |
| 99 | return 3 |
| 100 | } |
Koen Punt | 3000226 | 2014-03-15 04:32:35 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 103 | # |
| 104 | # Detect profile file if not specified as environment variable |
| 105 | # (eg: PROFILE=~/.myprofile) |
| 106 | # The echo'ed path is guaranteed to be an existing file |
| 107 | # Otherwise, an empty string is returned |
| 108 | # |
| 109 | nvm_detect_profile() { |
| 110 | if [ -f "$PROFILE" ]; then |
| 111 | echo "$PROFILE" |
| 112 | elif [ -f "$HOME/.bashrc" ]; then |
| 113 | echo "$HOME/.bashrc" |
| 114 | elif [ -f "$HOME/.bash_profile" ]; then |
| 115 | echo "$HOME/.bash_profile" |
| 116 | elif [ -f "$HOME/.zshrc" ]; then |
| 117 | echo "$HOME/.zshrc" |
| 118 | elif [ -f "$HOME/.profile" ]; then |
| 119 | echo "$HOME/.profile" |
| 120 | fi |
| 121 | } |
| 122 | |
elliottcable | 4ba7ee5 | 2015-01-23 20:38:50 -0600 | [diff] [blame] | 123 | # |
| 124 | # Check whether the user has any globally-installed npm modules in their system |
| 125 | # Node, and warn them if so. |
| 126 | # |
| 127 | nvm_check_global_modules() { |
elliottcable | 6cfc309 | 2015-02-02 20:42:12 -0600 | [diff] [blame^] | 128 | command -v npm >/dev/null 2>&1 || return 0 |
| 129 | |
| 130 | local NPM_VERSION |
| 131 | NPM_VERSION="$(npm --version)" |
| 132 | NPM_VERSION="${NPM_VERSION:-0}" |
| 133 | [ "${NPM_VERSION%%[!0-9]*}" -gt 1 ] || return 0 |
| 134 | |
elliottcable | 4ba7ee5 | 2015-01-23 20:38:50 -0600 | [diff] [blame] | 135 | local NPM_GLOBAL_MODULES |
| 136 | NPM_GLOBAL_MODULES=$(npm list -g --depth=0 | sed '/ npm@/d') |
| 137 | |
| 138 | local MODULE_COUNT |
| 139 | MODULE_COUNT=$( |
| 140 | printf %s\\n "$NPM_GLOBAL_MODULES" | |
| 141 | sed -ne '1!p' | # Remove the first line |
| 142 | wc -l | tr -d ' ' # Count entries |
| 143 | ) |
| 144 | |
| 145 | if [ $MODULE_COUNT -ne 0 ]; then |
| 146 | cat <<-'END_MESSAGE' |
| 147 | => You currently have modules installed globally with `npm`. These will no |
| 148 | => longer be linked to the active version of Node when you install a new node |
| 149 | => with `nvm`; and they may (depending on how you construct your `$PATH`) |
| 150 | => override the binaries of modules installed with `nvm`: |
| 151 | |
| 152 | END_MESSAGE |
| 153 | printf %s\\n "$NPM_GLOBAL_MODULES" |
| 154 | cat <<-'END_MESSAGE' |
| 155 | |
| 156 | => If you wish to uninstall them at a later point (or re-install them under your |
| 157 | => `nvm` Nodes), you can remove them from the system Node as follows: |
| 158 | |
| 159 | $ nvm use system |
| 160 | $ npm uninstall -g a_module |
| 161 | |
| 162 | END_MESSAGE |
| 163 | fi |
| 164 | } |
| 165 | |
Xavier Cambar | 3cdec8e | 2014-10-22 19:43:39 +0200 | [diff] [blame] | 166 | nvm_do_install() { |
| 167 | if [ -z "$METHOD" ]; then |
| 168 | # Autodetect install method |
| 169 | if nvm_has "git"; then |
| 170 | install_nvm_from_git |
| 171 | elif nvm_has "nvm_download"; then |
| 172 | install_nvm_as_script |
| 173 | else |
| 174 | echo >&2 "You need git, curl, or wget to install nvm" |
| 175 | exit 1 |
| 176 | fi |
| 177 | elif [ "~$METHOD" = "~git" ]; then |
| 178 | if ! nvm_has "git"; then |
| 179 | echo >&2 "You need git to install nvm" |
| 180 | exit 1 |
| 181 | fi |
Jordan Harband | cce5df3 | 2014-07-05 13:44:00 -0700 | [diff] [blame] | 182 | install_nvm_from_git |
Xavier Cambar | 3cdec8e | 2014-10-22 19:43:39 +0200 | [diff] [blame] | 183 | elif [ "~$METHOD" = "~script" ]; then |
| 184 | if ! nvm_has "nvm_download"; then |
| 185 | echo >&2 "You need curl or wget to install nvm" |
| 186 | exit 1 |
| 187 | fi |
Jordan Harband | cce5df3 | 2014-07-05 13:44:00 -0700 | [diff] [blame] | 188 | install_nvm_as_script |
Koen Punt | 2d0c025 | 2014-03-26 09:16:26 +0100 | [diff] [blame] | 189 | fi |
Koen Punt | 3c4bf80 | 2012-10-30 00:03:01 +0100 | [diff] [blame] | 190 | |
Antti Vähäkotamäki | 81d731d | 2013-07-26 14:58:47 +0300 | [diff] [blame] | 191 | echo |
Koen Punt | 3c4bf80 | 2012-10-30 00:03:01 +0100 | [diff] [blame] | 192 | |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 193 | local NVM_PROFILE |
| 194 | NVM_PROFILE=$(nvm_detect_profile) |
Xavier Cambar | 3cdec8e | 2014-10-22 19:43:39 +0200 | [diff] [blame] | 195 | |
| 196 | SOURCE_STR="\nexport NVM_DIR=\"$NVM_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\" # This loads nvm" |
| 197 | |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 198 | if [ -z "$NVM_PROFILE" ] ; then |
| 199 | echo "=> Profile not found. Tried $NVM_PROFILE (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile." |
| 200 | echo "=> Create one of them and run this script again" |
| 201 | echo "=> Create it (touch $NVM_PROFILE) and run this script again" |
Xavier Cambar | 3cdec8e | 2014-10-22 19:43:39 +0200 | [diff] [blame] | 202 | echo " OR" |
| 203 | echo "=> Append the following lines to the correct file yourself:" |
| 204 | printf "$SOURCE_STR" |
| 205 | echo |
| 206 | else |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 207 | if ! grep -qc 'nvm.sh' "$NVM_PROFILE"; then |
| 208 | echo "=> Appending source string to $NVM_PROFILE" |
| 209 | printf "$SOURCE_STR\n" >> "$NVM_PROFILE" |
Xavier Cambar | 3cdec8e | 2014-10-22 19:43:39 +0200 | [diff] [blame] | 210 | else |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 211 | echo "=> Source string already in $NVM_PROFILE" |
Xavier Cambar | 3cdec8e | 2014-10-22 19:43:39 +0200 | [diff] [blame] | 212 | fi |
| 213 | fi |
| 214 | |
elliottcable | 4ba7ee5 | 2015-01-23 20:38:50 -0600 | [diff] [blame] | 215 | nvm_check_global_modules |
| 216 | |
Xavier Cambar | 3cdec8e | 2014-10-22 19:43:39 +0200 | [diff] [blame] | 217 | echo "=> Close and reopen your terminal to start using nvm" |
| 218 | nvm_reset |
| 219 | } |
| 220 | |
| 221 | # |
| 222 | # Unsets the various functions defined |
| 223 | # during the execution of the install script |
| 224 | # |
| 225 | nvm_reset() { |
elliottcable | dd1a9ca | 2015-01-29 21:23:16 -0600 | [diff] [blame] | 226 | unset -f nvm_reset nvm_has nvm_latest_version \ |
| 227 | nvm_source nvm_download install_nvm_as_script install_nvm_from_git \ |
| 228 | nvm_detect_profile nvm_check_global_modules nvm_do_install |
Xavier Cambar | 3cdec8e | 2014-10-22 19:43:39 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | [ "_$NVM_ENV" = "_testing" ] || nvm_do_install |