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