From a05f8c0358a4f8207e210250fc6db43bc17d8b56 Mon Sep 17 00:00:00 2001 From: Mike Milkman Date: Tue, 19 Apr 2016 15:56:45 +0300 Subject: [PATCH 1/6] Add info how to enable version auto switching using .bashrc file --- README.markdown | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/README.markdown b/README.markdown index 42b1558..d654889 100644 --- a/README.markdown +++ b/README.markdown @@ -192,6 +192,36 @@ load-nvmrc() { } add-zsh-hook chpwd load-nvmrc ``` +##### Automatic version switching for `.bashrc` with fallback to `nvm use default` + +Put this into your `$HOME/.bashrc` to enable automatic version swithing whenever you enter a directory than contains an +`.node-version` file with a string telling nvm which node to `use`: + +```bash +cd() { + builtin cd "$@" + if [[ -f .node-version && -r .node-version ]]; then + nvm use "`cat .node-version`" + elif [[ `nvm current` != `nvm version default` ]]; then + nvm use default + fi +} +``` + +Note for [`rvm`](https://github.com/rvm/rvm) users: rvm overrides builtin `cd` function so code should be a bit +different to make `rvm` and `nvm` work both without conflicts and you should place it after `rvm` sourcing its scripts. +For example, on Ubuntu it should be placed into `$HOME/.bash_profil`: + +```bash +cd() { + __zsh_like_cd cd "$@" + if [[ -f .node-version && -r .node-version ]]; then + nvm use "`cat .node-version`" + elif [[ `nvm current` != `nvm version default` ]]; then + nvm use default + fi +} +``` ## License From ce9e502964188297732889a9f75b7550f8a4d1da Mon Sep 17 00:00:00 2001 From: Mike Milkman Date: Tue, 19 Apr 2016 15:59:41 +0300 Subject: [PATCH 2/6] Fix .bash_profile file name --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index d654889..019be79 100644 --- a/README.markdown +++ b/README.markdown @@ -210,7 +210,7 @@ cd() { Note for [`rvm`](https://github.com/rvm/rvm) users: rvm overrides builtin `cd` function so code should be a bit different to make `rvm` and `nvm` work both without conflicts and you should place it after `rvm` sourcing its scripts. -For example, on Ubuntu it should be placed into `$HOME/.bash_profil`: +For example, on Ubuntu it should be placed into `$HOME/.bash_profile`: ```bash cd() { From 6dbf497000b050958b303ec4b9e5e44a13405afd Mon Sep 17 00:00:00 2001 From: Mike Milkman Date: Tue, 19 Apr 2016 16:41:59 +0300 Subject: [PATCH 3/6] Update version auto switching for .bashrc to use proper version on opening new terminal tab --- README.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.markdown b/README.markdown index 019be79..9cff97c 100644 --- a/README.markdown +++ b/README.markdown @@ -206,6 +206,7 @@ cd() { nvm use default fi } +cd . ``` Note for [`rvm`](https://github.com/rvm/rvm) users: rvm overrides builtin `cd` function so code should be a bit @@ -221,6 +222,7 @@ cd() { nvm use default fi } +cd . ``` ## License From ac7e0a8dee44cd9ce3657dca1c8b37c80ae8b3d0 Mon Sep 17 00:00:00 2001 From: Mike Milkman Date: Thu, 21 Apr 2016 21:40:19 +0300 Subject: [PATCH 4/6] Use .nvmrc file to to switch node version --- README.markdown | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.markdown b/README.markdown index 9cff97c..0b58ded 100644 --- a/README.markdown +++ b/README.markdown @@ -195,13 +195,13 @@ add-zsh-hook chpwd load-nvmrc ##### Automatic version switching for `.bashrc` with fallback to `nvm use default` Put this into your `$HOME/.bashrc` to enable automatic version swithing whenever you enter a directory than contains an -`.node-version` file with a string telling nvm which node to `use`: +`.nvmrc` file with a string telling nvm which node to `use`: ```bash cd() { builtin cd "$@" - if [[ -f .node-version && -r .node-version ]]; then - nvm use "`cat .node-version`" + if [[ -f .nvmrc && -r .nvmrc ]]; then + nvm use elif [[ `nvm current` != `nvm version default` ]]; then nvm use default fi @@ -216,8 +216,8 @@ For example, on Ubuntu it should be placed into `$HOME/.bash_profile`: ```bash cd() { __zsh_like_cd cd "$@" - if [[ -f .node-version && -r .node-version ]]; then - nvm use "`cat .node-version`" + if [[ -f .nvmrc && -r .nvmrc ]]; then + nvm use elif [[ `nvm current` != `nvm version default` ]]; then nvm use default fi From e5bd2787ac25c324ba7aaf601763323f5cded1b1 Mon Sep 17 00:00:00 2001 From: Mike Milkman Date: Thu, 21 Apr 2016 21:46:39 +0300 Subject: [PATCH 5/6] Add more details on how to auto switch node version for nvm users which also use rvm --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 0b58ded..55c73f0 100644 --- a/README.markdown +++ b/README.markdown @@ -211,7 +211,7 @@ cd . Note for [`rvm`](https://github.com/rvm/rvm) users: rvm overrides builtin `cd` function so code should be a bit different to make `rvm` and `nvm` work both without conflicts and you should place it after `rvm` sourcing its scripts. -For example, on Ubuntu it should be placed into `$HOME/.bash_profile`: +For example, on Ubuntu it should be placed at the very bottom of `$HOME/.bash_profile`: ```bash cd() { From 9773734707dd63c2a1de04bcbbd4f26da2c80ecc Mon Sep 17 00:00:00 2001 From: Mike Milkman Date: Fri, 22 Apr 2016 15:42:03 +0300 Subject: [PATCH 6/6] Add --silent option to version auto switch commands --- README.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.markdown b/README.markdown index 55c73f0..35a324f 100644 --- a/README.markdown +++ b/README.markdown @@ -201,9 +201,9 @@ Put this into your `$HOME/.bashrc` to enable automatic version swithing whenever cd() { builtin cd "$@" if [[ -f .nvmrc && -r .nvmrc ]]; then - nvm use + nvm use --silent elif [[ `nvm current` != `nvm version default` ]]; then - nvm use default + nvm use --silent default fi } cd . @@ -217,9 +217,9 @@ For example, on Ubuntu it should be placed at the very bottom of `$HOME/.bash_pr cd() { __zsh_like_cd cd "$@" if [[ -f .nvmrc && -r .nvmrc ]]; then - nvm use + nvm use --silent elif [[ `nvm current` != `nvm version default` ]]; then - nvm use default + nvm use --silent default fi } cd .