diff --git a/modules/nvm/README.md b/modules/nvm/README.md new file mode 100644 index 00000000..6f262d48 --- /dev/null +++ b/modules/nvm/README.md @@ -0,0 +1,29 @@ +NVM +==== + +Configures [Node][1] local gem installation, loads version managers, and defines +aliases. + +NVM +--- + +[The Node Version Manager (NVM)][2], allows for managing multiple, isolated +Node installations and node_modules sets in the home directory. + +#### Auto-Switch + +To enable auto switching the Node version on directory change based on the +.nvmrc file, add the following line to *zpreztorc*: + + zstyle ':prezto:module:node:nvm' auto-switch 'yes' + + +Authors +------- + +*The authors of this module should be contacted via the [issue tracker][5].* + + - [Jim Ray](https://github.com/jimiray) + +[1]: https://nodejs.org/en/ +[2]: https://github.com/creationix/nvm diff --git a/modules/nvm/init.zsh b/modules/nvm/init.zsh new file mode 100644 index 00000000..8261a8f8 --- /dev/null +++ b/modules/nvm/init.zsh @@ -0,0 +1,22 @@ +### NVM +export NVM_DIR="$HOME/.nvm" +. "/usr/local/opt/nvm/nvm.sh" + +autoload -U add-zsh-hook + +add-zsh-hook chpwd load-nvmrc +load-nvmrc + +load-nvmrc() { + local node_version="$(nvm version)" + local nvmrc_path="$(nvm_find_nvmrc)" + if [ -n "$nvmrc_path" ]; then + local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")") + if [ "$nvmrc_node_version" != "N/A" ] && [ "$nvmrc_node_version" != "$node_version" ]; then + nvm use + fi + elif [ "$node_version" != "$(nvm version default)" ]; then + echo "Reverting to nvm default version" + nvm use default + fi +}