From 54f2e769f348c55b33b5a34e8fd463f3de590544 Mon Sep 17 00:00:00 2001 From: rafaelDev0ps Date: Mon, 11 Nov 2024 10:06:13 -0300 Subject: [PATCH] Check project directories --- cli/htmgo/tasks/astgen/entry.go | 42 +++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/cli/htmgo/tasks/astgen/entry.go b/cli/htmgo/tasks/astgen/entry.go index d53dc85..1644f5e 100644 --- a/cli/htmgo/tasks/astgen/entry.go +++ b/cli/htmgo/tasks/astgen/entry.go @@ -2,13 +2,9 @@ package astgen import ( "fmt" - "github.com/maddalax/htmgo/cli/htmgo/internal/dirutil" - "github.com/maddalax/htmgo/cli/htmgo/tasks/process" - "github.com/maddalax/htmgo/framework/h" "go/ast" "go/parser" "go/token" - "golang.org/x/mod/modfile" "io/fs" "log/slog" "os" @@ -16,6 +12,11 @@ import ( "slices" "strings" "unicode" + + "github.com/maddalax/htmgo/cli/htmgo/internal/dirutil" + "github.com/maddalax/htmgo/cli/htmgo/tasks/process" + "github.com/maddalax/htmgo/framework/h" + "golang.org/x/mod/modfile" ) type Page struct { @@ -479,9 +480,42 @@ func writeAssetsFile() { } +func HasModuleFile(path string) bool { + _, err := os.Stat(path) + return !os.IsNotExist(err) +} + +func CheckProjectDirectories(path string) error { + pagesPath := filepath.Join(path, "pages") + _, err := os.Stat(pagesPath) + if err != nil { + return fmt.Errorf("The directory pages does not exist.") + } + + partialsPath := filepath.Join(path, "partials") + _, err = os.Stat(partialsPath) + if err != nil { + return fmt.Errorf("The directory partials does not exist.") + } + + return nil +} + func GetModuleName() string { wd := process.GetWorkingDir() modPath := filepath.Join(wd, "go.mod") + + if HasModuleFile(modPath) == false { + fmt.Fprintf(os.Stderr, "Module not found: go.mod file does not exist.") + return "" + } + + checkDir := CheckProjectDirectories(wd) + if checkDir != nil { + fmt.Fprintf(os.Stderr, checkDir.Error()) + return "" + } + goModBytes, err := os.ReadFile(modPath) if err != nil { fmt.Fprintf(os.Stderr, "error reading go.mod: %v\n", err)