Skip to content

Commit

Permalink
Compare Node version against nvmrc file, show ⚠️ on mismatch (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScorpion authored Jul 26, 2023
1 parent 581fd02 commit 52eb2d8
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 16 deletions.
30 changes: 30 additions & 0 deletions internal/semver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package internal

import "strings"

func SemVerMatches(current, expected string) bool {
expected = strings.TrimSpace(expected)
if expected == "" {
return true
}

// Strip a leading 'v' from both versions.
if current[0] == 'v' {
current = current[1:]
}
if expected[0] == 'v' {
expected = expected[1:]
}

fullVersionParts := strings.Split(current, ".")
checkParts := strings.Split(expected, ".")

// Check if all the parts of the expected version match the current version.
for i, part := range checkParts {
if i >= len(fullVersionParts) || part != fullVersionParts[i] {
return false
}
}

return true
}
2 changes: 1 addition & 1 deletion internal/versions/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var Docker = section{
},

command: []string{"docker", "-v"},
versionFunc: func(output string) string {
versionFn: func(output string) string {
/*
Example:
Docker version 23.0.1, build a5ee5b1dfc
Expand Down
2 changes: 1 addition & 1 deletion internal/versions/dotnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var Dotnet = section{
},

command: []string{"dotnet", "--version"},
versionFunc: func(output string) string {
versionFn: func(output string) string {
/*
Example:
6.0.114
Expand Down
2 changes: 1 addition & 1 deletion internal/versions/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var Go = section{
},

command: []string{"go", "version"},
versionFunc: func(output string) string {
versionFn: func(output string) string {
/*
Example:
go version go1.20.2 linux/amd64
Expand Down
2 changes: 1 addition & 1 deletion internal/versions/java.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var Java = section{
},

command: []string{"java", "--version"},
versionFunc: func(output string) string {
versionFn: func(output string) string {
/*
Example:
openjdk 19.0.2 2023-01-17
Expand Down
19 changes: 18 additions & 1 deletion internal/versions/node.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package versions

import (
"gokart-prompt/internal"
"gokart-prompt/internal/ansi"
"os"
)

var Node = section{
Expand All @@ -11,18 +13,33 @@ var Node = section{
upsearchFiles: []string{
"node_modules",
"package.json",
".nvmrc",
},
wdFiles: []string{
"*.js",
"*.ts",
},

command: []string{"node", "-v"},
versionFunc: func(output string) string {
versionFn: func(output string) string {
/*
Example:
v19.1.0
*/
return output
},

expectedVersionFn: func() string {
file, found := internal.UpsearchWd([]string{".nvmrc"})
if !found {
return ""
}

bytes, err := os.ReadFile(file)
if err != nil {
return ""
}

return string(bytes)
},
}
2 changes: 1 addition & 1 deletion internal/versions/php.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var Php = section{
},

command: []string{"php", "-v"},
versionFunc: func(output string) string {
versionFn: func(output string) string {
/*
Example:
PHP 8.2.4 (cli) (built: Mar 15 2023 15:27:52) (NTS)
Expand Down
2 changes: 1 addition & 1 deletion internal/versions/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var Python = section{
},

command: []string{"python", "--version"},
versionFunc: func(output string) string {
versionFn: func(output string) string {
/*
Example:
Python 3.10.10
Expand Down
2 changes: 1 addition & 1 deletion internal/versions/ruby.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var Ruby = section{
},

command: []string{"ruby", "-v"},
versionFunc: func(output string) string {
versionFn: func(output string) string {
/*
Example:
ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c5) [x86_64-linux]
Expand Down
2 changes: 1 addition & 1 deletion internal/versions/rust.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var Rust = section{
},

command: []string{"rustc", "--version"},
versionFunc: func(output string) string {
versionFn: func(output string) string {
/*
Example:
rustc 1.69.0 (84c898d65 2023-04-16) (Arch Linux rust 1:1.69.0-2)
Expand Down
23 changes: 16 additions & 7 deletions internal/versions/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
)

type section struct {
symbol string
color ansi.ColorCode
upsearchFiles []string
wdFiles []string
command []string
versionFunc func(output string) string
symbol string
color ansi.ColorCode
upsearchFiles []string
wdFiles []string
command []string
versionFn func(output string) string
expectedVersionFn func() string
}

func (s section) version(wdFiles []string) string {
Expand All @@ -21,7 +22,15 @@ func (s section) version(wdFiles []string) string {
}

if output, ok := internal.Command(s.command[0], s.command[1:]...); ok {
return ansi.Color(s.color, s.symbol+" "+s.versionFunc(output))
curVersion := s.versionFn(output)
str := s.symbol + " " + curVersion

// Check if we know the expected version, and if that matches the found version.
if s.expectedVersionFn != nil && !internal.SemVerMatches(curVersion, s.expectedVersionFn()) {
str += " ⚠️ "
}

return ansi.Color(s.color, str)
}

return ""
Expand Down

0 comments on commit 52eb2d8

Please sign in to comment.