Skip to content

Commit

Permalink
chore:added version checking
Browse files Browse the repository at this point in the history
  • Loading branch information
urizennnn committed Jun 2, 2024
1 parent 79dc831 commit 13a91af
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
37 changes: 37 additions & 0 deletions lib/cmd/checkVersion.go
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 16 additions & 1 deletion lib/cmd/root.go
Original file line number Diff line number Diff line change
@@ -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{
Expand All @@ -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)
}
Expand Down
16 changes: 16 additions & 0 deletions lib/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@urizen/express-cli",
"version": "2.3.2",
"version": "2.3.3",
"license": "MIT",
"bin": {
"express": "bin/linux"
Expand Down

0 comments on commit 13a91af

Please sign in to comment.