diff --git a/cmd/make.go b/cmd/make.go index 383666c..f426ad8 100644 --- a/cmd/make.go +++ b/cmd/make.go @@ -33,7 +33,7 @@ func RegisterMake(root *cobra.Command) { "make:api": "Generate the Huma REST handler for a model", "make:seeder": "Generate the seeder for a model", "make:factory": "Generate the faker factory for a model", - "make:page": "Generate the Next.js page for a model", + "make:page": "Generate the Next.js page for a model (Next.js frontend only)", "make:test": "Generate a Go feature test (PHPUnit-style) for a model", "make:e2e": "Generate a Playwright e2e spec (Dusk-style) for a model", } { @@ -133,7 +133,7 @@ func makeControllerCmd() *cobra.Command { func makeViewCmd() *cobra.Command { cmd := &cobra.Command{ Use: "make:view ", - Short: "Create Next.js views (page + hook + type) for a model", + Short: "Create Next.js views (page + hook + type) for a model (Next.js frontend only)", GroupID: groupMake, Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { @@ -146,6 +146,10 @@ func makeViewCmd() *cobra.Command { if res == nil { return fmt.Errorf("model %q not found — run `togo make:model %s` first", generator.Pascal(name), name) } + if tanStack(proj) { + skipViews(res.Table) + return nil + } results, err := generator.Execute(opts, generator.ViewPlan(proj.Module, *res)) if err != nil { return err @@ -183,7 +187,11 @@ func makeResourceCmd() *cobra.Command { if _, err := m.Upsert(res, opts.Force); err != nil { return err } - results, err := generator.Execute(opts, generator.ResourcePlan(proj.Module, sqlDriver(proj), res, m)) + plan := generator.ResourcePlan(proj.Module, sqlDriver(proj), res, m) + if tanStack(proj) { + plan = generator.WithoutViews(plan) + } + results, err := generator.Execute(opts, plan) if err != nil { return err } @@ -195,6 +203,9 @@ func makeResourceCmd() *cobra.Command { } ui.Success("Created resource %s", res.Name) ui.Step("next: togo generate && togo migrate && togo serve") + if tanStack(proj) { + ui.Step("page: /admin/%s (the TanStack admin renders every resource)", res.Table) + } } return nil }, @@ -241,7 +252,12 @@ func runSingle(cmd *cobra.Command, verb, name string) error { if res == nil { return fmt.Errorf("model %q not found — run `togo make:model %s` first", generator.Pascal(name), name) } - results, err := generator.Execute(opts, generator.SinglePlan(verb, proj.Module, sqlDriver(proj), *res)) + plan := generator.SinglePlan(verb, proj.Module, sqlDriver(proj), *res) + if tanStack(proj) && len(generator.WithoutViews(plan)) < len(plan) { + skipViews(res.Table) + return nil + } + results, err := generator.Execute(opts, plan) if err != nil { return err } @@ -249,6 +265,16 @@ func runSingle(cmd *cobra.Command, verb, name string) error { return nil } +// tanStack reports whether the project's web app is the TanStack frontend, where +// the Next.js view layer (page, hook, API type) does not apply. +func tanStack(proj *config.Project) bool { + return generator.TanStackFrontend(proj.Root, proj.Frontend.Dir) +} + +func skipViews(table string) { + ui.Info("TanStack frontend: no Next.js views written; the generic admin renders /admin/%s", table) +} + // makeContext loads the project, manifest, and shared options for a make command. func makeContext(cmd *cobra.Command) (*config.Project, *config.Manifest, generator.Options, error) { proj, err := loadProject(cmd) diff --git a/cmd/make_frontend_test.go b/cmd/make_frontend_test.go new file mode 100644 index 0000000..5cb60ca --- /dev/null +++ b/cmd/make_frontend_test.go @@ -0,0 +1,55 @@ +package cmd + +import ( + "os" + "path/filepath" + "testing" + + "github.com/togo-framework/cli/internal/config" +) + +// make:resource in a TanStack app wrote a Next.js page (web/app//page.tsx) +// plus swr hooks the app can't build; the generic /admin/
renders it. +// A Next.js app still gets its views. +func TestMakeResourceViewsFollowFrontend(t *testing.T) { + for _, tc := range []struct { + name, marker string + views bool + }{ + {"tanstack", "web/src/router.tsx", false}, + {"nextjs", "web/next.config.mjs", true}, + } { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + mustWrite(t, filepath.Join(dir, config.ConfigFile), []byte("name: app\nmodule: example.com/app\n")) + marker := filepath.Join(dir, filepath.FromSlash(tc.marker)) + if err := os.MkdirAll(filepath.Dir(marker), 0o755); err != nil { + t.Fatal(err) + } + mustWrite(t, marker, []byte("// frontend\n")) + defer func() { + rootCmd.SetArgs(nil) + _ = rootCmd.PersistentFlags().Set("config", "") + }() + cfg := filepath.Join(dir, config.ConfigFile) + for _, args := range [][]string{ + {"make:resource", "Post", "title:string", "--config", cfg}, + {"make:view", "Post", "--config", cfg}, + {"make:page", "Post", "--config", cfg}, + } { + rootCmd.SetArgs(args) + if err := rootCmd.Execute(); err != nil { + t.Fatalf("%v: %v", args, err) + } + } + for _, f := range []string{"web/app/posts/page.tsx", "web/lib/hooks/usePosts.ts", "web/lib/api/post.ts"} { + if _, err := os.Stat(filepath.Join(dir, filepath.FromSlash(f))); (err == nil) != tc.views { + t.Errorf("%s exists = %v, want %v", f, err == nil, tc.views) + } + } + if _, err := os.Stat(filepath.Join(dir, "internal", "rest", "post_handler.go")); err != nil { + t.Errorf("the API layer must still be generated: %v", err) + } + }) + } +} diff --git a/go.mod b/go.mod index 20c392a..6143fbf 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/gertd/go-pluralize v0.2.1 github.com/spf13/cobra v1.8.1 github.com/spf13/pflag v1.0.5 - github.com/togo-framework/create-togo-app v0.2.3 + github.com/togo-framework/create-togo-app v0.2.4 golang.org/x/term v0.44.0 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index 257114a..cf80f45 100644 --- a/go.sum +++ b/go.sum @@ -8,8 +8,8 @@ github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/togo-framework/create-togo-app v0.2.3 h1:ynnGsBcGcR1wnDFLP6xU+vK6vDbTxeft0wXHcvO2D7Y= -github.com/togo-framework/create-togo-app v0.2.3/go.mod h1:/5nUN9ZyR7df9pSS2pVQDyQcBkzRIbPlsYbJsiH6bI8= +github.com/togo-framework/create-togo-app v0.2.4 h1:oHCRCkRL50Jup4LhoOosP/ys6eNtwilB+79qkH+yJWg= +github.com/togo-framework/create-togo-app v0.2.4/go.mod h1:/5nUN9ZyR7df9pSS2pVQDyQcBkzRIbPlsYbJsiH6bI8= golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw= golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= golang.org/x/term v0.44.0 h1:0rLvDRCtNj0gZkyIXhCyOb2OAzEhLVqc4B+hrsBhrmc= diff --git a/internal/generator/frontend.go b/internal/generator/frontend.go new file mode 100644 index 0000000..4f933c6 --- /dev/null +++ b/internal/generator/frontend.go @@ -0,0 +1,42 @@ +package generator + +import ( + "os" + "path/filepath" +) + +// TanStackFrontend reports whether the project's web app is the TanStack Router +// (Vite) frontend rather than Next.js. That frontend renders every resource with +// its generic /admin/
route, and the make:view files (web/app/
/ +// page.tsx, swr hooks under web/lib) are Next.js-only, so they are not written. +func TanStackFrontend(root, webDir string) bool { + if webDir == "" { + webDir = "web" + } + for _, f := range []string{"src/router.tsx", "vite.config.ts"} { + if _, err := os.Stat(filepath.Join(root, webDir, f)); err == nil { + return true + } + } + return false +} + +// WithoutViews drops the Next.js view layer (make:view targets) from a plan. +func WithoutViews(ops []FileOp) []FileOp { + out := ops[:0:0] + for _, op := range ops { + if !isViewTemplate(op.Template) { + out = append(out, op) + } + } + return out +} + +func isViewTemplate(key string) bool { + for _, t := range viewTargets { + if t.key == key { + return true + } + } + return false +} diff --git a/internal/generator/frontend_test.go b/internal/generator/frontend_test.go new file mode 100644 index 0000000..84d2407 --- /dev/null +++ b/internal/generator/frontend_test.go @@ -0,0 +1,34 @@ +package generator + +import ( + "os" + "path/filepath" + "testing" + + "github.com/togo-framework/cli/internal/config" +) + +func TestTanStackFrontend(t *testing.T) { + dir := t.TempDir() + if TanStackFrontend(dir, "") { + t.Fatal("an empty project is not TanStack (keeps the Next.js views)") + } + if err := os.MkdirAll(filepath.Join(dir, "frontend", "src"), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "frontend", "src", "router.tsx"), nil, 0o644); err != nil { + t.Fatal(err) + } + if !TanStackFrontend(dir, "frontend") || TanStackFrontend(dir, "") { + t.Error("TanStackFrontend should follow the configured frontend dir") + } + ops := WithoutViews(ResourcePlan("example.com/app", "sqlite", config.Resource{Name: "Post", Table: "posts"}, &config.Manifest{})) + for _, op := range ops { + if isViewTemplate(op.Template) { + t.Errorf("WithoutViews kept %s", op.Path) + } + } + if len(ops) == 0 { + t.Error("WithoutViews dropped everything") + } +}