Skip to content

Commit 20bbb09

Browse files
committed
Improve Bash cdnvm alias
- Prevent unnecessary running of `nvm use`
1 parent 8471e27 commit 20bbb09

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

Diff for: README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -457,17 +457,18 @@ cdnvm(){
457457
declare nvm_version
458458
nvm_version=$(<"$nvm_path"/.nvmrc)
459459

460-
# Add the `v` suffix if it does not exists in the .nvmrc file
461-
if [[ $nvm_version != v* && $nvm_version != lts/* && $nvm_version != iojs* && $nvm_version != node && $nvm_version != system && $nvm_version != stable && $nvm_version != default ]]; then
462-
nvm_version="v""$nvm_version"
463-
fi
460+
declare locally_resolved_nvm_version
461+
# `nvm ls` will check all locally-available versions
462+
# If there are multiple matching versions, take the latest one
463+
# Remove the `->` and `*` characters and spaces
464+
# `locally_resolved_nvm_version` will be `N/A` if no local versions are found
465+
locally_resolved_nvm_version=$(nvm ls --no-colors $(<"./.nvmrc") | tail -1 | tr -d '\->*' | tr -d '[:space:]')
464466

465467
# If it is not already installed, install it
466-
if [[ $(nvm ls "$nvm_version" | tr -d '[:space:]') == "N/A" ]]; then
468+
# `nvm install` will implicitly use the newly-installed version
469+
if [[ "$locally_resolved_nvm_version" == "N/A" ]]; then
467470
nvm install "$nvm_version";
468-
fi
469-
470-
if [[ $(nvm current) != "$nvm_version" ]]; then
471+
elif [[ $(nvm current) != "$locally_resolved_nvm_version" ]]; then
471472
nvm use "$nvm_version";
472473
fi
473474
fi

0 commit comments

Comments
 (0)