blob: 67bb71a3d1e54732ded4f1902294828c7e3eaaa5 [file] [log] [blame]
Koen Punt3c4bf802012-10-30 00:03:01 +01001#!/bin/bash
Jordan Harbandf0d81e22015-02-09 16:56:08 -08002
Koen Punt0787a552013-12-22 17:46:26 +01003set -e
4
Jordan Harband63672642015-05-11 01:46:18 -07005{ # this ensures the entire script is downloaded #
6
Jordan Harband7e269962014-07-05 13:42:44 -07007nvm_has() {
Koen Punt30002262014-03-15 04:32:35 +01008 type "$1" > /dev/null 2>&1
Koen Punt30002262014-03-15 04:32:35 +01009}
Koen Puntd2422a62013-12-22 18:23:59 +010010
Koen Punt30002262014-03-15 04:32:35 +010011if [ -z "$NVM_DIR" ]; then
Koen Puntd2422a62013-12-22 18:23:59 +010012 NVM_DIR="$HOME/.nvm"
13fi
Koen Punt3c4bf802012-10-30 00:03:01 +010014
Jordan Harband8e45afb2014-12-28 15:57:18 -080015nvm_latest_version() {
Jordan Harbandd78722b2015-05-29 11:11:40 -070016 echo "v0.25.4"
Jordan Harband8e45afb2014-12-28 15:57:18 -080017}
18
Xavier Cambar516e5532014-10-31 13:37:59 +010019#
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 Harband3d6b7972015-02-11 10:52:41 -080023# NVM_SOURCE always takes precedence unless the method is "script-nvm-exec"
Xavier Cambar516e5532014-10-31 13:37:59 +010024#
25nvm_source() {
26 local NVM_METHOD
27 NVM_METHOD="$1"
Jordan Harband3d6b7972015-02-11 10:52:41 -080028 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 Harbandfd2fb242014-12-28 15:54:09 -080033 if [ "_$NVM_METHOD" = "_script" ]; then
Jordan Harband3d6b7972015-02-11 10:52:41 -080034 NVM_SOURCE_URL="https://raw.githubusercontent.com/creationix/nvm/$(nvm_latest_version)/nvm.sh"
Jordan Harbandfd2fb242014-12-28 15:54:09 -080035 elif [ "_$NVM_METHOD" = "_git" ] || [ -z "$NVM_METHOD" ]; then
Jordan Harband3d6b7972015-02-11 10:52:41 -080036 NVM_SOURCE_URL="https://github.com/creationix/nvm.git"
Jordan Harbandfd2fb242014-12-28 15:54:09 -080037 else
38 echo >&2 "Unexpected value \"$NVM_METHOD\" for \$NVM_METHOD"
39 return 1
40 fi
Xavier Cambar516e5532014-10-31 13:37:59 +010041 fi
Jordan Harband3d6b7972015-02-11 10:52:41 -080042 echo "$NVM_SOURCE_URL"
Xavier Cambar516e5532014-10-31 13:37:59 +010043}
44
Jordan Harband63f72b32014-07-07 15:40:59 -070045nvm_download() {
Jordan Harband74cc1eb2014-07-07 10:04:20 -070046 if nvm_has "curl"; then
Jordan Harbanda4f89c62015-05-01 02:00:49 -070047 curl -q $*
Jordan Harband74cc1eb2014-07-07 10:04:20 -070048 elif nvm_has "wget"; then
Koen Punt5342b6a2014-03-26 09:29:05 +010049 # Emulate curl with wget
Jordan Harband3fc82d62015-01-09 01:50:05 -080050 ARGS=$(echo "$*" | command sed -e 's/--progress-bar /--progress=bar /' \
Koen Punt9c2127c2014-07-18 16:18:17 +020051 -e 's/-L //' \
Jordan Harband708ac802014-08-15 20:21:46 -070052 -e 's/-I /--server-response /' \
Koen Punt9c2127c2014-07-18 16:18:17 +020053 -e 's/-s /-q /' \
54 -e 's/-o /-O /' \
55 -e 's/-C - /-c /')
Jordan Harband74cc1eb2014-07-07 10:04:20 -070056 wget $ARGS
Koen Punt30002262014-03-15 04:32:35 +010057 fi
Jordan Harband74cc1eb2014-07-07 10:04:20 -070058}
Dennis Dryden34a06762013-06-07 23:42:28 +010059
Jordan Harbandcce5df32014-07-05 13:44:00 -070060install_nvm_from_git() {
Koen Punt30002262014-03-15 04:32:35 +010061 if [ -d "$NVM_DIR/.git" ]; then
Jordan Harband2d9494a2015-01-22 10:21:04 -080062 echo "=> nvm is already installed in $NVM_DIR, trying to update using git"
Jordan Harbandb6f1c152014-06-26 10:26:57 -070063 printf "\r=> "
Jordan Harbandbf794ff2015-01-11 11:53:24 -080064 cd "$NVM_DIR" && (command git fetch 2> /dev/null || {
Jordan Harband55d892a2014-07-17 00:19:01 -070065 echo >&2 "Failed to update nvm, run 'git fetch' in $NVM_DIR yourself." && exit 1
Jordan Harbande0537ce2014-07-05 13:47:22 -070066 })
Koen Punt30002262014-03-15 04:32:35 +010067 else
68 # Cloning to $NVM_DIR
Koen Punt6ed93f42014-03-26 09:08:37 +010069 echo "=> Downloading nvm from git to '$NVM_DIR'"
Jordan Harbandb6f1c152014-06-26 10:26:57 -070070 printf "\r=> "
Koen Punt30002262014-03-15 04:32:35 +010071 mkdir -p "$NVM_DIR"
Jordan Harbandbf794ff2015-01-11 11:53:24 -080072 command git clone "$(nvm_source git)" "$NVM_DIR"
Koen Punt30002262014-03-15 04:32:35 +010073 fi
Jarrett Chisholmc16919b2015-03-05 18:21:46 -050074 cd "$NVM_DIR" && command git checkout --quiet $(nvm_latest_version)
75 if [ ! -z "$(cd "$NVM_DIR" && git show-ref refs/heads/master)" ]; then
Jordan Harband356ac7a2015-04-22 00:40:27 -070076 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 Chisholmc16919b2015-03-05 18:21:46 -050082 fi
Jordan Harband5ad00f12014-09-25 22:10:48 -070083 return
Koen Punt30002262014-03-15 04:32:35 +010084}
85
Jordan Harbandcce5df32014-07-05 13:44:00 -070086install_nvm_as_script() {
Jordan Harband3d6b7972015-02-11 10:52:41 -080087 local NVM_SOURCE_LOCAL
88 NVM_SOURCE_LOCAL=$(nvm_source script)
Jordan Harband689c52c2014-11-22 10:31:42 -080089 local NVM_EXEC_SOURCE
Jordan Harbandfd2fb242014-12-28 15:54:09 -080090 NVM_EXEC_SOURCE=$(nvm_source script-nvm-exec)
Koen Punt30002262014-03-15 04:32:35 +010091
92 # Downloading to $NVM_DIR
Koen Puntd2422a62013-12-22 18:23:59 +010093 mkdir -p "$NVM_DIR"
Koen Punt30002262014-03-15 04:32:35 +010094 if [ -d "$NVM_DIR/nvm.sh" ]; then
Jordan Harband2d9494a2015-01-22 10:21:04 -080095 echo "=> nvm is already installed in $NVM_DIR, trying to update the script"
Koen Punt30002262014-03-15 04:32:35 +010096 else
Koen Punt6ed93f42014-03-26 09:08:37 +010097 echo "=> Downloading nvm as script to '$NVM_DIR'"
Koen Punt30002262014-03-15 04:32:35 +010098 fi
Jordan Harband3d6b7972015-02-11 10:52:41 -080099 nvm_download -s "$NVM_SOURCE_LOCAL" -o "$NVM_DIR/nvm.sh" || {
100 echo >&2 "Failed to download '$NVM_SOURCE_LOCAL'"
Koen Punt30002262014-03-15 04:32:35 +0100101 return 1
102 }
Jordan Harband689c52c2014-11-22 10:31:42 -0800103 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 Punt30002262014-03-15 04:32:35 +0100111}
112
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100113#
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#
119nvm_detect_profile() {
Cristian Consonnic37dbbe2015-07-19 02:01:06 +0200120
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 Cambarb9f15b02014-10-29 13:05:11 +0100152 fi
153}
154
elliottcable4ba7ee52015-01-23 20:38:50 -0600155#
156# Check whether the user has any globally-installed npm modules in their system
157# Node, and warn them if so.
158#
159nvm_check_global_modules() {
elliottcable6cfc3092015-02-02 20:42:12 -0600160 command -v npm >/dev/null 2>&1 || return 0
161
162 local NPM_VERSION
163 NPM_VERSION="$(npm --version)"
elliottcableea426462015-02-07 21:12:46 -0600164 NPM_VERSION="${NPM_VERSION:--1}"
elliottcable4508f7c2015-02-08 02:43:05 -0600165 [ "${NPM_VERSION%%[!-0-9]*}" -gt 0 ] || return 0
elliottcable6cfc3092015-02-02 20:42:12 -0600166
elliottcable4ba7ee52015-01-23 20:38:50 -0600167 local NPM_GLOBAL_MODULES
elliottcable4508f7c2015-02-08 02:43:05 -0600168 NPM_GLOBAL_MODULES="$(
169 npm list -g --depth=0 |
170 sed '/ npm@/d' |
171 sed '/ (empty)$/d'
172 )"
elliottcable0717d5f2015-02-04 16:39:57 -0600173
elliottcable4ba7ee52015-01-23 20:38:50 -0600174 local MODULE_COUNT
elliottcable0717d5f2015-02-04 16:39:57 -0600175 MODULE_COUNT="$(
elliottcable4ba7ee52015-01-23 20:38:50 -0600176 printf %s\\n "$NPM_GLOBAL_MODULES" |
177 sed -ne '1!p' | # Remove the first line
178 wc -l | tr -d ' ' # Count entries
elliottcable0717d5f2015-02-04 16:39:57 -0600179 )"
elliottcable4ba7ee52015-01-23 20:38:50 -0600180
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 Cambar3cdec8e2014-10-22 19:43:39 +0200202nvm_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 Harbandcce5df32014-07-05 13:44:00 -0700218 install_nvm_from_git
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200219 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 Harbandcce5df32014-07-05 13:44:00 -0700224 install_nvm_as_script
Koen Punt2d0c0252014-03-26 09:16:26 +0100225 fi
Koen Punt3c4bf802012-10-30 00:03:01 +0100226
Antti Vähäkotamäki81d731d2013-07-26 14:58:47 +0300227 echo
Koen Punt3c4bf802012-10-30 00:03:01 +0100228
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100229 local NVM_PROFILE
230 NVM_PROFILE=$(nvm_detect_profile)
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200231
232 SOURCE_STR="\nexport NVM_DIR=\"$NVM_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\" # This loads nvm"
233
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100234 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 Cambar3cdec8e2014-10-22 19:43:39 +0200238 echo " OR"
239 echo "=> Append the following lines to the correct file yourself:"
240 printf "$SOURCE_STR"
241 echo
242 else
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100243 if ! grep -qc 'nvm.sh' "$NVM_PROFILE"; then
244 echo "=> Appending source string to $NVM_PROFILE"
245 printf "$SOURCE_STR\n" >> "$NVM_PROFILE"
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200246 else
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100247 echo "=> Source string already in $NVM_PROFILE"
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200248 fi
249 fi
250
elliottcable4ba7ee52015-01-23 20:38:50 -0600251 nvm_check_global_modules
Ionică Bizăub4151e12015-06-30 09:47:55 +0300252
253 . $NVM_DIR/nvm.sh
254 echo "=> You can now start using nvm"
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200255 nvm_reset
256}
257
258#
259# Unsets the various functions defined
260# during the execution of the install script
261#
262nvm_reset() {
Jordan Harbandf0d81e22015-02-09 16:56:08 -0800263 unset -f nvm_reset nvm_has nvm_latest_version \
elliottcabledd1a9ca2015-01-29 21:23:16 -0600264 nvm_source nvm_download install_nvm_as_script install_nvm_from_git \
Jordan Harband758141f2015-02-22 00:29:25 -0800265 nvm_detect_profile nvm_check_global_modules nvm_do_install
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200266}
267
268[ "_$NVM_ENV" = "_testing" ] || nvm_do_install
Jordan Harband63672642015-05-11 01:46:18 -0700269
270} # this ensures the entire script is downloaded #