`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
pull/1946/head
Konstantin Gredeskoul 2021-09-13 00:11:16 -07:00
parent 99eab7a76c
commit b935ddd309
No known key found for this signature in database
GPG Key ID: F64DECB748F527EB
1 changed files with 21 additions and 4 deletions

View File

@ -386,6 +386,27 @@ function hg_prompt_vars {
fi
}
function node_command_version_prompt {
local node_version
local node_command="$(command -v node)"
if [[ -n "${node_command}" ]]; then
node_version="$(${node_command} --version 2>/dev/null)"
if [[ -n ${node_version} ]]; then
echo -e "${NVM_THEME_PROMPT_PREFIX}${node_version}${NVM_THEME_PROMPT_SUFFIX}"
fi
fi
}
function node_version_prompt {
local node_version="$(nvm_version_prompt)"
if [[ -z "${node_version}" ]]; then
node_version="$(node_command_version_prompt)"
fi
if [[ -n "${node_version}" ]] ; then
echo -e "${node_version}"
fi
}
function nvm_version_prompt {
local node
if declare -f -F nvm &> /dev/null; then
@ -395,10 +416,6 @@ function nvm_version_prompt {
fi
}
function node_version_prompt {
echo -e "$(nvm_version_prompt)"
}
function rvm_version_prompt {
if which rvm &> /dev/null; then
rvm=$(rvm-prompt) || return