-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
node.go
207 lines (179 loc) · 4.71 KB
/
node.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package node
import(
"os/exec"
"strings"
"regexp"
"io/ioutil"
"encoding/json"
"../arch"
"../file"
"../web"
// "../semver"
"github.com/blang/semver"
)
/**
* Returns version, architecture
*/
func GetCurrentVersion() (string, string) {
cmd := exec.Command("node","-v")
str, err := cmd.Output()
if err == nil {
v := strings.Trim(regexp.MustCompile("-.*$").ReplaceAllString(regexp.MustCompile("v").ReplaceAllString(strings.Trim(string(str)," \n\r"),""),"")," \n\r")
cmd := exec.Command("node","-p","console.log(process.execPath)")
str, _ := cmd.Output()
file := strings.Trim(regexp.MustCompile("undefined").ReplaceAllString(string(str),"")," \n\r")
bit := arch.Bit(file)
if (bit == "?"){
cmd := exec.Command("node", "-e", "console.log(process.arch)" )
str, err := cmd.Output()
if (err == nil) {
if (string(str) == "x64") {
bit = "64"
} else {
bit = "32"
}
} else {
return v, "Unknown"
}
}
return v, bit
}
return "Unknown",""
}
func IsVersionInstalled(root string, version string, cpu string) bool {
e32 := file.Exists(root+"\\v"+version+"\\node32.exe")
e64 := file.Exists(root+"\\v"+version+"\\node64.exe")
used := file.Exists(root+"\\v"+version+"\\node.exe")
if cpu == "all" {
return ((e32 || e64) && used) || e32 && e64
}
if file.Exists(root+"\\v"+version+"\\node"+cpu+".exe") {
return true
}
if ((e32||e64) && used) || (e32 && e64) {
return true
}
if !e32 && !e64 && used && arch.Validate(cpu) == arch.Bit(root+"\\v"+version+"\\node.exe") {
return true
}
if cpu == "32" {
return e32
}
if cpu == "64" {
return e64
}
return false
}
func IsVersionAvailable(v string) bool {
// Check the service to make sure the version is available
avail, _, _, _, _, _ := GetAvailable()
for _, b := range avail {
if b == v {
return true
}
}
return false
}
func GetInstalled(root string) []string {
list := make([]string,0)
files, _ := ioutil.ReadDir(root)
for i := len(files) - 1; i >= 0; i-- {
if files[i].IsDir() {
isnode, _ := regexp.MatchString("v",files[i].Name())
if isnode {
list = append(list,files[i].Name())
}
}
}
return list
}
// Sorting
type BySemanticVersion []string
func (s BySemanticVersion) Len() int {
return len(s)
}
func (s BySemanticVersion) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s BySemanticVersion) Less(i, j int) bool {
v1, _ := semver.Make(s[i])
v2, _ := semver.Make(s[j])
return v1.GTE(v2)
}
// Identifies a version as "LTS"
func isLTS(element map[string]interface{}) bool {
switch datatype := element["lts"].(type) {
case bool:
return datatype
case string:
return true
}
return false
}
// Identifies a version as "current"
func isCurrent(element map[string]interface{}) bool {
if isLTS(element) {
return false
}
version, _ := semver.Make(element["version"].(string)[1:])
benchmark, _ := semver.Make("1.0.0")
if version.LT(benchmark) {
return false
}
return true
// return version.Major%2 == 1
}
// Identifies a stable old version.
func isStable(element map[string]interface{}) bool {
if isCurrent(element) {
return false
}
version, _ := semver.Make(element["version"].(string)[1:])
if (version.Major != 0) {
return false
}
return version.Minor%2 == 0
}
// Identifies an unstable old version.
func isUnstable(element map[string]interface{}) bool {
if isStable(element) {
return false
}
version, _ := semver.Make(element["version"].(string)[1:])
if (version.Major != 0) {
return false
}
return version.Minor%2 != 0
}
// Retrieve the remotely available versions
func GetAvailable() ([]string, []string, []string, []string, []string, map[string]string) {
all := make([]string,0)
lts := make([]string,0)
current := make([]string,0)
stable := make([]string,0)
unstable := make([]string,0)
npm := make(map[string]string)
url := web.GetFullNodeUrl("index.json")
// Check the service to make sure the version is available
text := web.GetRemoteTextFile(url)
// Parse
var data = make([]map[string]interface{}, 0)
json.Unmarshal([]byte(text), &data);
for _, element := range data {
var version = element["version"].(string)[1:]
all = append(all, version)
if val, ok := element["npm"].(string); ok {
npm[version] = val
}
if isLTS(element) {
lts = append(lts, version)
} else if isCurrent(element) {
current = append(current, version)
} else if isStable(element) {
stable = append(stable, version)
} else if isUnstable(element) {
unstable = append(unstable, version)
}
}
return all, lts, current, stable, unstable, npm
}