[Docs] update `bash` `cdnvm` script to properly handle aliases

- Only works for common aliases like `default`, `system`, `node`, `lts/*`, `iojs` etc.
 - Prevent unnecessary running of `nvm use`
Daniel Li 2019-03-12 16:43:08 +00:00 committed by Jordan Harband
parent 2410215b6a
commit ff77ac17af
No known key found for this signature in database
GPG Key ID: 64A196AEE0916D55
1 changed files with 9 additions and 8 deletions

View File

@ -457,17 +457,18 @@ cdnvm(){
declare nvm_version declare nvm_version
nvm_version=$(<"$nvm_path"/.nvmrc) nvm_version=$(<"$nvm_path"/.nvmrc)
# Add the `v` suffix if it does not exists in the .nvmrc file declare locally_resolved_nvm_version
if [[ $nvm_version != v* ]]; then # `nvm ls` will check all locally-available versions
nvm_version="v""$nvm_version" # If there are multiple matching versions, take the latest one
fi # Remove the `->` and `*` characters and spaces
# `locally_resolved_nvm_version` will be `N/A` if no local versions are found
locally_resolved_nvm_version=$(nvm ls --no-colors $(<"./.nvmrc") | tail -1 | tr -d '\->*' | tr -d '[:space:]')
# If it is not already installed, install it # If it is not already installed, install it
if [[ $(nvm ls "$nvm_version" | tr -d '[:space:]') == "N/A" ]]; then # `nvm install` will implicitly use the newly-installed version
if [[ "$locally_resolved_nvm_version" == "N/A" ]]; then
nvm install "$nvm_version"; nvm install "$nvm_version";
fi elif [[ $(nvm current) != "$locally_resolved_nvm_version" ]]; then
if [[ $(nvm current) != "$nvm_version" ]]; then
nvm use "$nvm_version"; nvm use "$nvm_version";
fi fi
fi fi