Last active
August 20, 2022 20:15
-
-
Save ryanpback/1d0b2808f896398eab6106f49b3e99ab to your computer and use it in GitHub Desktop.
Revisions
-
ryanpback revised this gist
Jul 1, 2022 . 1 changed file with 1 addition and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,10 +32,8 @@ preexec() { local current_version=$(nvm version) local current_clean_version=${current_version:gs/v/} if [[ "$clean_version" != "$current_clean_version" ]]; then nvm use $clean_version || nvm install $clean_version fi else nvm use default -
ryanpback revised this gist
Jul 1, 2022 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,11 +3,11 @@ # This version will get your current working directory and traverse each directory looking for a `.nvmrc` file until it reaches your current directory. # Once it finds the closest directory with a `.nvmrc` file, it will store that version in a variable `$NVM_VERSION_TO_USE`. # Next is to hook into the preexec hook. If the command you're running starts with yarn, npm, or node (could maybe check others as well - any command that would invoke node), # it will then set the node version using NVM. # Running a yarn command has its own delay (install, test, start, etc.) so the small lag before the yarn command is negligable. # It will also only set the node version once so long as you haven't switched directories, so subsequent invoking of yarn is quick. # To run this, add the below script to your .zshrc file or clone this and source it from your .zshrc file. Then: -
ryanpback revised this gist
Jul 1, 2022 . 1 changed file with 11 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,6 +9,17 @@ # Running a yarn command has its own delay (install, test, start, etc.) so the small lag before the yarn command is negligable. # It will also only set the node version once so long as you haven't switched directories, so subsequent envoking of yarn is quick. # To run this, add the below script to your .zshrc file or clone this and source it from your .zshrc file. Then: # Add the following in your .zshrc file # anywhere below nvm is loaded # autoload -U add-zsh-hook # add-zsh-hook chpwd switch-node # switch-node ---------------------------------------------------------------------------- # Prior to one of the following commands being executed, # instantiate which node version to use based on switch_node @@ -63,9 +74,3 @@ switch-node() { export NVM_VERSION_TO_USE="$(cat $closest/.nvmrc)" fi } -
ryanpback created this gist
Jul 1, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,71 @@ # This is how I've managed to make this work with the least amount of lag time. # load-nvm from NVM checks the node version every time you cd into another directory which causes noticeable delays. # This version will get your current working directory and traverse each directory looking for a `.nvmrc` file until it reaches your current directory. # Once it finds the closest directory with a `.nvmrc` file, it will store that version in a variable `$NVM_VERSION_TO_USE`. # Next is to hook into the preexec hook. If the command you're running starts with yarn, npm, or node (could maybe check others as well - any command that would envoke node), # it will then set the node version using NVM. # Running a yarn command has its own delay (install, test, start, etc.) so the small lag before the yarn command is negligable. # It will also only set the node version once so long as you haven't switched directories, so subsequent envoking of yarn is quick. # Prior to one of the following commands being executed, # instantiate which node version to use based on switch_node preexec() { if [[ "$1" =~ "^yarn" || "$1" =~ "^npm" || "$1" =~ "^node" ]]; then if [[ $NVM_VERSION_TO_USE != "empty" ]]; then # remove the 'v' from `cat`ting the .nvmrc file. # Sometimes it exists, other times it doesn't local clean_version=${NVM_VERSION_TO_USE:gs/v/} local current_version=$(nvm version) local current_clean_version=${current_version:gs/v/} # Probably need to handle what happens if I don't # have the correct node version installed if [[ "$clean_version" != "$current_clean_version" ]]; then nvm use $clean_version fi else nvm use default fi fi } # Traverse directories to find closest node # version and store as variable for later. switch-node() { local pwd="$PWD" local pwd_list=("${(@s:/:)pwd}") local closest="" local buildPath="" # Could be improved by traversing up rather than down. for ((i = 2; i <= $#pwd_list; i++)); do local pathname=${pwd_list[i]} if [[ "$i" == "2" ]]; then buildPath="/$pathname" else buildPath="$buildPath/$pathname" fi if [ -f "${buildPath}/.nvmrc" ]; then closest=$buildPath fi done # Set variable for later use. Don't switch # to version unless node is needed if [[ $closest == "" ]]; then export NVM_VERSION_TO_USE="empty" else export NVM_VERSION_TO_USE="$(cat $closest/.nvmrc)" fi } # Add the following in your .zshrc file # anywhere below nvm is loaded # autoload -U add-zsh-hook # add-zsh-hook chpwd switch-node # switch-node