Skip to content

Commit

Permalink
Add checks for network connectivity
Browse files Browse the repository at this point in the history
  • Loading branch information
coreybutler committed Feb 14, 2023
1 parent 6e2c8da commit 9366b98
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/nvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,10 +928,33 @@ func checkLocalEnvironment() {
fmt.Printf("\nWindows Developer Mode: %v\n", devmode)
}

fmt.Printf("\nInformation\n-----------\nPath: %v\nVersion: %v\nNVM_HOME: %v\nNVM_SYMLINK: %v\n", os.Args[0], NvmVersion, home, symlink)

if !nvmsymlinkfound {
problems = append(problems, "The NVM4W symlink ("+env.symlink+") was not found in the PATH environment variable.")
}

nodelist := web.Ping(web.GetFullNodeUrl("index.json"))
if !nodelist {
if len(env.node_mirror) > 0 && env.node_mirror != "none" {
problems = append(problems, "Connection to "+env.node_mirror+" (mirror) cannot be established. Check the mirror server to assure it is online.")
} else {
if len(env.proxy) > 0 {
problems = append(problems, "Connection to nodejs.org cannot be established. Check your proxy ("+env.proxy+") and your physical internet connection.")
} else {
problems = append(problems, "Connection to nodejs.org cannot be established. Check your internet connection.")
}
}
}

if len(env.npm_mirror) > 0 {
fmt.Println("If you are experiencing npm problems, check the npm mirror (" + env.npm_mirror + ") to assure it is online and accessible.")
}

if _, err := os.Stat(env.settings); err != nil {
problems = append(problems, "Cannot find "+env.settings)
}

if len(problems) == 0 {
fmt.Println("\nNo problems detected.")
return
Expand Down
21 changes: 21 additions & 0 deletions src/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ func GetFullNpmUrl(path string) string {
return npmBaseAddress + path
}

func Ping(url string) bool {
req, err := http.NewRequest("HEAD", url, nil)
if err != nil {
fmt.Println(err)
return false
}

req.Header.Set("User-Agent", "NVM for Windows")

response, err := client.Do(req)
if err != nil {
return false
}

if response.StatusCode == 200 {
return true
}

return false
}

func Download(url string, target string, version string) bool {
output, err := os.Create(target)
if err != nil {
Expand Down

0 comments on commit 9366b98

Please sign in to comment.