Optionally installs Node.js if a version is provided

Xavier Cambar 2014-10-13 14:05:06 +02:00
parent 39c119c5db
commit a24ff3e605
2 changed files with 39 additions and 4 deletions

View File

@ -42,8 +42,8 @@ or Wget:
<sub>The script clones the nvm repository to `~/.nvm` and adds the source line to your profile (`~/.bash_profile`, `~/.zshrc`, `~/.profile`, or `~/.bashrc`).</sub> <sub>The script clones the nvm repository to `~/.nvm` and adds the source line to your profile (`~/.bash_profile`, `~/.zshrc`, `~/.profile`, or `~/.bashrc`).</sub>
You can customize the install source, directory and profile using the `NVM_SOURCE`, `NVM_DIR`, and `PROFILE` variables. You can customize the install source, directory, profile, and version using the `NVM_SOURCE`, `NVM_DIR`, `PROFILE`, and `NODE_VERSION` variables.
Eg: `curl ... | NVM_DIR="path/to/nvm" bash` Eg: `curl ... | NVM_DIR=/usr/local/nvm bash` for a global install.
<sub>*NB. The installer can use `git`, `curl`, or `wget` to download `nvm`, whatever is available.*</sub> <sub>*NB. The installer can use `git`, `curl`, or `wget` to download `nvm`, whatever is available.*</sub>

View File

@ -40,6 +40,13 @@ nvm_source() {
echo "$NVM_SOURCE_URL" echo "$NVM_SOURCE_URL"
} }
#
# Node.js version to install
#
nvm_node_version() {
echo "$NODE_VERSION"
}
nvm_download() { nvm_download() {
if nvm_has "curl"; then if nvm_has "curl"; then
curl -q $* curl -q $*
@ -84,6 +91,30 @@ install_nvm_from_git() {
return return
} }
#
# Automatically install Node.js
#
nvm_install_node() {
local NODE_VERSION
NODE_VERSION="$(nvm_node_version)"
if [ -z "$NODE_VERSION" ]; then
echo "=> You can now install Node.js by running \`nvm install\`"
return 0
fi
echo "=> Installing Node.js version $NODE_VERSION"
nvm install "$NODE_VERSION"
local CURRENT_NVM_NODE
CURRENT_NVM_NODE="$(nvm_version current)"
if [ "$(nvm_version "$NODE_VERSION")" == "$CURRENT_NVM_NODE" ]; then
echo "=> Node.js version $NODE_VERSION has been successfully installed"
else
echo >&2 "Failed to install Node.js $NODE_VERSION"
fi
}
install_nvm_as_script() { install_nvm_as_script() {
local INSTALL_DIR local INSTALL_DIR
INSTALL_DIR="$(nvm_install_dir)" INSTALL_DIR="$(nvm_install_dir)"
@ -255,9 +286,13 @@ nvm_do_install() {
fi fi
fi fi
# Sourcing $PROFILE to take into account the new environment
. "$NVM_PROFILE"
nvm_check_global_modules nvm_check_global_modules
echo "=> Close and reopen your terminal to start using nvm" nvm_install_node
nvm_reset nvm_reset
} }
@ -269,7 +304,7 @@ nvm_reset() {
unset -f nvm_reset nvm_has nvm_latest_version \ unset -f nvm_reset nvm_has nvm_latest_version \
nvm_source nvm_download install_nvm_as_script install_nvm_from_git \ nvm_source nvm_download install_nvm_as_script install_nvm_from_git \
nvm_detect_profile nvm_check_global_modules nvm_do_install \ nvm_detect_profile nvm_check_global_modules nvm_do_install \
nvm_install_dir nvm_install_dir nvm_node_version nvm_install_node
} }
[ "_$NVM_ENV" = "_testing" ] || nvm_do_install [ "_$NVM_ENV" = "_testing" ] || nvm_do_install