From c4dbc3c1999b669347fdcdd6473cbbc0bf2151a5 Mon Sep 17 00:00:00 2001 From: Jim Ray Date: Thu, 27 Oct 2016 06:28:50 -0600 Subject: [PATCH] add first pass at adding NVM to modules --- modules/nvm/README.md | 29 +++++++++++++++++++++++++++++ modules/nvm/init.zsh | 22 ++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 modules/nvm/README.md create mode 100644 modules/nvm/init.zsh 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 +}