Created
August 26, 2019 09:18
-
-
Save mudge/7d45e9c664d416f47369bc806a9e3bce to your computer and use it in GitHub Desktop.
Auto-switch Node.js versions with chnode by reading .node-version files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Based off chruby's auto.sh: https://github.com/postmodern/chruby#auto-switching | |
unset NODE_AUTO_VERSION | |
function chnode_auto() { | |
local dir="$PWD/" version | |
until [[ -z "$dir" ]]; do | |
dir="${dir%/*}" | |
if { read -r version <"$dir/.node-version"; } 2>/dev/null || [[ -n "$version" ]]; then | |
if [[ "$version" == "$NODE_AUTO_VERSION" ]]; then return | |
else | |
NODE_AUTO_VERSION="$version" | |
chnode "$version" | |
return $? | |
fi | |
fi | |
done | |
if [[ -n "$NODE_AUTO_VERSION" ]]; then | |
chnode_reset | |
unset NODE_AUTO_VERSION | |
fi | |
} | |
if [[ -n "$ZSH_VERSION" ]]; then | |
if [[ ! "$preexec_functions" == *chnode_auto* ]]; then | |
preexec_functions+=("chnode_auto") | |
fi | |
elif [[ -n "$BASH_VERSION" ]]; then | |
trap '[[ "$BASH_COMMAND" != "$PROMPT_COMMAND" ]] && chnode_auto' DEBUG | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment