Skip to content

Commit

Permalink
Check for tsconfig only if any of the TS function
Browse files Browse the repository at this point in the history
  • Loading branch information
veljko-matic committed Jan 26, 2022
1 parent daee14e commit 1778217
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
39 changes: 31 additions & 8 deletions commands/actions/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actions

import (
"fmt"
"github.com/pkg/errors"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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, ""
}
32 changes: 8 additions & 24 deletions commands/util/typescript.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1778217

Please sign in to comment.