From c53f82fcf4a9ca00f7d6bb1d12ea614587f92278 Mon Sep 17 00:00:00 2001 From: Ira Abramov Date: Tue, 7 Oct 2025 13:39:03 +0300 Subject: [PATCH 1/2] Add NODE_VERSION_CHECK_PROJECT option to conditionally display node version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds the ability to only show node version in the prompt when inside a Node.js project (detected by presence of package.json). **Problem:** The node version was always displayed in the prompt regardless of the current directory context. This wastes space when working on non-Node.js projects (Python, Ansible, etc.). **Solution:** - New environment variable: `NODE_VERSION_CHECK_PROJECT` (default: false) - When set to 'true', node version only shows in directories with package.json - Searches current directory and parent directories up to $HOME - Applies to all node version display strategies (nvm, node, command) **Usage:** ```bash # Add to ~/.bashrc or ~/.bash_profile before sourcing bash-it export NODE_VERSION_CHECK_PROJECT=true ``` **Implementation:** - New helper function `_is_node_project()` checks for package.json - Modified `node_version_prompt()` to check NODE_VERSION_CHECK_PROJECT - Modified `node_command_version_prompt()` for consistency - Backwards compatible (disabled by default) **Testing:** - Passes shellcheck with no warnings - Passes shfmt formatting checks - Maintains backward compatibility Closes #2216 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- themes/base.theme.bash | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 7232d15890..b8ee284720 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -395,6 +395,14 @@ function hg_prompt_vars() { } function node_command_version_prompt() { + # Set to 'true' to only show node version in directories with package.json + NODE_VERSION_CHECK_PROJECT="${NODE_VERSION_CHECK_PROJECT:-false}" + + # If NODE_VERSION_CHECK_PROJECT is enabled, only show version in Node.js projects + if [[ "${NODE_VERSION_CHECK_PROJECT}" == "true" ]] && ! _is_node_project; then + return + fi + local node_version if _command_exists node; then node_version="$(node --version 2> /dev/null)" @@ -421,11 +429,33 @@ function node_native_version_prompt() { fi } +function _is_node_project() { + # Check if we're in a Node.js project by looking for package.json + # Search current directory and parent directories up to $HOME + local dir="${PWD}" + while [[ "${dir}" != "${HOME-}" && "${dir}" != "/" ]]; do + if [[ -f "${dir}/package.json" ]]; then + return 0 + fi + dir=$(dirname "${dir}") + done + # Also check $HOME itself + [[ -f "${HOME-}/package.json" ]] && return 0 + return 1 +} + function node_version_prompt() { NODE_VERSION_STRATEGY="${NODE_VERSION_STRATEGY:-nvm}" + # Set to 'true' to only show node version in directories with package.json + NODE_VERSION_CHECK_PROJECT="${NODE_VERSION_CHECK_PROJECT:-false}" _log_debug "node: using version strategy '$NODE_VERSION_STRATEGY'" + # If NODE_VERSION_CHECK_PROJECT is enabled, only show version in Node.js projects + if [[ "${NODE_VERSION_CHECK_PROJECT}" == "true" ]] && ! _is_node_project; then + return + fi + if [ "$NODE_VERSION_STRATEGY" == "nvm" ]; then nvm_version_prompt elif [ "$NODE_VERSION_STRATEGY" == "node" ]; then From 0008d77ff0e1bfcdc3fefc9934e58d248e11071a Mon Sep 17 00:00:00 2001 From: Ira Abramov Date: Tue, 7 Oct 2025 16:04:49 +0300 Subject: [PATCH 2/2] Apply review feedback: add explicit return 0 Add explicit return code 0 to all return statements as suggested by @akinomyoga to avoid strange behaviors in trap handlers. Co-Authored-By: akinomyoga --- themes/base.theme.bash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index b8ee284720..47e09b24d2 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -400,7 +400,7 @@ function node_command_version_prompt() { # If NODE_VERSION_CHECK_PROJECT is enabled, only show version in Node.js projects if [[ "${NODE_VERSION_CHECK_PROJECT}" == "true" ]] && ! _is_node_project; then - return + return 0 fi local node_version @@ -453,7 +453,7 @@ function node_version_prompt() { # If NODE_VERSION_CHECK_PROJECT is enabled, only show version in Node.js projects if [[ "${NODE_VERSION_CHECK_PROJECT}" == "true" ]] && ! _is_node_project; then - return + return 0 fi if [ "$NODE_VERSION_STRATEGY" == "nvm" ]; then