Merge pull request #331 from jchitel/master

Added exponential backoff when moving npm installation directory
This commit is contained in:
Bardur Pihl
2018-04-16 23:24:37 +02:00
committed by GitHub

View File

@@ -8,6 +8,7 @@ import (
"io/ioutil"
"regexp"
"bytes"
"time"
"./nvm/web"
"./nvm/arch"
"./nvm/file"
@@ -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"))
if err == nil {
// Remove the temp directory
// may consider keep the temp files here
os.RemoveAll(tempDir)
// 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.")