diff --git a/src/nvm.go b/src/nvm.go index 5e4bae0..5202301 100644 --- a/src/nvm.go +++ b/src/nvm.go @@ -211,20 +211,24 @@ func install(version string, cpuarch string) { // If successful, add npm npmv := getNpmVersion(version) - success := web.GetNpm(getNpmVersion(version)) + success := web.GetNpm(env.root, getNpmVersion(version)) if success { fmt.Printf("Installing npm v"+npmv+"...") + // new temp directory under the nvm root + tempDir := env.root + "\\temp" + // Extract npm to the temp directory - file.Unzip(os.TempDir()+"\\npm-v"+npmv+".zip",os.TempDir()+"\\nvm-npm") + file.Unzip(tempDir+"\\npm-v"+npmv+".zip",tempDir+"\\nvm-npm") // Copy the npm and npm.cmd files to the installation directory - os.Rename(os.TempDir()+"\\nvm-npm\\npm-"+npmv+"\\bin\\npm",env.root+"\\v"+version+"\\npm") - os.Rename(os.TempDir()+"\\nvm-npm\\npm-"+npmv+"\\bin\\npm.cmd",env.root+"\\v"+version+"\\npm.cmd") - os.Rename(os.TempDir()+"\\nvm-npm\\npm-"+npmv,env.root+"\\v"+version+"\\node_modules\\npm") + os.Rename(tempDir+"\\nvm-npm\\npm-"+npmv+"\\bin\\npm",env.root+"\\v"+version+"\\npm") + os.Rename(tempDir+"\\nvm-npm\\npm-"+npmv+"\\bin\\npm.cmd",env.root+"\\v"+version+"\\npm.cmd") + os.Rename(tempDir+"\\nvm-npm\\npm-"+npmv,env.root+"\\v"+version+"\\node_modules\\npm") - // Remove the source file - os.RemoveAll(os.TempDir()+"\\nvm-npm") + // 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) } else { diff --git a/src/nvm/web/web.go b/src/nvm/web/web.go index c8d646e..a4f7e42 100644 --- a/src/nvm/web/web.go +++ b/src/nvm/web/web.go @@ -10,6 +10,7 @@ import( "strings" "strconv" "../arch" + "../file" ) var client = &http.Client{} @@ -81,9 +82,21 @@ func GetNodeJS(root string, v string, a string) bool { } -func GetNpm(v string) bool { +func GetNpm(root string, v string) bool { url := "https://github.com/npm/npm/archive/v"+v+".zip" - fileName := os.TempDir()+"\\"+"npm-v"+v+".zip" + // temp directory to download the .zip file + tempDir := root+"\\temp" + + // if the temp directory doesn't exist, create it + if (!file.Exists(tempDir)) { + fmt.Println("Creating "+tempDir+"\n") + err := os.Mkdir(tempDir, os.ModePerm) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + } + fileName := tempDir+"\\"+"npm-v"+v+".zip" fmt.Printf("Downloading npm version "+v+"... ") if Download(url,fileName) {