Skip to content

Commit

Permalink
Merge pull request coreybutler#331 from jchitel/master
Browse files Browse the repository at this point in the history
Added exponential backoff when moving npm installation directory
  • Loading branch information
Bardur Pihl authored Apr 16, 2018
2 parents fa4cfec + a9ed9fa commit 24c57b8
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/nvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"regexp"
"bytes"
"time"
"./nvm/web"
"./nvm/arch"
"./nvm/file"
Expand Down Expand Up @@ -279,14 +280,26 @@ func install(version string, cpuarch string) {
os.Rename(filepath.Join(tempNpmBin, "npx"), filepath.Join(env.root, "v"+version, "npx"))
os.Rename(filepath.Join(tempNpmBin, "npx.cmd"), filepath.Join(env.root, "v"+version, "npx.cmd"))
}

err := os.Rename(filepath.Join(tempDir, "nvm-npm", "npm-"+npmv), filepath.Join(env.root, "v"+version, "node_modules", "npm"))
if err != nil {
// sometimes Windows can take some time to enable access to large amounts of files after unzip, use exponential backoff to wait until it is ready
for _, i := range [5]int{1, 2, 4, 8, 16} {
time.Sleep(time.Duration(i)*time.Second)
err = os.Rename(filepath.Join(tempDir, "nvm-npm", "npm-"+npmv), filepath.Join(env.root, "v"+version, "node_modules", "npm"))
if err == nil { break }
}
}

os.Rename(filepath.Join(tempDir, "nvm-npm", "npm-"+npmv), filepath.Join(env.root, "v"+version, "node_modules", "npm"))

// Remove the temp directory
// may consider keep the temp files here
os.RemoveAll(tempDir)
if err == nil {
// Remove the temp directory
// may consider keep the temp files here
os.RemoveAll(tempDir)

fmt.Println("\n\nInstallation complete. If you want to use this version, type\n\nnvm use "+version)
fmt.Println("\n\nInstallation complete. If you want to use this version, type\n\nnvm use "+version)
} else {
fmt.Println("Error: Unable to install NPM: "+err.Error());
}
} else {
fmt.Println("Could not download npm for node v"+version+".")
fmt.Println("Please visit https://github.com/npm/npm/releases/tag/v"+npmv+" to download npm.")
Expand Down

0 comments on commit 24c57b8

Please sign in to comment.