This is a little ZSH function that hooks change cwd and checks for our ./deployment.json it then uses nvm to change the current node version to the one required by the project.
- jq
- nvm
function set_node_version() {
if [ -e ./deployment.json ]
then
desired_node_ver=`cat ./deployment.json | jq --raw-output '.setup.nodejs.version'`
current_node_ver=`nvm current`
if [ -n $desired_node_ver -a $desired_node_ver != $current_node_ver ]
then
nvm ls | grep -q $desired_node_ver
if [ $? -ne 0 ]; then
nvm install $desired_node_ver
else
nvm use $desired_node_ver
fi
fi
fi
}
chpwd_functions=(${chpwd_functions[@]} "set_node_version")
I did change the
nvm install
line to below just to speed it up if no install is needed