Check project directories

This commit is contained in:
rafaelDev0ps 2024-11-11 10:06:13 -03:00
parent b3834bf559
commit 54f2e769f3

View file

@ -2,13 +2,9 @@ package astgen
import ( import (
"fmt" "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/ast"
"go/parser" "go/parser"
"go/token" "go/token"
"golang.org/x/mod/modfile"
"io/fs" "io/fs"
"log/slog" "log/slog"
"os" "os"
@ -16,6 +12,11 @@ import (
"slices" "slices"
"strings" "strings"
"unicode" "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 { 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 { func GetModuleName() string {
wd := process.GetWorkingDir() wd := process.GetWorkingDir()
modPath := filepath.Join(wd, "go.mod") 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) goModBytes, err := os.ReadFile(modPath)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "error reading go.mod: %v\n", err) fmt.Fprintf(os.Stderr, "error reading go.mod: %v\n", err)