Skip to content

Commit

Permalink
feat: add function to auto read and apply .nvmrc file with fnm
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinocossar committed Feb 6, 2023
1 parent 9393575 commit 0012ae0
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions oh-my-custom-zsh.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,36 @@ theme() {
fi
echo "Theme folder doesn't exist!"
}

# Auto read and apply .nvmrc with fnm
applynvmrc() {
if [ ! -f ".nvmrc" ]; then
return
fi

local node_version="$(node --version | grep -o '[^-]*$')"
local nvmrc_node_version="$(cat .nvmrc | tr -d " \t\n\r" )"

if [ ! -n $nvmrc_node_version ]; then
return
fi

case "$(tr -dc '.' <<<"$nvmrc_node_version" | awk '{ print length; }')" in
1)
if [ "$(echo $node_version | cut -d. -f1).$(echo $node_version | cut -d. -f2)" != $nvmrc_node_version ]; then
fnm use $nvmrc_node_version
fi
;;
2)
if [ $node_version != $nvmrc_node_version ]; then
fnm use $nvmrc_node_version
fi
;;
*)
if [ "$(echo $node_version | cut -d. -f1)" != $nvmrc_node_version ]; then
fnm use $nvmrc_node_version
fi
;;
esac
}
chpwd_functions=(${chpwd_functions[@]} "applynvmrc")

0 comments on commit 0012ae0

Please sign in to comment.