-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve_node_version.go
132 lines (101 loc) · 2.48 KB
/
resolve_node_version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strings"
"golang.org/x/mod/semver"
)
/**
* resolve_node_version
*
* This program does the following:
* 1. Walks up the directory to find the nearest .nvmrc
* 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()
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 := !strings.Contains(version, ".")
files, err := ioutil.ReadDir(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 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() {
nvmrc_path := get_nvmrc_path()
version := ""
if nvmrc_path != "" {
value, _ := os.ReadFile(nvmrc_path)
version = string(value)
}
if version == "" {
// Read the nvm "default" alias
value, _ := os.ReadFile(nvm_dir + "/alias/default")
version = string(value)
}
node_path := get_node_path(version)
fmt.Println(prepend_to_path(node_path))
}