Skip to content

Instantly share code, notes, and snippets.

@tjstebbing
Last active October 4, 2016 03:06
Show Gist options
  • Save tjstebbing/4af2df1e45980baceba4d58a685a0206 to your computer and use it in GitHub Desktop.
Save tjstebbing/4af2df1e45980baceba4d58a685a0206 to your computer and use it in GitHub Desktop.

Change node version for current project.

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.

Install deps

  • jq
  • nvm

Add this to .zshrc

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")
@patowens
Copy link

patowens commented Oct 3, 2016

💯

@joshgillies
Copy link

💯

@timmyw
Copy link

timmyw commented Oct 3, 2016

Needs jq installed - but works great

@timmyw
Copy link

timmyw commented Oct 4, 2016

I did change the nvm install line to below just to speed it up if no install is needed

        nvm ls | grep -q $desired_node_ver
        if [ $? -ne 0 ]; then
            nvm install $desired_node_ver
        else
            nvm use $desired_node_ver
        fi

@tjstebbing
Copy link
Author

Nice @timmyw, updated the example to include your changes :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment