diff --git a/lib/cmd/checkVersion.go b/lib/cmd/checkVersion.go new file mode 100644 index 0000000..3ab2417 --- /dev/null +++ b/lib/cmd/checkVersion.go @@ -0,0 +1,37 @@ +package cmd + +import ( + "encoding/json" + "fmt" + ioutil "io" + "net/http" +) + +type PackageInfo struct { + Version string `json:"version"` +} + +func GetCurrentVersionFromNpm(packageName string) (string, error) { + url := fmt.Sprintf("https://registry.npmjs.org/%s/latest", packageName) + resp, err := http.Get(url) + if err != nil { + return "", err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("failed to fetch package info: %s", resp.Status) + } + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + + var info PackageInfo + if err := json.Unmarshal(body, &info); err != nil { + return "", err + } + + return info.Version, nil +} diff --git a/lib/cmd/root.go b/lib/cmd/root.go index c3647ec..4dc014d 100644 --- a/lib/cmd/root.go +++ b/lib/cmd/root.go @@ -1,9 +1,11 @@ package cmd import ( + "fmt" "os" "github.com/spf13/cobra" + "github.com/urizennnn/express-cli/lib/functions/config" ) var rootCmd = &cobra.Command{ @@ -15,10 +17,23 @@ var rootCmd = &cobra.Command{ // Run: func(cmd *cobra.Command, args []string) { }, } +const ( + packageName = "@urizen/express-cli" +) + +var linstalledVersion = Version() + // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { - err := rootCmd.Execute() + version, err := GetCurrentVersionFromNpm(packageName) + if err != nil { + os.Exit(1) + } + if version != linstalledVersion { + fmt.Printf(config.Red+"A new version of %s is available. Run `npm install -g %s` to update.\n"+config.Reset, packageName, packageName) + } + err = rootCmd.Execute() if err != nil { os.Exit(1) } diff --git a/lib/cmd/version.go b/lib/cmd/version.go index 063c097..1de26f6 100644 --- a/lib/cmd/version.go +++ b/lib/cmd/version.go @@ -36,6 +36,22 @@ func getRootDir() (string, error) { return "/usr/lib/node_modules/@urizen/express-cli", nil } +func Version() string { + data, err := getRootDir() + errors.Check_Err(err) + var file string + if PLATFORM == "windows" { + file = data + "\\version.js" + } else { + file = data + "/version.js" + } + cleaned_File := filepath.Clean(file) + version, err := exec.Command("node", cleaned_File).Output() + errors.Check_Err(err) + + return string(version) +} + func printVersion() { data, err := getRootDir() errors.Check_Err(err) diff --git a/package.json b/package.json index b741b01..56c460d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@urizen/express-cli", - "version": "2.3.2", + "version": "2.3.3", "license": "MIT", "bin": { "express": "bin/linux"