@@ -3,10 +3,59 @@ package util
3
3
import (
4
4
"fmt"
5
5
"os"
6
+ "runtime"
7
+ "strings"
6
8
)
7
9
10
+ func removeFromSlice (s []string , r string ) []string {
11
+ result := s [:0 ] // Use the same slice to avoid extra allocations
12
+ for _ , v := range s {
13
+ if v != r {
14
+ result = append (result , v )
15
+ }
16
+ }
17
+ return result
18
+ }
19
+
20
+ func removeNodePath (paths string ) string {
21
+ // Split the PATH into directories
22
+ dirs := strings .Split (paths , string (os .PathListSeparator ))
23
+
24
+ // Remove the node binary directory from the PATH
25
+ for _ , dir := range dirs {
26
+ stat , err := os .Stat (dir )
27
+
28
+ if err != nil {
29
+ continue
30
+ }
31
+
32
+ if ! stat .IsDir () {
33
+ continue
34
+ }
35
+
36
+ // Check if the directory contains the node binary
37
+ files , err := os .ReadDir (dir )
38
+ if err != nil {
39
+ continue
40
+ }
41
+
42
+ fileLoop:
43
+ for _ , file := range files {
44
+ isContainsNodeBinary := (runtime .GOOS == "windows" && file .Name () == "node.exe" ) || (runtime .GOOS != "windows" && file .Name () == "node" )
45
+
46
+ if isContainsNodeBinary {
47
+ // Remove the directory from the PATH
48
+ dirs = removeFromSlice (dirs , dir )
49
+ break fileLoop
50
+ }
51
+ }
52
+ }
53
+
54
+ return strings .Join (dirs , string (os .PathListSeparator ))
55
+ }
56
+
8
57
func AppendEnvPath (pathDir string ) string {
9
- oldPath := os .Getenv ("PATH" )
58
+ oldPath := removeNodePath ( os .Getenv ("PATH" ) )
10
59
11
60
newPath := fmt .Sprintf ("%s%c%s%c%s" , pathDir , os .PathListSeparator , oldPath , os .PathListSeparator , pathDir )
12
61
0 commit comments