diff --git a/src/nvm.go b/src/nvm.go index 653881c..e7a7b77 100644 --- a/src/nvm.go +++ b/src/nvm.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "log" + "net/url" "os" "os/exec" "path/filepath" @@ -179,6 +180,7 @@ func update() { */ func install(version string, cpuarch string) { + requestedVersion := version args := os.Args lastarg := args[len(args)-1] @@ -225,6 +227,17 @@ func install(version string, cpuarch string) { version = cleanVersion(version) } + v, err := semver.Make(version) + if err == nil { + err = v.Validate() + } + + if err != nil { + fmt.Println("\"" + requestedVersion + "\" is not a valid version.") + fmt.Println("Please use a valid semantic version number, \"lts\", or \"latest\".") + return + } + if checkVersionExceedsLatest(version) { fmt.Println("Node.js v" + version + " is not yet released or available.") return @@ -368,6 +381,20 @@ func uninstall(version string) { return } + if strings.ToLower(version) == "latest" { + version = getLatest() + } else if strings.ToLower(version) == "lts" { + version = getLTS() + } else if strings.ToLower(version) == "newest" { + installed := node.GetInstalled(env.root) + if len(installed) == 0 { + fmt.Println("No versions of node.js found. Try installing the latest by typing nvm install latest") + return + } + + version = installed[0] + } + version = cleanVersion(version) // Determine if the version exists and skip if it doesn't @@ -786,28 +813,43 @@ func setup() { } // Process each line and extract the value + m := make(map[string]string) for _, line := range lines { - line = strings.Trim(line, " \r\n") + line = strings.TrimSpace(line) line = os.ExpandEnv(line) - if strings.HasPrefix(line, "root:") { - env.root = filepath.Clean(strings.TrimSpace(regexp.MustCompile("^root:").ReplaceAllString(line, ""))) - } else if strings.HasPrefix(line, "originalpath:") { - env.originalpath = filepath.Clean(strings.TrimSpace(regexp.MustCompile("^originalpath:").ReplaceAllString(line, ""))) - } else if strings.HasPrefix(line, "originalversion:") { - env.originalversion = strings.TrimSpace(regexp.MustCompile("^originalversion:").ReplaceAllString(line, "")) - } else if strings.HasPrefix(line, "arch:") { - env.arch = strings.TrimSpace(regexp.MustCompile("^arch:").ReplaceAllString(line, "")) - } else if strings.HasPrefix(line, "node_mirror:") { - env.node_mirror = strings.TrimSpace(regexp.MustCompile("^node_mirror:").ReplaceAllString(line, "")) - } else if strings.HasPrefix(line, "npm_mirror:") { - env.npm_mirror = strings.TrimSpace(regexp.MustCompile("^npm_mirror:").ReplaceAllString(line, "")) - } else if strings.HasPrefix(line, "proxy:") { - env.proxy = strings.TrimSpace(regexp.MustCompile("^proxy:").ReplaceAllString(line, "")) - if env.proxy != "none" && env.proxy != "" { - if strings.ToLower(env.proxy[0:4]) != "http" { - env.proxy = "http://" + env.proxy - } - web.SetProxy(env.proxy, env.verifyssl) + res := strings.Split(line, ":") + if len(res) < 2 { + continue + } + m[res[0]] = strings.TrimSpace(strings.Join(res[1:], ":")) + } + + if val, ok := m["root"]; ok { + env.root = filepath.Clean(val) + } + if val, ok := m["originalpath"]; ok { + env.originalpath = filepath.Clean(val) + } + if val, ok := m["originalversion"]; ok { + env.originalversion = val + } + if val, ok := m["arch"]; ok { + env.arch = val + } + if val, ok := m["node_mirror"]; ok { + env.node_mirror = val + } + if val, ok := m["npm_mirror"]; ok { + env.npm_mirror = val + } + if val, ok := m["proxy"]; ok { + if val != "none" && val != "" { + if strings.ToLower(val[0:4]) != "http" { + val = "http://" + val + } + res, err := url.Parse(val) + if err != nil { + web.SetProxy(res.String(), env.verifyssl) } } } diff --git a/v1.1.8.md b/v1.1.8.md index 7105d5d..42b847b 100644 --- a/v1.1.8.md +++ b/v1.1.8.md @@ -16,7 +16,7 @@ Several long-requested features/fixes have been merged since v1.1.7. I did not g 2. Support for spaces in filepaths [#355](https://github.com/coreybutler/nvm-windows/pull/355). 3. `nvm install latest` installs the latest patch version of Node (instead of latest minor). 4. `nvm install lts` installs the latest LTS patch version (new). -5. +5. `nvm use latest`, `nvm use lts`, and `nvm use newest` (new) now supported. 6. Elevated permissions applied only when necessary [#511](https://github.com/coreybutler/nvm-windows/pull/511). 7. `nvm current` (new) displays the active version.