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

Commit 45b8fb7

Browse files
add node_version only inside project folder option
Co-authored-by: Thore Weilbier <[email protected]>
1 parent 00b6da0 commit 45b8fb7

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

segments/node_version/node_version.p9k

+27-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,33 @@
2424
# $2 integer Segment index
2525
# $3 boolean Whether the segment should be joined
2626
##
27+
p9k::set_default P9K_NODE_VERSION_PROJECT_ONLY=false
2728
prompt_node_version() {
28-
local node_version=$(node -v 2>/dev/null)
29-
[[ -z "${node_version}" ]] && return
29+
if [ "$P9K_NODE_VERSION_PROJECT_ONLY" = true ] ; then
30+
local foundProject=false # Variable to stop searching if a project is found
31+
local currentDir=$(pwd) # Variable to iterate through the path ancestry tree
3032

31-
p9k::prepare_segment "$0" "" $1 "$2" $3 "${node_version:1}"
33+
# Search as long as no project could been found or until the root directory
34+
# has been reached
35+
while [ "$foundProject" = false -a ! "$currentDir" = "/" ] ; do
36+
# Check if directory contains a project description
37+
if [[ -e "$currentDir/package.json" ]] ; then
38+
foundProject=true
39+
break
40+
fi
41+
# Go to the parent directory
42+
currentDir="$(dirname "$currentDir")"
43+
done
44+
fi
45+
46+
# Show version if a project has been found, or set to always show
47+
if [ "$P9K_NODE_VERSION_PROJECT_ONLY" != true -o "$foundProject" = true ] ; then
48+
# Get the node version
49+
local node_version=$(node -v 2>/dev/null)
50+
51+
# Return if node is not installed
52+
[[ -z "${node_version}" ]] && return
53+
54+
p9k::prepare_segment "$0" "" $1 "$2" $3 "${node_version:1}"
55+
fi
3256
}

0 commit comments

Comments
 (0)