/* Install NPM */ func InstallNPM(version string) { // try catch defer func() { if err := recover(); err != nil { msg := fmt.Sprintf("'gnvm npm %v' an error has occurred. please check. \nError: ", version) Error(ERROR, msg, err) os.Exit(0) } }() version = strings.ToLower(version) if !util.VerifyNodeVer(version) { P(ERROR, "'%v' param only support [%v] [%v] or %v e.g. [%v], please check your input. See '%v'.\n", "gnvm npm", "latest", "global", "valid version", "3.8.1", "gnvm help npm") return } prompt, local, newver := "n", getLocalNPMVer(), version if version == util.GLOBAL { newver = getNodeNpmVer() } else if version == util.LATEST { newver = getLatNPMVer() } cp := CP{Red, false, None, false, newver} P(NOTICE, "local npm version is %v\n", local) P(NOTICE, "remote npm version is %v\n", cp) P(NOTICE, "download %v version [Y/n]? ", cp) fmt.Scanf("%s\n", &prompt) prompt = strings.ToLower(prompt) if prompt == "y" { downloadNpm(newver) } else { P(NOTICE, "operation has been cancelled.") } }
/* Print current local Node.js version list Param: - isPrint: when isPrint == true, print console */ func LS(isPrint bool) ([]string, error) { // try catch defer func() { if err := recover(); err != nil { Error(ERROR, "'gnvm ls' an error has occurred. please check. \nError: ", err) os.Exit(0) } }() var lsArr []string existVersion := false files, err := ioutil.ReadDir(rootPath) // show error if err != nil { P(ERROR, "'%v' Error: %v.\n", "gnvm ls", err.Error()) return lsArr, err } P(NOTICE, "gnvm.exe root is %v \n", rootPath) for _, file := range files { // set version version := file.Name() // check node version if util.VerifyNodeVer(version) { // <root>/x.xx.xx/node.exe is exist if util.IsDirExist(rootPath + version + util.DIVIDE + util.NODE) { desc := "" switch { case version == config.GetConfig(config.GLOBAL_VERSION) && version == config.GetConfig(config.LATEST_VERSION): desc = " -- global, latest" case version == config.GetConfig(config.LATEST_VERSION): desc = " -- latest" case version == config.GetConfig(config.GLOBAL_VERSION): desc = " -- global" } ver, _, _, suffix, _ := util.ParseNodeVer(version) if suffix == "x86" { desc = " -- x86" } else if suffix == "x64" { desc = " -- x64" } // set true existVersion = true // set lsArr lsArr = append(lsArr, version) if isPrint { if desc == "" { P(DEFAULT, "v"+ver+desc, "\n") } else { P(DEFAULT, "%v", "v"+ver+desc, "\n") } } } } } // version is exist if !existVersion { P(WARING, "don't have any available Node.js version, please check your input. See '%v'.\n", "gnvm help install") } return lsArr, err }
continue } v = util.EqualAbs("ALL", v) if v == "ALL" { P(WARING, "'%v' not supported mixed parameters, please usage '%v'. See '%v'.\n", "gnvm uninstall ALL", "gnvm uninstall ALL", "gnvm help uninstall") continue } v = util.EqualAbs("latest", v) if v == util.LATEST { util.FormatLatVer(&v, config.GetConfig(config.LATEST_VERSION), true) } // check version format if !util.VerifyNodeVer(v) { P(ERROR, "%v not an %v Node.js version.\n", v, "valid") } else { nodehandle.Uninstall(v) } } }, } // sub cmd var useCmd = &cobra.Command{ Use: "use", Short: "Use any the local already exists of Node.js version", Long: `Use any the local already exists of Node.js version e.g. gnvm use x.xx.xx :Usage x.xx.xx Node.js version. gnvm use latest :Usage latest Node.js version.