Add Bash script for auto-nvm use to README.

This commit is contained in:
William Bert 2016-04-09 14:17:06 -04:00
parent d3e334d86d
commit e5273acf0a
1 changed files with 20 additions and 1 deletions

View File

@ -193,6 +193,25 @@ load-nvmrc() {
add-zsh-hook chpwd load-nvmrc add-zsh-hook chpwd load-nvmrc
``` ```
#### Bash
##### Calling `nvm use` automatically in a directory with a `.nvmrc` file
Put this into your `$HOME/.bashrc` to call `nvm use` automatically whenever you enter a directory that contains an
`.nvmrc` file with a string telling nvm which node to `use`:
```bash
function nvm_use_if_needed () {
[[ -r ./.nvmrc && -s ./.nvmrc ]] || return
WANTED=$(sed 's/v//' < ./.nvmrc)
CURRENT=$(hash node 2>/dev/null && node -v | sed 's/v//')
if [ "$WANTED" != "$CURRENT" ]; then
nvm use
fi
}
export PROMPT_COMMAND="$PROMPT_COMMAND ; nvm_use_if_needed"
```
## License ## License
nvm is released under the MIT license. nvm is released under the MIT license.