Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Commit

Permalink
add node_version only inside project folder option
Browse files Browse the repository at this point in the history
Co-authored-by: Thore Weilbier <thore@weilbier.net>
  • Loading branch information
ChrisBaker97 and weilbith committed Mar 18, 2019
1 parent 00b6da0 commit 45b8fb7
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions segments/node_version/node_version.p9k
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,33 @@
# $2 integer Segment index
# $3 boolean Whether the segment should be joined
##
p9k::set_default P9K_NODE_VERSION_PROJECT_ONLY=false
prompt_node_version() {
local node_version=$(node -v 2>/dev/null)
[[ -z "${node_version}" ]] && return
if [ "$P9K_NODE_VERSION_PROJECT_ONLY" = true ] ; then
local foundProject=false # Variable to stop searching if a project is found
local currentDir=$(pwd) # Variable to iterate through the path ancestry tree

p9k::prepare_segment "$0" "" $1 "$2" $3 "${node_version:1}"
# Search as long as no project could been found or until the root directory
# has been reached
while [ "$foundProject" = false -a ! "$currentDir" = "/" ] ; do
# Check if directory contains a project description
if [[ -e "$currentDir/package.json" ]] ; then
foundProject=true
break
fi
# Go to the parent directory
currentDir="$(dirname "$currentDir")"
done
fi

# Show version if a project has been found, or set to always show
if [ "$P9K_NODE_VERSION_PROJECT_ONLY" != true -o "$foundProject" = true ] ; then
# Get the node version
local node_version=$(node -v 2>/dev/null)

# Return if node is not installed
[[ -z "${node_version}" ]] && return

p9k::prepare_segment "$0" "" $1 "$2" $3 "${node_version:1}"
fi
}

0 comments on commit 45b8fb7

Please sign in to comment.