diff --git a/artifactory/utils/yarn/configget.go b/artifactory/utils/yarn/configget.go index 48d14c3b1..ab529c5c5 100644 --- a/artifactory/utils/yarn/configget.go +++ b/artifactory/utils/yarn/configget.go @@ -6,7 +6,7 @@ import ( "strings" ) -// This method runs "yarn config set" command and sets the yarn configuration. +// This method runs "yarn config get" command and sets the yarn configuration. func ConfigGet(key, executablePath string, jsonOutput bool) (string, error) { var flags []string = nil if jsonOutput { diff --git a/artifactory/utils/yarn/versionVerify.go b/artifactory/utils/yarn/versionVerify.go new file mode 100644 index 000000000..acebfc922 --- /dev/null +++ b/artifactory/utils/yarn/versionVerify.go @@ -0,0 +1,39 @@ +package yarn + +import ( + gofrogcmd "github.com/jfrog/gofrog/io" + "github.com/jfrog/gofrog/version" + "github.com/jfrog/jfrog-client-go/utils/errorutils" + "strings" +) + +const unsupportedYarnVersion = "4.0.0" + +func IsInstalledYarnVersionSupported(executablePath string) error { + versionGetCmdConfig := getVersionCmdConfig(executablePath) + output, err := gofrogcmd.RunCmdOutput(versionGetCmdConfig) + if err != nil { + return errorutils.CheckError(err) + } + yarnVersion := strings.TrimSpace(output) + return IsVersionSupported(yarnVersion) +} + +func IsVersionSupported(versionStr string) error { + yarnVersion := version.NewVersion(versionStr) + if yarnVersion.Compare(unsupportedYarnVersion) <= 0 { + return errorutils.CheckErrorf("Yarn version 4 is not supported. The current version is: " + versionStr + + ". Please downgrade to a compatible version to continue") + } + return nil +} + +func getVersionCmdConfig(executablePath string) *YarnConfig { + return &YarnConfig{ + Executable: executablePath, + Command: []string{"--version"}, + CommandFlags: nil, + StrWriter: nil, + ErrWriter: nil, + } +} diff --git a/artifactory/utils/yarn/versionVerify_test.go b/artifactory/utils/yarn/versionVerify_test.go new file mode 100644 index 000000000..0160e291f --- /dev/null +++ b/artifactory/utils/yarn/versionVerify_test.go @@ -0,0 +1,26 @@ +package yarn + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestIsVersionSupported(t *testing.T) { + tests := []struct { + versionStr string + expectErr bool + }{ + {"3.9.0", false}, + {"4.0.0", true}, + {"4.1.0", true}, + } + + for _, test := range tests { + err := IsVersionSupported(test.versionStr) + if test.expectErr { + assert.Error(t, err, "Expected an error for version: %s", test.versionStr) + } else { + assert.NoError(t, err, "Did not expect an error for version: %s", test.versionStr) + } + } +}