Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions cmd/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
} {
Expand Down Expand Up @@ -133,7 +133,7 @@ func makeControllerCmd() *cobra.Command {
func makeViewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "make:view <Name>",
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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
},
Expand Down Expand Up @@ -241,14 +252,29 @@ 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
}
generator.Report(results, opts.DryRun)
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)
Expand Down
55 changes: 55 additions & 0 deletions cmd/make_frontend_test.go
Original file line number Diff line number Diff line change
@@ -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/<table>/page.tsx)
// plus swr hooks the app can't build; the generic /admin/<table> 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)
}
})
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
42 changes: 42 additions & 0 deletions internal/generator/frontend.go
Original file line number Diff line number Diff line change
@@ -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/<table> route, and the make:view files (web/app/<table>/
// 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
}
34 changes: 34 additions & 0 deletions internal/generator/frontend_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
Loading