From 17782173bb0850fbf47fa00734049e2bdef8760c Mon Sep 17 00:00:00 2001 From: Veljko Matic Date: Wed, 26 Jan 2022 14:37:06 +0100 Subject: [PATCH] Check for tsconfig only if any of the TS function --- commands/actions/publish.go | 39 +++++++++++++++++++++++++++++-------- commands/util/typescript.go | 32 ++++++++---------------------- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/commands/actions/publish.go b/commands/actions/publish.go index 3816a54..d445f4f 100644 --- a/commands/actions/publish.go +++ b/commands/actions/publish.go @@ -2,6 +2,7 @@ package actions import ( "fmt" + "github.com/pkg/errors" "os" "os/exec" "path/filepath" @@ -95,18 +96,16 @@ func buildFunc(cmd *cobra.Command, args []string) { } mustParseAndValidateTriggers(actions) - existsTsFiles, err := util.TsFilesExists(actions.Sources) - if err != nil { - logrus.Warn("\nError while detecting .ts files in sources") - } tsConfigExists := util.TsConfigExists(actions.Sources) - - if existsTsFiles && !tsConfigExists { + tsFileExists, tsFile := anyFunctionTsFileExists(actions) + if tsFileExists && !tsConfigExists { + err := errors.New(fmt.Sprintf("File %s is a typescript file but there is no typescript config file!", tsFile)) userError.LogErrorf("missing typescript config file %s", userError.NewUserError(err, commands.Colorizer.Sprintf( - "Missing typescript config file in your sources! Sources: %s", - commands.Colorizer.Bold(commands.Colorizer.Red(actions.Sources))), + "Missing typescript config file in your sources! Sources: %s, File: %s", + commands.Colorizer.Bold(commands.Colorizer.Red(actions.Sources)), + commands.Colorizer.Bold(commands.Colorizer.Red(tsFile))), ), ) os.Exit(1) @@ -356,3 +355,27 @@ func mustValidateTsconfig(tsconfig *typescript.TsConfig) { os.Exit(1) } } + +func anyFunctionTsFileExists(actions *actionsModel.ProjectActions) (bool, string) { + for _, spec := range actions.Specs { + locator := spec.Function + internalLocator, err := actionsModel.NewInternalLocator(locator) + if err != nil { + userError.LogErrorf("invalid locator: %s", + userError.NewUserError( + err, + commands.Colorizer.Sprintf( + "Invalid locator format %s.", + commands.Colorizer.Bold(commands.Colorizer.Red(locator)), + ), + ), + ) + os.Exit(1) + } + filePath := filepath.Join(actions.Sources, internalLocator.Path) + if util.IsFileTs(filePath) { + return true, fmt.Sprintf("%s%s", internalLocator.Path, typescript.TsFileExt) + } + } + return false, "" +} diff --git a/commands/util/typescript.go b/commands/util/typescript.go index b7bdb85..8994cdd 100644 --- a/commands/util/typescript.go +++ b/commands/util/typescript.go @@ -1,12 +1,11 @@ package util import ( - "io" - "os" - "path/filepath" - + "fmt" "github.com/tenderly/tenderly-cli/typescript" "github.com/tenderly/tenderly-cli/userError" + "os" + "path/filepath" ) func MustSaveTsConfig(directory string, config *typescript.TsConfig) { @@ -35,27 +34,12 @@ func TsConfigExists(directory string) bool { return ExistFile(filepath.Join(directory, typescript.TsConfigFile)) } -func TsFilesExists(directory string) (bool, error) { - found := false - err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error { - if info.IsDir() { - return nil - } - if filepath.Ext(path) == typescript.TsFileExt { - found = true - // return EOF because we just want to find at least one file with .ts extension - return io.EOF - } - return nil - }) - if err == io.EOF { - err = nil - } - if err != nil { - return false, err +func IsFileTs(filePath string) bool { + if filepath.Ext(filePath) != "" { + return filepath.Ext(filePath) == typescript.TsFileExt } - - return found, nil + filePathWithTsExt := fmt.Sprintf("%s%s", filePath, typescript.TsFileExt) + return ExistFile(filePathWithTsExt) } func MustSavePackageJSON(directory string, config *typescript.PackageJson) {