/* Update local Node.js latest verion - localVersion, remoteVersion: string Node.js version - local, remote: float64 Node.js version Param: - global: when global == true, call Use func. */ func Update(global bool) { // try catch defer func() { if err := recover(); err != nil { msg := fmt.Sprintf("'%v' an error has occurred. \nError: ", "gnvm updte latest") Error(ERROR, msg, err) os.Exit(0) } }() localVersion, remoteVersion := config.GetConfig(config.LATEST_VERSION), util.GetLatVer(latURL) P(NOTICE, "local Node.js latest version is %v.\n", localVersion) if remoteVersion == "" { P(ERROR, "get latest version error, please check. See '%v'.\n", "gnvm help config") return } P(NOTICE, "remote Node.js latest version is %v from %v.\n", remoteVersion, config.GetConfig("registry")) local, remote, args := util.FormatNodeVer(localVersion), util.FormatNodeVer(remoteVersion), []string{remoteVersion} switch { case localVersion == util.UNKNOWN: if code := InstallNode(args, global); code == 0 { config.SetConfig(config.LATEST_VERSION, remoteVersion) P(DEFAULT, "Update Node.js latest success, current latest version is %v.\n", remoteVersion) } case local == remote: if util.IsDirExist(rootPath + localVersion) { cp := CP{Red, false, None, false, "="} P(DEFAULT, "Remote latest version %v %v latest version %v, don't need to upgrade.\n", remoteVersion, cp, localVersion) if global { if ok := Use(localVersion); ok { config.SetConfig(config.GLOBAL_VERSION, localVersion) } } } else { P(WARING, "%v folder is not exist. See '%v'.\n", localVersion, "gnvm ls") if code := InstallNode(args, global); code == 0 { P(DEFAULT, "Local Node.js latest version is %v.\n", localVersion) } } case local > remote: cp := CP{Red, false, None, false, ">"} P(WARING, "local latest version %v %v remote latest version %v.\nPlease check your config %v. See '%v'.\n", localVersion, cp, remoteVersion, "registry", "gnvm help config") case local < remote: cp := CP{Red, false, None, false, ">"} P(WARING, "remote latest version %v %v local latest version %v.\n", remoteVersion, cp, localVersion) if code := InstallNode(args, global); code == 0 { config.SetConfig(config.LATEST_VERSION, remoteVersion) P(DEFAULT, "Update success, Node.js latest version is %v.\n", remoteVersion) } } }
/* Format exe Param: - version: Node.js version Return: - exec: formatting string, e.g. '[x]' */ func formatExe(version string) (exec string) { switch util.GetNodeVerLev(util.FormatNodeVer(version)) { case 0: exec = "[x]" case 1: exec = "x86" default: exec = "x86 x64" } return }
/* Get npm version by global( local ) node version Return: - string: npm version */ func getNodeNpmVer() string { ver, err := util.GetNodeVer(rootPath) if err != nil { panic(errors.New("not exist global node.exe. please usage 'gnvm install latest -g' frist.")) } url := config.GetConfig(config.REGISTRY) if level := util.GetNodeVerLev(util.FormatNodeVer(ver)); level == 3 { url = config.GetIOURL(url) } url += util.NODELIST nd, err := FindNodeDetailByVer(url, ver) if err != nil { panic(err) } return nd.NPM.Version }
/* Show local / global Node.js version Param: - args: include: latest global */ func NodeVersion(args []string) { // try catch defer func() { if err := recover(); err != nil { msg := fmt.Sprintf("'gnvm node-version %v' an error has occurred. please check. \nError: ", strings.Join(args, " ")) Error(ERROR, msg, err) os.Exit(0) } }() /* if len(args) == 0 { P(DEFAULT, "Node.js %v version is %v.\n", "latest", latest) P(DEFAULT, "Node.js %v version is %v.\n", "global", global) if latest == util.UNKNOWN { P(WARING, "latest version is %v, please use '%v'. See '%v'.\n", util.UNKNOWN, "gnvm node-version latest -r", "gnvm help node-version") } if global == util.UNKNOWN { P(WARING, "global version is %v, please use '%v' or '%v'. See '%v'.\n", util.UNKNOWN, "gnvm install latest -g", "gnvm install x.xx.xx -g", "gnvm help install") } } else { switch { case args[0] == "global": P(DEFAULT, "Node.js global version is %v.\n", global) if global == util.UNKNOWN { P(WARING, "global version is %v, please use '%v' or '%v'. See '%v'.\n", util.UNKNOWN, "gnvm install latest -g", "gnvm install x.xx.xx -g", "gnvm help install") } case args[0] == "latest" && !remote: P(DEFAULT, "Node.js latest version is %v.\n", latest) if latest == util.UNKNOWN { P(WARING, "latest version is %v, please use '%v'. See '%v'.\n", util.UNKNOWN, "gnvm node-version latest -r", "gnvm help node-version") } case args[0] == "latest" && remote: remoteVersion := util.GetLatVer(latURL) if remoteVersion == "" { P(ERROR, "get remote %v Node.js %v error, please check your input. See '%v'.\n", config.GetConfig(config.REGISTRY), "latest version", "gnvm help config") return } P(DEFAULT, "Local Node.js latest version is %v.\n", latest) P(DEFAULT, "Remote Node.js latest version is %v from %v.\n", remoteVersion, config.GetConfig(config.REGISTRY)) if latest == util.UNKNOWN { config.SetConfig(config.LATEST_VERSION, remoteVersion) P(DEFAULT, "Set success, local Node.js %v version is %v.\n", util.LATEST, remoteVersion) return } v1 := util.FormatNodeVer(latest) v2 := util.FormatNodeVer(remoteVersion) if v1 < v2 { cp := CP{Red, false, None, false, ">"} P(WARING, "remote Node.js latest version %v %v local Node.js latest version %v, suggest to upgrade, usage '%v'.\n", remoteVersion, cp, latest, "gnvm update latest") } } } */ isLatest, isGlobal := false, false latest, global := config.GetConfig(config.LATEST_VERSION), config.GetConfig(config.GLOBAL_VERSION) if len(args) == 0 { isLatest = true isGlobal = true } else { if args[0] == util.LATEST { isLatest = true } else { isGlobal = true } } if isGlobal { if global == util.UNKNOWN { P(WARING, "global Node.js version is %v.\n", util.UNKNOWN) if global, err := util.GetNodeVer(rootPath); err == nil { config.SetConfig(config.GLOBAL_VERSION, global) P(DEFAULT, "Set success, %v new value is %v.\n", config.GLOBAL_VERSION, global) } else { P(WARING, "global Node.js version is %v, please use %v or %v. See '%v'.\n", util.UNKNOWN, "gnvm install latest -g", "gnvm install x.xx.xx -g", "gnvm help install") } } else { P(DEFAULT, "Node.js %v version is %v.\n", "global", global) } } if isLatest { if latest == util.UNKNOWN { P(WARING, "latest Node.js version is %v, please use %v or %v. See '%v'.\n", util.UNKNOWN, "gnvm install latest -g", "gnvm update latest", "gnvm help node-version") } else { P(DEFAULT, "Node.js %v version is %v.\n", "latest", latest) } remoteVersion := util.GetLatVer(latURL) if remoteVersion == "" { P(ERROR, "get remote %v Node.js %v error, please check your input. See '%v'.\n", config.GetConfig(config.REGISTRY), "latest version", "gnvm help config") return } if latest == util.UNKNOWN { P(NOTICE, "remote Node.js %v version is %v from %v.\n", "latest", remoteVersion, config.GetConfig(config.REGISTRY)) //config.SetConfig(config.LATEST_VERSION, remoteVersion) //P(DEFAULT, "Set success, local Node.js %v version is %v.\n", util.LATEST, remoteVersion) return } v1 := util.FormatNodeVer(latest) v2 := util.FormatNodeVer(remoteVersion) if v1 < v2 { cp := CP{Red, false, None, false, ">"} P(WARING, "remote Node.js latest version %v %v local Node.js latest version %v, suggest to upgrade, usage '%v'.\n", remoteVersion, cp, latest, "gnvm update latest") } } }