Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c612a65

Browse files
committedSep 13, 2021
node_version_prompt should work without NVM
Adding `node` prompt that does not depend on `nvm` and will work with other version managers as well. There are now alternative version managers available, such as a much more streamlined [volta.sh](https://volta.sh). It feels like a deja-vu of `rvm` to `rbenv` switch, all over again. Regardless, we should be able to show the current `node` version whether you are using NVM, VOLTA or a hot potato. I decided not to add dedicated PREFIX variables for now, but it can be done later. We still check if `nvm` prompt returns something first because the `declare` check is practically free, and if it returns something — we use it. Only if the output of NVM is blank do we use the new function to grab the version of NodeJS. There is a caveat — if `node` is installed with the OS, eg `/usr/bin/node` the new function will now pick up the version of that "system" node and show it. Therefore "system" node version will now be visible in the prompt of those who added `node` component to their prompt. Personally, I believe this is the correct behavior, because why should we hide the system node version if that's what's available and in the PATH? We shouldn't. In fact, I think it's rather confusing that previously we wouldn't show the system node version at all. Tested locally on OS-X/bash: * with/without NVM * with/without VOLTA * with/without system node
1 parent 99eab7a commit c612a65

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed
 

‎themes/base.theme.bash

+19-4
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,25 @@ function hg_prompt_vars {
386386
fi
387387
}
388388

389+
function node_command_version_prompt {
390+
local node_version
391+
local node_command="$(command -v node)"
392+
if [[ -n "${node_command}" && -x "${node_command}" ]]; then
393+
node_version="$(${node_command} --version)"
394+
echo -e "${NVM_THEME_PROMPT_PREFIX}${node_version}${NVM_THEME_PROMPT_SUFFIX}"
395+
fi
396+
}
397+
398+
function node_version_prompt {
399+
local node_version="$(nvm_version_prompt)"
400+
if [[ -z "${node_version}" ]]; then
401+
node_version="$(node_command_version_prompt)"
402+
fi
403+
if [[ -n "${node_version}" ]] ; then
404+
echo -e "${node_version}"
405+
fi
406+
}
407+
389408
function nvm_version_prompt {
390409
local node
391410
if declare -f -F nvm &> /dev/null; then
@@ -395,10 +414,6 @@ function nvm_version_prompt {
395414
fi
396415
}
397416

398-
function node_version_prompt {
399-
echo -e "$(nvm_version_prompt)"
400-
}
401-
402417
function rvm_version_prompt {
403418
if which rvm &> /dev/null; then
404419
rvm=$(rvm-prompt) || return

0 commit comments

Comments
 (0)
Please sign in to comment.