Skip to content

Commit

Permalink
Merge pull request coreybutler#51 from inoc603/tempdir
Browse files Browse the repository at this point in the history
Add a local temp directory to prevent problems with cross-partition permissions. Closes coreybutler#46.
  • Loading branch information
coreybutler committed Mar 30, 2015
2 parents e4164d0 + a1f6d41 commit 73c5e93
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
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

0 comments on commit 73c5e93

Please sign in to comment.