Skip to content

Commit

Permalink
Output entirely new PATH with utility
Browse files Browse the repository at this point in the history
  • Loading branch information
abejfehr committed Feb 17, 2022
1 parent 9ee5b5b commit 09f3178
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function nvm() {
For the zsh shell hook that automatically switches node versions on directory change, you can simply start by downloading the `resolve_node_version` binary to your `.nvm` folder:

```bash
curl -o- https://raw.githubusercontent.com/abejfehr/fast-nvm-switcher/v0.1.5/install.sh | bash
curl -o- https://raw.githubusercontent.com/abejfehr/fast-nvm-switcher/v0.1.6/install.sh | bash
```

And follow the instructions given.
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
#!/usr/bin/env bash

go build -o ./bin/ resolve_node_version.go
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/abejfehr/fast-nvm-switcher

go 1.17

require golang.org/x/mod v0.5.1 // indirect
require golang.org/x/mod v0.5.1
21 changes: 4 additions & 17 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,9 @@ function echo_shell_instructions() {
echo "Finalize your shell integration by copying the following lines to your .zshrc:"
echo ""
echo ""
echo " nvm_strip_path() {"
echo " command printf %s "\${1-}" | command awk -v NVM_DIR="\${NVM_DIR}" -v RS=: '"
echo " index(\$0, NVM_DIR) == 1 {"
echo " path = substr(\$0, length(NVM_DIR) + 1)"
echo " if (path ~ "^\(/versions/[^/]*\)?/[^/]*'"${2-}"'.*$") { next }"
echo " }"
echo " { print }' | command paste -s -d: -"
echo " }"
echo " "
echo " load-nvmrc() {"
echo " NODE_PATH=\$(\${NVM_DIR}/resolve_node_version)"
echo " if [ -n "\$NODE_PATH" ]; then"
echo " PATH="\$PATH:\$NODE_PATH""
echo " echo "Note location set to \$NODE_PATH""
echo " fi"
echo " }"
echo " load-nvmrc() {"
echo " PATH=\$(\${NVM_DIR}/resolve_node_version)"
echo " }"
echo " "
echo " autoload -U add-zsh-hook"
echo " add-zsh-hook chpwd load-nvmrc"
Expand All @@ -65,7 +52,7 @@ function echo_shell_instructions() {
function download_and_install_utility() {
cd $NVM_DIR

curl -L -O https://github.com/abejfehr/fast-nvm-switcher/releases/download/v0.1.5/resolve_node_version
curl -L -O https://github.com/abejfehr/fast-nvm-switcher/releases/download/v0.1.6/resolve_node_version

chmod +x $NVM_DIR/resolve_node_version

Expand Down
29 changes: 22 additions & 7 deletions resolve_node_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import (
*
* This program does the following:
* 1. Walks up the directory to find the nearest .nvmrc
* 2. Determine if that version of node is installed with nvm
* 3. Output the path to that version of node so that the shell script can set it to $PATH
* 2. Determine if that node version is installed with nvm
* 3. Output a $PATH value updated with the resolved node version for the shell integration to set
*/

var nvm_dir string = os.Getenv("NVM_DIR")

func get_nvmrc_path() string {
pwd, err := os.Getwd()

Expand Down Expand Up @@ -53,7 +55,7 @@ func get_node_path(_version string) string {

is_fuzzy_version := !strings.Contains(version, ".")

files, err := ioutil.ReadDir(os.Getenv("NVM_DIR") + "/versions/node/")
files, err := ioutil.ReadDir(nvm_dir + "/versions/node/")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -92,7 +94,20 @@ func get_node_path(_version string) string {
os.Exit(1)
}

return os.Getenv("NVM_DIR") + "/versions/node/" + resolved_version + "/bin"
return nvm_dir + "/versions/node/" + resolved_version + "/bin"
}

func prepend_to_path(path string) string {
_paths := strings.Split(os.Getenv("PATH"), ":")
paths := []string{path}

for _, p := range _paths {
if !strings.Contains(p, nvm_dir) {
paths = append(paths, p)
}
}

return strings.Join(paths, ":")
}

func main() {
Expand All @@ -106,12 +121,12 @@ func main() {
}

if version == "" {
// Get the nvm "default" alias
value, _ := os.ReadFile(os.Getenv("NVM_DIR") + "/alias/default")
// Read the nvm "default" alias
value, _ := os.ReadFile(nvm_dir + "/alias/default")
version = string(value)
}

node_path := get_node_path(version)

fmt.Println(node_path)
fmt.Println(prepend_to_path(node_path))
}

0 comments on commit 09f3178

Please sign in to comment.