-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathnode-utils.go
More file actions
90 lines (79 loc) · 2.08 KB
/
node-utils.go
File metadata and controls
90 lines (79 loc) · 2.08 KB
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
package config
import (
"codacy/cli-v2/utils"
"fmt"
"log"
"os"
"path"
"path/filepath"
"runtime"
)
func getNodeFileName(nodeRuntime *Runtime) string {
// Detect the OS and architecture
goos := runtime.GOOS
goarch := runtime.GOARCH
// Map Go architecture to Node.js architecture
var nodeArch string
switch goarch {
case "386":
nodeArch = "x86"
case "amd64":
nodeArch = "x64"
case "arm":
nodeArch = "armv7l"
case "arm64":
nodeArch = "arm64"
default:
nodeArch = goarch
}
return fmt.Sprintf("node-v%s-%s-%s", nodeRuntime.Version(), goos, nodeArch)
}
func genInfoNode(r *Runtime) map[string]string {
nodeFileName := getNodeFileName(r)
return map[string]string{
"nodeFileName": nodeFileName,
"installDir": path.Join(Config.RuntimesDirectory(), nodeFileName),
"node": path.Join(Config.RuntimesDirectory(), nodeFileName, "bin", "node"),
"npm": path.Join(Config.RuntimesDirectory(), nodeFileName, "bin", "npm"),
}
}
func getNodeDownloadURL(nodeRuntime *Runtime) string {
// Detect the OS and architecture
goos := runtime.GOOS
// Construct the Node.js download URL
extension := "tar.gz"
if goos == "windows" {
extension = "zip"
}
downloadURL := fmt.Sprintf("https://nodejs.org/dist/v%s/%s.%s", nodeRuntime.Version(), getNodeFileName(nodeRuntime), extension)
return downloadURL
}
func InstallNode(r *Runtime) error {
// TODO should delete downloaded archive
// TODO check for deflated archive
downloadNodeURL := getNodeDownloadURL(r)
fileName := filepath.Base(downloadNodeURL)
t, err := os.Open(filepath.Join(Config.RuntimesDirectory(), fileName))
defer t.Close()
if err != nil {
log.Println("Node is not present, fetching node...")
nodeTar, err := utils.DownloadFile(downloadNodeURL, Config.RuntimesDirectory())
if err != nil {
return err
}
t, err = os.Open(nodeTar)
defer t.Close()
if err != nil {
return err
}
} else {
fmt.Println("Node is already present...")
}
fmt.Println("Extracting node...")
// deflate node archive
err = utils.ExtractTarGz(t, Config.RuntimesDirectory())
if err != nil {
return err
}
return nil
}