Skip to content

Commit

Permalink
make dir if not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
vineelsai26 committed Mar 19, 2023
1 parent ac07896 commit 06dc872
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 56 deletions.
118 changes: 62 additions & 56 deletions node/use.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,62 @@
package node

import (
"fmt"
"os"
"path/filepath"
"runtime"
)

func SetPath(path string) {
if runtime.GOOS == "windows" {
SetPathWindows(path)
} else if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
SetPathLinux(path)
} else {
fmt.Println("Not implemented for this OS")
}
}

func Use(version string) {
path, err := GetVersionPath(version)
if err != nil {
panic(err)
}

f, err := os.OpenFile(filepath.Join(GetHome(), ".vmn", "current"), os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
panic(err)
}
defer f.Close()

if _, err := f.Stat(); err == nil {
f.Truncate(0)
f.Seek(0, 0)
f.WriteString(path)
}

if _, err := os.Stat(path); err == nil {
fmt.Println("Setting VMN_VERSION to " + version + " ... ")
SetPath(path)
} else {
fmt.Println("Node.js version " + version + " is not installed")
}
}

func UseLatest() {
Use(GetLatestVersion())
}

func UseLatestLTS() {
Use(GetLatestLTSVersion())
}

func UseSpecific(version string) {
Use(GetLatestVersionOfVersion(version))
}
package node

import (
"fmt"
"os"
"path/filepath"
"runtime"
)

func SetPath(path string) {
if runtime.GOOS == "windows" {
SetPathWindows(path)
} else if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
SetPathLinux(path)
} else {
fmt.Println("Not implemented for this OS")
}
}

func Use(version string) {
path, err := GetVersionPath(version)
if err != nil {
panic(err)
}

if _, err := os.Stat(filepath.Join(GetHome(), ".vmn")); os.IsNotExist(err) {
if err := os.MkdirAll(filepath.Join(GetHome(), ".vmn"), 0755); err != nil {
panic(err)
}
}

f, err := os.OpenFile(filepath.Join(GetHome(), ".vmn", "current"), os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
panic(err)
}
defer f.Close()

if _, err := f.Stat(); err == nil {
f.Truncate(0)
f.Seek(0, 0)
f.WriteString(path)
}

if _, err := os.Stat(path); err == nil {
fmt.Println("Setting VMN_VERSION to " + version + " ... ")
SetPath(path)
} else {
fmt.Println("Node.js version " + version + " is not installed")
}
}

func UseLatest() {
Use(GetLatestVersion())
}

func UseLatestLTS() {
Use(GetLatestLTSVersion())
}

func UseSpecific(version string) {
Use(GetLatestVersionOfVersion(version))
}
6 changes: 6 additions & 0 deletions setup/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ func Install() {
}
defer srcFile.Close()

if _, err := os.Stat(filepath.Join(node.GetHome(), ".vmn")); os.IsNotExist(err) {
if err := os.MkdirAll(filepath.Join(node.GetHome(), ".vmn"), 0755); err != nil {
panic(err)
}
}

destFile, err := os.Create(filepath.Join(node.GetHome(), ".vmn", "vmn.exe"))
if err != nil {
panic(err)
Expand Down

0 comments on commit 06dc872

Please sign in to comment.