示例#1
0
文件: regedit.go 项目: Kenshin/gnvm
func init() {
	noderoot = config.GetConfig(config.NODEROOT)
	nodehome = os.Getenv(NODE_HOME)
	if nodehome == "" && config.GetConfig(config.GLOBAL_VERSION) == util.UNKNOWN {
		P(NOTICE, "not found environment variable '%v', please use '%v'. See '%v'.\n", NODE_HOME, "gnvm reg noderoot", "gnvm help reg")
	}
}
示例#2
0
/*
 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)
		}
	}
}
示例#3
0
func NodeVersion(args []string, remote bool) {

	// 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)
		}
	}()

	latest := config.GetConfig(config.LATEST_VERSION)
	global := config.GetConfig(config.GLOBAL_VERSION)

	if len(args) == 0 || len(args) > 1 {
		P(DEFAULT, "Node.exe %v version is %v.\n", "latest", latest)
		P(DEFAULT, "Node.exe %v version is %v.\n", "global", global)
	} else {
		switch {
		case args[0] == "global":
			P(DEFAULT, "Node.exe global version is %v.\n", global)
		case args[0] == "latest" && !remote:
			P(DEFAULT, "Node.exe latest version is %v.\n", latest)
		case args[0] == "latest" && remote:
			remoteVersion := getLatestVersionByRemote()
			if remoteVersion == "" {
				P(ERROR, "get remote %v latest version error, please check. See '%v'.\n", config.GetConfig("registry")+config.LATEST+"/"+config.NODELIST, "gnvm help config")
				P(DEFAULT, "Node.exe latest version is %v.\n", latest)
				return
			}
			P(DEFAULT, "Node.exe remote %v version is %v.\n", config.GetConfig("registry"), remoteVersion)
			P(DEFAULT, "Node.exe latest version is %v.\n", latest)
		}
	}

	isPrint := false
	switch {
	case len(args) == 0 && (global == config.UNKNOWN || latest == config.UNKNOWN):
		isPrint = true
	case len(args) > 0 && args[0] == "latest" && latest == config.UNKNOWN:
		isPrint = true
	case len(args) > 0 && args[0] == "global" && global == config.UNKNOWN:
		isPrint = true
	}
	if isPrint {
		P(WARING, "when version is %v, please use '%v'. See '%v'.\n", config.UNKNOWN, "gnvm config INIT", "gnvm help config")
	}
}
示例#4
0
func TransLatestVersion(latest string, isPrint bool) string {
	if latest == config.LATEST {
		latest = config.GetConfig(config.LATEST_VERSION)
		if isPrint {
			P(NOTICE, "current latest version is %v.\n", latest)
		}
	}
	return latest
}
示例#5
0
文件: npm.go 项目: Kenshin/gnvm
/*
 Create NPMange
*/
func (this *NPMange) New() *NPMange {
	this.root = config.GetConfig(config.NODEROOT)
	this.modules = this.root + util.DIVIDE + "node_modules"
	this.npmpath = this.modules + util.DIVIDE + util.NPM
	this.npmbin = this.npmpath + util.DIVIDE + "bin"
	this.command1 = "npm"
	this.command2 = "npm.cmd"
	return this
}
示例#6
0
/*
 * return code
 * 0: success
 *
 */
func downloadNpm(version string) int {

	// set url
	url := config.GetConfig(config.REGISTRY) + "npm/" + version

	// download
	if code := curl.New(url, version, os.TempDir()+DIVIDE+version); code != 0 {
		return code
	}

	return 0
}
示例#7
0
func getLatestVersionByRemote() string {

	var version string

	// set url
	url := config.GetConfig("registry") + "latest/" + util.SHASUMS

	version = util.GetLatestVersion(url)

	return version

}
示例#8
0
func LsRemote() {

	// set url
	url := config.GetConfig(config.REGISTRY) + config.NODELIST

	// try catch
	defer func() {
		if err := recover(); err != nil {
			msg := fmt.Sprintf("'gnvm ls --remote' an error has occurred. please check registry %v. \nError: ", url)
			Error(ERROR, msg, err)
			os.Exit(0)
		}
	}()

	// set exist version
	isExistVersion := false

	// print
	P(DEFAULT, "Read all node.exe version list from %v, please wait.\n", url)

	// get
	code, res, _ := curl.Get(url)
	if code != 0 {
		return
	}
	// close
	defer res.Body.Close()

	writeVersion := func(content string, line int) bool {
		// replace '\n'
		content = strings.Replace(content, "\n", "", -1)

		// split 'vx.xx.xx  1.1.0-alpha-2'
		args := strings.Split(content, " ")

		if ok := util.VerifyNodeVersion(args[0][1:]); ok {
			isExistVersion = true
			P(DEFAULT, args[0], "\n")
		}
		return false
	}

	if err := curl.ReadLine(res.Body, writeVersion); err != nil && err != io.EOF {
		P(ERROR, "%v Error: %v\n", "gnvm ls --remote", err)
	}

}
示例#9
0
/*
 Search Node.js version and Print

 Param:
 	- s: Node.js version, inlcude: *.*.* 0.*.* 0.10.* /<regexp>/ latest 0.10.10

*/
func Search(s string) {
	regex, err := util.FormatWildcard(s, latURL)
	if err != nil {
		P(ERROR, "%v not an %v Node.js version.\n", s, "valid")
		return
	}

	// set url
	url := config.GetConfig(config.REGISTRY)
	if arr := strings.Split(s, "."); len(arr) == 3 {
		if ver, _ := strconv.Atoi(arr[0]); ver >= 1 && ver <= 3 {
			url = config.GetIOURL(url)
		}
	}
	url += util.NODELIST

	// try catch
	defer func() {
		if err := recover(); err != nil {
			msg := fmt.Sprintf("'%v' an error has occurred. please check your input.\nError: ", "gnvm search")
			Error(ERROR, msg, err)
			os.Exit(0)
		}
	}()

	// print
	P(DEFAULT, "Search Node.js version rules [%v] from %v, please wait.\n", s, url)

	// generate nodist
	nodist, err, code := New(url, regex)
	if err != nil {
		if code == -1 {
			P(ERROR, "'%v' get url %v error, Error: %v\n", "gnvm search", url, err)
		} else {
			P(ERROR, "%v an error has occurred. please check. Error: %v\n", "gnvm search", err)
		}
		return
	}

	if len(nodist.nl) > 0 {
		nodist.Detail(0)
	} else {
		P(WARING, "not search any Node.js version details, use rules [%v] from %v.\n", s, url)
	}
}
示例#10
0
文件: npm.go 项目: Kenshin/gnvm
/*
 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
}
示例#11
0
/*
 * return code
 * 0: success
 * 1: remove folder error
 * 2: folder exist
 * 3: create folder error
 *
 */
func download(version string) int {

	// get current os arch
	amd64 := "/"
	if runtime.GOARCH == "amd64" {
		amd64 = "/x64/"
	}

	// rootPath/version/node.exe is exist
	if _, err := util.GetNodeVersion(rootPath + version + DIVIDE); err == nil {
		P(WARING, "%v folder exist.\n", version)
		return 2
	} else {
		if err := os.RemoveAll(rootPath + version); err != nil {
			P(ERROR, "remove %v fail, Error: %v\n", version, err.Error())
			return 1
		}
		//P(DEFAULT, "Remove empty [%v] folder success.\n", version)
	}

	// rootPath/version is exist
	if isDirExist(rootPath+version) != true {
		if err := os.Mkdir(rootPath+version, 0777); err != nil {
			P(ERROR, "create %v fail, Error: %v\n", version, err.Error())
			return 3
		}
	}

	// set url
	url := config.GetConfig(config.REGISTRY) + "v" + version + amd64 + NODE

	// download
	if code := curl.New(url, version, rootPath+version+DIVIDE+NODE); code != 0 {
		if code == -1 {
			if err := os.RemoveAll(rootPath + version); err != nil {
				P(ERROR, "remove %v fail, Error: %v\n", version, err.Error())
				return 1
			}
		}
		return code
	}

	return 0
}
示例#12
0
文件: npm.go 项目: Kenshin/gnvm
/*
 Download / unzip / install npm

 Param:
    - ver: npm version

*/
func downloadNpm(ver string) {
	version := "v" + ver + ZIP
	url := NPMTAOBAO + version
	if config.GetConfig(config.REGISTRY) != util.ORIGIN_TAOBAO {
		url = NPMDEFAULT + version
	}

	// create npm
	npm.New().SetZip(version)

	P(DEFAULT, "Start download new npm version %v\n", version)

	// download
	if err := npm.Download(url, version); err != nil {
		panic(err.Error())
	}

	P(DEFAULT, "Start unzip and install %v zip file, please wait.\n", version)

	// create node_modules
	npm.CreateModules()

	// clean all npm files
	npm.CleanAll()

	// unzip
	if _, err := npm.Unzip(); err != nil {
		msg := fmt.Sprintf("unzip %v an error has occurred. \nError: ", npm.zipname, err.Error())
		panic(errors.New(msg))
	}

	// install
	if err := npm.Install(); err != nil {
		return
	}

	// remove download zip file
	npm.Clean(npm.zippath)

	P(DEFAULT, "Set success, current npm version is %v.\n", ver)
}
示例#13
0
/*
 Print remote Node.js version list

 Param:
 	- limit: print max line
 	- io:    when io == true, print iojs

*/
func LsRemote(limit int, io bool) {
	// set url
	url := config.GetConfig(config.REGISTRY)
	if io {
		url = config.GetIOURL(url)
	}
	url += util.NODELIST

	// try catch
	defer func() {
		if err := recover(); err != nil {
			msg := fmt.Sprintf("'gnvm ls --remote' an error has occurred. please check your input %v. \nError: ", url)
			Error(ERROR, msg, err)
			os.Exit(0)
		}
	}()

	// print
	P(DEFAULT, "Read all Node.js version list from %v, please wait.\n", url)

	// generate nodist
	nodist, err, code := New(url, nil)
	if err != nil {
		if code == -1 {
			P(ERROR, "'%v' get url %v error, Error: %v\n", "gnvm ls -r -d", url, err)
		} else {
			P(ERROR, "%v an error has occurred. please check your input. Error: %v\n", "gnvm ls -r -d", err)
		}
		return
	}

	if limit != -1 {
		nodist.Detail(limit)
	} else {
		for _, v := range nodist.Sorts {
			fmt.Println(v)
		}
	}
}
示例#14
0
文件: command.go 项目: Kenshin/gnvm
			v = util.EqualAbs("npm", v)
			if v == "npm" {
				nodehandle.UninstallNPM()
				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",
示例#15
0
func Update(global bool) {

	// try catch
	defer func() {
		if err := recover(); err != nil {
			Error(ERROR, "'gnvm updte latest' an error has occurred. \nError: ", err)
			os.Exit(0)
		}
	}()

	localVersion := config.GetConfig(config.LATEST_VERSION)
	P(NOTICE, "local latest version is %v.\n", localVersion)

	remoteVersion := getLatestVersionByRemote()
	if remoteVersion == "" {
		P(ERROR, "get latest version error, please check. See '%v'.\n", "gnvm help config")
		return
	}
	P(NOTICE, "remote %v latest version is %v.\n", config.GetConfig("registry"), remoteVersion)

	local, _ := util.ConverFloat(localVersion)
	remote, _ := util.ConverFloat(remoteVersion)

	var args []string
	args = append(args, remoteVersion)

	switch {
	case localVersion == config.UNKNOWN:
		P(WARING, "local latest version is %v.\n", config.UNKNOWN)
		if code := Install(args, global); code == 0 || code == 2 {
			config.SetConfig(config.LATEST_VERSION, remoteVersion)
			P(DEFAULT, "Update latest success, current latest version is %v.\n", remoteVersion)
		}
	case local == remote:

		if 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 if !isDirExist(rootPath + localVersion) {
			P(WARING, "local not exist %v\n", localVersion)
			if code := Install(args, global); code == 0 || code == 2 {
				P(DEFAULT, "Download latest version %v success.\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 registry. See 'gnvm help config'.\n", localVersion, cp, remoteVersion)
	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 := Install(args, global); code == 0 || code == 2 {
			config.SetConfig(config.LATEST_VERSION, remoteVersion)
			P(DEFAULT, "Update latest success, current latest version is %v.\n", remoteVersion)
		}
	}
}
示例#16
0
func InstallNpm() {

	// try catch
	defer func() {
		if err := recover(); err != nil {
			if strings.HasPrefix(err.(string), "CURL Error:") {
				fmt.Printf("\n")
			}
			Error(ERROR, "'gnvm install npm' an error has occurred. \nError: ", err)
			os.Exit(0)
		}
	}()

	out, err := exec.Command(rootPath+"npm", "--version").Output()
	if err == nil {
		P(WARING, "current path %v exist npm, version is %v", rootPath, string(out[:]), "\n")
		return
	}

	url := config.GetConfig(config.REGISTRY) + "npm"

	// get
	code, res, _ := curl.Get(url)
	if code != 0 {
		return
	}
	// close
	defer res.Body.Close()

	maxTime, _ := time.Parse(TIMEFORMART, TIMEFORMART)
	var maxVersion string

	getNpmVersion := func(content string, line int) bool {
		if strings.Index(content, `<a href="`) == 0 && strings.Contains(content, ".zip") {

			// parse
			newLine := strings.Replace(content, `<a href="`, "", -1)
			newLine = strings.Replace(newLine, `</a`, "", -1)
			newLine = strings.Replace(newLine, `">`, " ", -1)

			// e.g. npm-1.3.9.zip npm-1.3.9.zip> 23-Aug-2013 21:14 1535885
			orgArr := strings.Fields(newLine)

			// e.g. npm-1.3.9.zip
			version := orgArr[0:1][0]

			// e.g. 23-Aug-2013 21:14
			sTime := strings.Join(orgArr[2:len(orgArr)-1], " ")

			// bubble sort
			if t, err := time.Parse(TIMEFORMART, sTime); err == nil {
				if t.Sub(maxTime).Seconds() > 0 {
					maxTime = t
					maxVersion = version
				}
			}
		}
		return false
	}

	if err := curl.ReadLine(res.Body, getNpmVersion); err != nil && err != io.EOF {
		P(ERROR, "parse npm version Error: %v, from %v\n", err, url)
		return
	}

	if maxVersion == "" {
		P(ERROR, "get npm version fail from %v, please check. See '%v'.\n", url, "gnvm help config")
		return
	}

	P(NOTICE, "the latest version is %v from %v.\n", maxVersion, config.GetConfig(config.REGISTRY))

	// download zip
	zipPath := os.TempDir() + DIVIDE + maxVersion
	if code := downloadNpm(maxVersion); code == 0 {

		P(DEFAULT, "Start unarchive file %v.\n", maxVersion)

		//unzip(maxVersion)

		if err := zip.UnarchiveFile(zipPath, config.GetConfig(config.NODEROOT), nil); err != nil {
			panic(err)
		}

		P(DEFAULT, "End unarchive.\n")
	}

	// remove temp zip file
	if err := os.RemoveAll(zipPath); err != nil {
		P(ERROR, "remove zip file fail from %v, Error: %v.\n", zipPath, err.Error())
	}

}
示例#17
0
/*
 Install node

 Param:
 	- args  : install Node.js versions, include: x.xx.xx latest x.xx.xx-io-x86 x.xx.xx-x86
 	- global: when global == true, call Use func.

 Return:
 	- code: dl[0].Code, usage 'gnvm update latest'

*/
func InstallNode(args []string, global bool) int {

	localVersion, isLatest, code, dl, ts := "", false, 0, new(curl.Download), new(curl.Task)

	// try catch
	defer func() {
		if err := recover(); err != nil {
			if strings.HasPrefix(err.(string), "CURL Error:") {
				fmt.Printf("\n")
			}
			msg := fmt.Sprintf("'gnvm install %v' an error has occurred. \nError: ", strings.Join(args, " "))
			Error(ERROR, msg, err)
			os.Exit(0)
		}
	}()

	for _, v := range args {
		ver, io, arch, suffix, err := util.ParseNodeVer(v)
		if err != nil {
			switch err.Error() {
			case "1":
				P(ERROR, "%v not node.exe download.\n", v)
			case "2":
				P(ERROR, "%v format error, suffix only must be '%v' or '%v'.\n", v, "x86", "x64")
			case "3":
				P(ERROR, "%v format error, parameter must be '%v' or '%v'.\n", v, "x.xx.xx", "x.xx.xx-x86|x64")
			case "4":
				P(ERROR, "%v not an %v Node.js version.\n", v, "valid")
			case "5":
				P(WARING, "'%v' command is no longer supported. See '%v'.\n", "gnvm install npm", "gnvm help npm")
			}
			continue
		}

		// when os is 386, not download 64 bit node.exe
		if runtime.GOARCH == "386" && suffix == "x64" {
			P(WARING, "current operating system is %v, not support %v suffix.\n", "32-bit", "x64")
			continue
		}

		// check local latest and get remote latest
		v = util.EqualAbs("latest", v)
		if ver == util.LATEST {
			localVersion = config.GetConfig(config.LATEST_VERSION)
			P(NOTICE, "local  latest version is %v.\n", localVersion)

			version := util.GetLatVer(latURL)
			if version == "" {
				P(ERROR, "get latest version error, please check. See '%v'.\n", "gnvm config help")
				break
			}

			isLatest = true
			ver = version
			P(NOTICE, "remote latest version is %v.\n", version)
		} else {
			isLatest = false
		}

		// ture folder name
		if suffix != "" {
			ver += "-" + suffix
		}

		// verify <root>/folder is exist
		folder := rootPath + ver
		if _, err := util.GetNodeVer(folder); err == nil {
			P(WARING, "%v folder exist.\n", ver)
			continue
		}

		// get and set url( include iojs)
		url := config.GetConfig(config.REGISTRY)
		if io {
			url = config.GetIOURL(url)
		}

		// add task
		if url, err := util.GetRemoteNodePath(url, ver, arch); err == nil {
			dl.AddTask(ts.New(url, ver, util.NODE, folder))
		}
	}

	// downlaod
	if len(*dl) > 0 {
		curl.Options.Header = false
		arr := (*dl).GetValues("Title")
		P(DEFAULT, "Start download Node.js versions [%v].\n", strings.Join(arr, ", "))
		newDL, errs := curl.New(*dl)
		for _, task := range newDL {
			v := strings.Replace(task.Dst, rootPath, "", -1)
			if v != localVersion && isLatest {
				config.SetConfig(config.LATEST_VERSION, v)
				P(DEFAULT, "Set success, %v new value is %v\n", config.LATEST_VERSION, v)
			}
			if global && len(args) == 1 {
				if ok := Use(v); ok {
					config.SetConfig(config.GLOBAL_VERSION, v)
				}
			}
		}
		if len(errs) > 0 {
			code = (*dl)[0].Code
			s := ""
			for _, v := range errs {
				s += v.Error()
			}
			P(WARING, s)
		}
	}

	return code
}
示例#18
0
func init() {
	rootPath = util.GlobalNodePath + util.DIVIDE
	latURL = config.GetConfig(config.REGISTRY) + util.LATEST + "/" + util.SHASUMS
}
示例#19
0
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
	err := filepath.Walk(rootPath, func(dir string, f os.FileInfo, err error) error {

		// check nil
		if f == nil {
			return err
		}

		// check dir
		if f.IsDir() == false {
			return nil
		}

		// set version
		version := f.Name()

		// check node version
		if ok := util.VerifyNodeVersion(version); ok {

			// <root>/x.xx.xx/node.exe is exist
			if isDirExist(rootPath + version + DIVIDE + 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"
				}

				// set true
				existVersion = true

				// set lsArr
				lsArr = append(lsArr, version)

				if isPrint {
					if desc == "" {
						P(DEFAULT, "v"+version+desc, "\n")
					} else {
						P(DEFAULT, "%v", "v"+version+desc, "\n")
					}

				}
			}
		}

		// return
		return nil
	})

	// show error
	if err != nil {
		P(ERROR, "'%v' Error: %v.\n", "gnvm ls", err.Error())
		return lsArr, err
	}

	// version is exist
	if !existVersion {
		P(WARING, "don't have any available version, please check. See '%v'.\n", "gnvm help install")
	}

	return lsArr, err
}
示例#20
0
/**
 * rootPath    : node.exe global path,         e.g. x:\xxx\xx\xx\
 *
 * global      : global node.exe version num,  e.g. x.xx.xx-x86 ( only rumtime.GOARCH == "amd64", suffix include: 'x86' and 'x64' )
 * globalPath  : global node.exe version path, e.g. x:\xxx\xx\xx\x.xx.xx-x86
 *
 * newer       : newer node.exe version num,   e.g. x.xx.xx
 * newerPath   : newer node.exe version path,  e.g. <rootPath>\x.xx.xx\
 *
 */
func Use(newer string) bool {

	// try catch
	defer func() {
		if err := recover(); err != nil {
			msg := fmt.Sprintf("'gnvm use %v' an error has occurred. please check. \nError: ", newer)
			Error(ERROR, msg, err)
			os.Exit(0)
		}
	}()

	// get true folder, e.g. folder is latest return x.xx.xx
	util.FormatLatVer(&newer, config.GetConfig(config.LATEST_VERSION), true)
	if newer == util.UNKNOWN {
		P(WARING, "current latest is %v, please usage '%v' first. See '%v'.\n", newer, "gnvm update latest", "gnvm help update")
		return false
	}

	// set newerPath and verify newerPath is exist?
	newerPath := rootPath + newer
	if _, err := util.GetNodeVer(newerPath); err != nil {
		P(WARING, "%v folder is not exist %v, use '%v' get local Node.js version list. See '%v'.\n", newer, "node.exe", "gnvm ls", "gnvm help ls")
		return false
	}

	// get <root>/node.exe version, when exist, get full version, e.g. x.xx.xx-x86
	global, err := util.GetNodeVer(rootPath)
	if err != nil {
		P(WARING, "not found %v Node.js version.\n", "global")
	} else {
		if bit, err := util.Arch(rootPath); err == nil {
			if bit == "x86" && runtime.GOARCH == "amd64" {
				global += "-" + bit
			}
		}
	}

	// check newer is global
	if newer == global {
		P(WARING, "current Node.js version is %v, not re-use. See '%v'.\n", newer, "gnvm node-version")
		return false
	}

	// set globalPath
	globalPath := rootPath + global

	// <root>/global is exist? when not exist, create global folder
	if !util.IsDirExist(globalPath) {
		if err := os.Mkdir(globalPath, 0777); err != nil {
			P(ERROR, "create %v folder Error: %v.\n", global, err.Error())
			return false
		}
	}

	// backup copy <root>/node.exe to <root>/global/node.exe
	if global != "" {
		if err := util.Copy(rootPath, globalPath, util.NODE); err != nil {
			P(ERROR, "copy %v to %v folder Error: %v.\n", rootPath, globalPath, err.Error())
			return false
		}
	}

	// copy <root>/newer/node.exe to <root>/node.exe
	if err := util.Copy(newerPath, rootPath, util.NODE); err != nil {
		P(ERROR, "copy %v to %v folder Error: %v.\n", newerPath, rootPath, err.Error())
		return false
	}

	P(DEFAULT, "Set success, global Node.js version is %v.\n", newer)

	return true
}
示例#21
0
gnvm config registry http://dist.u.qiniudn.com/
gnvm config registry DEFAULT
gnvm config INIT`,
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) == 1 {

			args[0] = util.EqualAbs("registry", args[0])
			args[0] = util.EqualAbs("noderoot", args[0])
			args[0] = util.EqualAbs("INIT", args[0])

			if args[0] == "INIT" {
				config.ReSetConfig()
				return
			}

			P(DEFAULT, "gnvm config %v is %v\n", args[0], config.GetConfig(args[0]))

		} else if len(args) == 2 {
			args[0] = util.EqualAbs("registry", args[0])
			args[0] = util.EqualAbs("noderoot", args[0])
			args[1] = util.EqualAbs("DEFAULT", args[1])
			switch {
			case args[0] == "registry" && args[1] != "DEFAULT":
				if newValue := config.SetConfig(args[0], args[1]); newValue != "" {
					P(DEFAULT, "Set success, %v new value is %v\n", args[0], newValue)
				}
			case args[0] == "registry" && args[1] == "DEFAULT":
				if newValue := config.SetConfig(args[0], config.REGISTRY_VAL); newValue != "" {
					P(DEFAULT, "Reset success, %v new value is %v\n", args[0], newValue)
				}
			case args[0] == "noderoot":
示例#22
0
/*
 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")
		}
	}
}
示例#23
0
/*
 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
}