Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use new temp directory for npm #51

Merged
merged 1 commit into from
Mar 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/nvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 15 additions & 2 deletions src/nvm/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import(
"strings"
"strconv"
"../arch"
"../file"
)

var client = &http.Client{}
Expand Down Expand Up @@ -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) {
Expand Down