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() { |
| 128 | local NPM_GLOBAL_MODULES |
| 129 | NPM_GLOBAL_MODULES=$(npm list -g --depth=0 | sed '/ npm@/d') |
| 130 | |
| 131 | local MODULE_COUNT |
| 132 | MODULE_COUNT=$( |
| 133 | printf %s\\n "$NPM_GLOBAL_MODULES" | |
| 134 | sed -ne '1!p' | # Remove the first line |
| 135 | wc -l | tr -d ' ' # Count entries |
| 136 | ) |
| 137 | |
| 138 | if [ $MODULE_COUNT -ne 0 ]; then |
| 139 | cat <<-'END_MESSAGE' |
| 140 | => You currently have modules installed globally with `npm`. These will no |
| 141 | => longer be linked to the active version of Node when you install a new node |
| 142 | => with `nvm`; and they may (depending on how you construct your `$PATH`) |
| 143 | => override the binaries of modules installed with `nvm`: |
| 144 | |
| 145 | END_MESSAGE |
| 146 | printf %s\\n "$NPM_GLOBAL_MODULES" |
| 147 | cat <<-'END_MESSAGE' |
| 148 | |
| 149 | => If you wish to uninstall them at a later point (or re-install them under your |
| 150 | => `nvm` Nodes), you can remove them from the system Node as follows: |
| 151 | |
| 152 | $ nvm use system |
| 153 | $ npm uninstall -g a_module |
| 154 | |
| 155 | END_MESSAGE |
| 156 | fi |
| 157 | } |
| 158 | |
Xavier Cambar | 3cdec8e | 2014-10-22 19:43:39 +0200 | [diff] [blame] | 159 | nvm_do_install() { |
| 160 | if [ -z "$METHOD" ]; then |
| 161 | # Autodetect install method |
| 162 | if nvm_has "git"; then |
| 163 | install_nvm_from_git |
| 164 | elif nvm_has "nvm_download"; then |
| 165 | install_nvm_as_script |
| 166 | else |
| 167 | echo >&2 "You need git, curl, or wget to install nvm" |
| 168 | exit 1 |
| 169 | fi |
| 170 | elif [ "~$METHOD" = "~git" ]; then |
| 171 | if ! nvm_has "git"; then |
| 172 | echo >&2 "You need git to install nvm" |
| 173 | exit 1 |
| 174 | fi |
Jordan Harband | cce5df3 | 2014-07-05 13:44:00 -0700 | [diff] [blame] | 175 | install_nvm_from_git |
Xavier Cambar | 3cdec8e | 2014-10-22 19:43:39 +0200 | [diff] [blame] | 176 | elif [ "~$METHOD" = "~script" ]; then |
| 177 | if ! nvm_has "nvm_download"; then |
| 178 | echo >&2 "You need curl or wget to install nvm" |
| 179 | exit 1 |
| 180 | fi |
Jordan Harband | cce5df3 | 2014-07-05 13:44:00 -0700 | [diff] [blame] | 181 | install_nvm_as_script |
Koen Punt | 2d0c025 | 2014-03-26 09:16:26 +0100 | [diff] [blame] | 182 | fi |
Koen Punt | 3c4bf80 | 2012-10-30 00:03:01 +0100 | [diff] [blame] | 183 | |
Antti Vähäkotamäki | 81d731d | 2013-07-26 14:58:47 +0300 | [diff] [blame] | 184 | echo |
Koen Punt | 3c4bf80 | 2012-10-30 00:03:01 +0100 | [diff] [blame] | 185 | |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 186 | local NVM_PROFILE |
| 187 | NVM_PROFILE=$(nvm_detect_profile) |
Xavier Cambar | 3cdec8e | 2014-10-22 19:43:39 +0200 | [diff] [blame] | 188 | |
| 189 | SOURCE_STR="\nexport NVM_DIR=\"$NVM_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\" # This loads nvm" |
| 190 | |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 191 | if [ -z "$NVM_PROFILE" ] ; then |
| 192 | echo "=> Profile not found. Tried $NVM_PROFILE (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile." |
| 193 | echo "=> Create one of them and run this script again" |
| 194 | echo "=> Create it (touch $NVM_PROFILE) and run this script again" |
Xavier Cambar | 3cdec8e | 2014-10-22 19:43:39 +0200 | [diff] [blame] | 195 | echo " OR" |
| 196 | echo "=> Append the following lines to the correct file yourself:" |
| 197 | printf "$SOURCE_STR" |
| 198 | echo |
| 199 | else |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 200 | if ! grep -qc 'nvm.sh' "$NVM_PROFILE"; then |
| 201 | echo "=> Appending source string to $NVM_PROFILE" |
| 202 | printf "$SOURCE_STR\n" >> "$NVM_PROFILE" |
Xavier Cambar | 3cdec8e | 2014-10-22 19:43:39 +0200 | [diff] [blame] | 203 | else |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 204 | echo "=> Source string already in $NVM_PROFILE" |
Xavier Cambar | 3cdec8e | 2014-10-22 19:43:39 +0200 | [diff] [blame] | 205 | fi |
| 206 | fi |
| 207 | |
elliottcable | 4ba7ee5 | 2015-01-23 20:38:50 -0600 | [diff] [blame] | 208 | nvm_check_global_modules |
| 209 | |
Xavier Cambar | 3cdec8e | 2014-10-22 19:43:39 +0200 | [diff] [blame] | 210 | echo "=> Close and reopen your terminal to start using nvm" |
| 211 | nvm_reset |
| 212 | } |
| 213 | |
| 214 | # |
| 215 | # Unsets the various functions defined |
| 216 | # during the execution of the install script |
| 217 | # |
| 218 | nvm_reset() { |
elliottcable | dd1a9ca | 2015-01-29 21:23:16 -0600 | [diff] [blame^] | 219 | unset -f nvm_reset nvm_has nvm_latest_version \ |
| 220 | nvm_source nvm_download install_nvm_as_script install_nvm_from_git \ |
| 221 | nvm_detect_profile nvm_check_global_modules nvm_do_install |
Xavier Cambar | 3cdec8e | 2014-10-22 19:43:39 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | [ "_$NVM_ENV" = "_testing" ] || nvm_do_install |