Skip to content

Commit

Permalink
Convert it to Go instead
Browse files Browse the repository at this point in the history
  • Loading branch information
abejfehr committed Feb 15, 2022
1 parent d598a32 commit 051a501
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 85 deletions.
4 changes: 0 additions & 4 deletions .vscode/settings.json

This file was deleted.

12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ num calls time self name

### With "fast nvm switcher"

The output is always on the order of tens of milliseconds, with hundreds of milliseconds being the longest possible time.
The output is always on the order of ~10ms.

<details>

Expand All @@ -141,16 +141,16 @@ The output is always on the order of tens of milliseconds, with hundreds of mill
```
num calls time self name
-----------------------------------------------------------------------------------
1) 1 119.36 119.36 99.36% 119.36 119.36 99.36% load-nvmrc
2) 1 0.77 0.77 0.64% 0.77 0.77 0.64% add-zsh-hook
1) 1 7.77 7.77 94.53% 7.77 7.77 94.53% load-nvmrc
2) 1 0.45 0.45 5.47% 0.45 0.45 5.47% add-zsh-hook
-----------------------------------------------------------------------------------
1) 1 119.36 119.36 99.36% 119.36 119.36 99.36% load-nvmrc
1) 1 7.77 7.77 94.53% 7.77 7.77 94.53% load-nvmrc
-----------------------------------------------------------------------------------
2) 1 0.77 0.77 0.64% 0.77 0.77 0.64% add-zsh-hook
2) 1 0.45 0.45 5.47% 0.45 0.45 5.47% add-zsh-hook
```

</details>
Expand All @@ -164,7 +164,7 @@ If you already have nvm installed, remove anything nvm-related you might already
Next, run the following script to download the `resolve_node_version` binary to your machine:

```sh
(cd $HOME/.nvm/ && curl -L -O https://github.com/abejfehr/fast-nvm-switcher/releases/download/0.1.1/resolve_node_version)
(cd $HOME/.nvm/ && curl -L -O https://github.com/abejfehr/fast-nvm-switcher/releases/download/0.1.2/resolve_node_version)
```

Add the following lines to your .zshrc
Expand Down
4 changes: 3 additions & 1 deletion build
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash

deno compile --allow-read --allow-env --output ./bin/resolve_node_version ./resolve_node_version.ts
# deno compile --allow-read --allow-env --output ./bin/resolve_node_version ./resolve_node_version.ts

go build -o ./bin resolve_node_version.go
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/abejfehr/fast-nvm-switcher

go 1.17

require golang.org/x/mod v0.5.1 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
118 changes: 118 additions & 0 deletions resolve_node_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package main

import (
"fmt"
"golang.org/x/mod/semver"
"io/ioutil"
"log"
"os"
"path"
"strings"
)

/**
* resolve_node_version
*
* 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
*/

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

if err != nil {
log.Println(err)
}

nvmrc_path := path.Join(pwd, ".nvmrc");

if _, err := os.Stat(nvmrc_path); err == nil {
return nvmrc_path;
}

for (pwd != "/") {
pwd = path.Dir(pwd); // goes up one

nvmrc_path = path.Join(pwd, ".nvmrc");

if _, err := os.Stat(nvmrc_path); err == nil {
return nvmrc_path;
}
}

return "" // TODO: Return an error in this case
}


func get_node_path(_version string) string {
versions := []string {};
version := strings.Trim(_version, "\n");
version = "v" + strings.Trim(version, "v");

is_fuzzy_version := !semver.IsValid(version);

files, err := ioutil.ReadDir(os.Getenv("NVM_DIR") + "/versions/node/")
if err != nil {
log.Fatal(err)
}

for _, file := range files {
if (file.IsDir()) {
versions = append(versions, file.Name());
}
}

semver.Sort(versions);

resolved_version := ""

// Reversing the versions slice
for i, j := 0, len(versions)-1; i < j; i, j = i+1, j-1 {
versions[i], versions[j] = versions[j], versions[i]
}

for _, v := range versions {
if (is_fuzzy_version) {
if (v == version) {
resolved_version = v;
break;
}
} else {
if (semver.Major(v) == semver.Major(version)) {
resolved_version = v;
break;
}
}
}

if (resolved_version == "") {
fmt.Println("Unable to find node version that matches " + version + ", please run 'nvm install'");
os.Exit(1);
}

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

func main() {
nvmrc_path := get_nvmrc_path();

version := ""

if (nvmrc_path != "") {
value, _ := os.ReadFile(nvmrc_path)
version = string(value);
}

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


node_path := get_node_path(version);

fmt.Println(node_path)
}
74 changes: 0 additions & 74 deletions resolve_node_version.ts

This file was deleted.

0 comments on commit 051a501

Please sign in to comment.