Example #1
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
}
Example #2
0
			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",