From b67df20d76583677f4d9b2d1040c43dc49ad6f37 Mon Sep 17 00:00:00 2001 From: Fady Mondy Date: Tue, 13 Oct 2026 15:01:39 +0300 Subject: [PATCH 1/3] fix: TanStack apps get no Next.js views; npm shim installs when postinstall is skipped - make:resource wrote web/app//page.tsx and swr hooks under web/lib into TanStack Router apps, which render resources through the generic /admin/
route. When the web app is TanStack (src/router.tsx or vite.config.ts), make:resource skips the view layer and points at /admin/
; make:view and make:page say so instead of writing files. Next.js apps are unchanged. - The npm shim downloads the binary on first run when the postinstall never ran (npm --ignore-scripts or an allow-scripts gate that blocks), instead of exiting with "binary not found". --- cmd/make.go | 34 +++++++++++++++--- cmd/make_frontend_test.go | 55 +++++++++++++++++++++++++++++ internal/generator/frontend.go | 42 ++++++++++++++++++++++ internal/generator/frontend_test.go | 34 ++++++++++++++++++ npm/bin/togo.js | 25 ++++++++++--- npm/install.js | 13 ++++--- npm/install.test.js | 25 +++++++++++++ 7 files changed, 214 insertions(+), 14 deletions(-) create mode 100644 cmd/make_frontend_test.go create mode 100644 internal/generator/frontend.go create mode 100644 internal/generator/frontend_test.go 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/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") + } +} diff --git a/npm/bin/togo.js b/npm/bin/togo.js index 8cb0dd4..3bd759f 100644 --- a/npm/bin/togo.js +++ b/npm/bin/togo.js @@ -1,13 +1,28 @@ #!/usr/bin/env node // Thin shim: exec the platform `togo` binary (placed in this dir by install.js). +// npm 11 skips dependency install scripts unless allowed (and --ignore-scripts +// always does), so when the postinstall never ran, download the binary on first use. const path = require("path"); const fs = require("fs"); const { spawnSync } = require("child_process"); const bin = path.join(__dirname, process.platform === "win32" ? "togo.exe" : "togo"); -if (!fs.existsSync(bin)) { - console.error("[togo] binary not found. Reinstall: npm install -g @togo-framework/cli"); - process.exit(1); + +function run() { + const r = spawnSync(bin, process.argv.slice(2), { stdio: "inherit" }); + process.exit(r.status == null ? 1 : r.status); +} + +if (fs.existsSync(bin)) { + run(); +} else { + console.error("[togo] binary not installed yet (npm skipped the postinstall script); fetching it now…"); + require("../install.js").install().then((ok) => { + if (ok && fs.existsSync(bin)) return run(); + console.error("[togo] binary not found. Install it with: go install github.com/togo-framework/cli/cmd/togo@latest"); + process.exit(1); + }, (e) => { + console.error(`[togo] install failed: ${e.message}`); + process.exit(1); + }); } -const r = spawnSync(bin, process.argv.slice(2), { stdio: "inherit" }); -process.exit(r.status == null ? 1 : r.status); diff --git a/npm/install.js b/npm/install.js index ae203b5..c7134e4 100644 --- a/npm/install.js +++ b/npm/install.js @@ -3,7 +3,8 @@ * Postinstall: download the prebuilt `togo` binary for this platform from the * matching GitHub release into ./bin. Falls back to `go install` if the download * fails (e.g. an unsupported platform but Go is present). Never hard-fails the - * npm install — a missing binary is reported by bin/togo.js at run time. + * npm install. When npm skips this script (npm 11's allow-scripts gate, + * --ignore-scripts), bin/togo.js runs install() on first use instead. */ const fs = require("fs"); const path = require("path"); @@ -50,10 +51,11 @@ function download(u, dest, redirects = 0) { }); } -async function main() { +// install fetches the binary into ./bin and resolves whether it is now there. +async function install() { if (!PLAT || !ARCH) { console.error(`[togo] no prebuilt binary for ${process.platform}/${process.arch} — install from source: go install github.com/togo-framework/cli/cmd/togo@v${version}`); - process.exit(0); + return false; } fs.mkdirSync(binDir, { recursive: true }); const archive = path.join(binDir, asset); @@ -74,8 +76,9 @@ async function main() { console.error(`[togo] could not install automatically. Download manually: https://github.com/togo-framework/cli/releases/tag/v${version}`); } } + return fs.existsSync(path.join(binDir, binName)); } -if (require.main === module) main(); +if (require.main === module) install(); -module.exports = { tarBin, extract }; +module.exports = { tarBin, extract, install, binDir, binName }; diff --git a/npm/install.test.js b/npm/install.test.js index 8765164..e98783d 100644 --- a/npm/install.test.js +++ b/npm/install.test.js @@ -62,3 +62,28 @@ test("extracts the release .zip on Windows when GNU tar comes first in PATH", assert.strictEqual(fs.readFileSync(path.join(out, "togo.exe"), "utf8"), "binary"); assert.ok(!fs.existsSync(path.join(out, "LICENSE")), "only the binary is extracted"); }); + +// npm 11's allow-scripts gate (or --ignore-scripts) skips the postinstall, so the +// shim must fetch the binary itself on first run, then exec it. The stub +// installer "downloads" a copy of node, which the shim then runs. +test("shim installs the binary on first run when postinstall was skipped", () => { + const pkg = tmp(); + fs.mkdirSync(path.join(pkg, "bin")); + fs.copyFileSync(path.join(__dirname, "bin", "togo.js"), path.join(pkg, "bin", "togo.js")); + const name = process.platform === "win32" ? "togo.exe" : "togo"; + fs.writeFileSync(path.join(pkg, "install.js"), ` +const fs = require("fs"), path = require("path"); +module.exports.install = async () => { + fs.copyFileSync(${JSON.stringify(process.execPath)}, path.join(__dirname, "bin", ${JSON.stringify(name)})); + return true; +};`); + const run = (code) => require("child_process").spawnSync(process.execPath, + [path.join(pkg, "bin", "togo.js"), "-e", `process.exit(${code})`], { encoding: "utf8" }); + + const first = run(7); + assert.strictEqual(first.status, 7, first.stderr); + assert.match(first.stderr, /fetching it now/); + const second = run(3); + assert.strictEqual(second.status, 3, second.stderr); + assert.doesNotMatch(second.stderr, /fetching/, "an installed binary runs directly"); +}); From 417db1eecbc58f2472ebe1d86efda44f9325452f Mon Sep 17 00:00:00 2001 From: Fady Mondy Date: Tue, 13 Oct 2026 15:21:20 +0300 Subject: [PATCH 2/3] revert: keep the npm install mechanism unchanged (decision 8cc7eb28) --- npm/bin/togo.js | 25 +++++-------------------- npm/install.js | 13 +++++-------- npm/install.test.js | 25 ------------------------- 3 files changed, 10 insertions(+), 53 deletions(-) diff --git a/npm/bin/togo.js b/npm/bin/togo.js index 3bd759f..8cb0dd4 100644 --- a/npm/bin/togo.js +++ b/npm/bin/togo.js @@ -1,28 +1,13 @@ #!/usr/bin/env node // Thin shim: exec the platform `togo` binary (placed in this dir by install.js). -// npm 11 skips dependency install scripts unless allowed (and --ignore-scripts -// always does), so when the postinstall never ran, download the binary on first use. const path = require("path"); const fs = require("fs"); const { spawnSync } = require("child_process"); const bin = path.join(__dirname, process.platform === "win32" ? "togo.exe" : "togo"); - -function run() { - const r = spawnSync(bin, process.argv.slice(2), { stdio: "inherit" }); - process.exit(r.status == null ? 1 : r.status); -} - -if (fs.existsSync(bin)) { - run(); -} else { - console.error("[togo] binary not installed yet (npm skipped the postinstall script); fetching it now…"); - require("../install.js").install().then((ok) => { - if (ok && fs.existsSync(bin)) return run(); - console.error("[togo] binary not found. Install it with: go install github.com/togo-framework/cli/cmd/togo@latest"); - process.exit(1); - }, (e) => { - console.error(`[togo] install failed: ${e.message}`); - process.exit(1); - }); +if (!fs.existsSync(bin)) { + console.error("[togo] binary not found. Reinstall: npm install -g @togo-framework/cli"); + process.exit(1); } +const r = spawnSync(bin, process.argv.slice(2), { stdio: "inherit" }); +process.exit(r.status == null ? 1 : r.status); diff --git a/npm/install.js b/npm/install.js index c7134e4..ae203b5 100644 --- a/npm/install.js +++ b/npm/install.js @@ -3,8 +3,7 @@ * Postinstall: download the prebuilt `togo` binary for this platform from the * matching GitHub release into ./bin. Falls back to `go install` if the download * fails (e.g. an unsupported platform but Go is present). Never hard-fails the - * npm install. When npm skips this script (npm 11's allow-scripts gate, - * --ignore-scripts), bin/togo.js runs install() on first use instead. + * npm install — a missing binary is reported by bin/togo.js at run time. */ const fs = require("fs"); const path = require("path"); @@ -51,11 +50,10 @@ function download(u, dest, redirects = 0) { }); } -// install fetches the binary into ./bin and resolves whether it is now there. -async function install() { +async function main() { if (!PLAT || !ARCH) { console.error(`[togo] no prebuilt binary for ${process.platform}/${process.arch} — install from source: go install github.com/togo-framework/cli/cmd/togo@v${version}`); - return false; + process.exit(0); } fs.mkdirSync(binDir, { recursive: true }); const archive = path.join(binDir, asset); @@ -76,9 +74,8 @@ async function install() { console.error(`[togo] could not install automatically. Download manually: https://github.com/togo-framework/cli/releases/tag/v${version}`); } } - return fs.existsSync(path.join(binDir, binName)); } -if (require.main === module) install(); +if (require.main === module) main(); -module.exports = { tarBin, extract, install, binDir, binName }; +module.exports = { tarBin, extract }; diff --git a/npm/install.test.js b/npm/install.test.js index e98783d..8765164 100644 --- a/npm/install.test.js +++ b/npm/install.test.js @@ -62,28 +62,3 @@ test("extracts the release .zip on Windows when GNU tar comes first in PATH", assert.strictEqual(fs.readFileSync(path.join(out, "togo.exe"), "utf8"), "binary"); assert.ok(!fs.existsSync(path.join(out, "LICENSE")), "only the binary is extracted"); }); - -// npm 11's allow-scripts gate (or --ignore-scripts) skips the postinstall, so the -// shim must fetch the binary itself on first run, then exec it. The stub -// installer "downloads" a copy of node, which the shim then runs. -test("shim installs the binary on first run when postinstall was skipped", () => { - const pkg = tmp(); - fs.mkdirSync(path.join(pkg, "bin")); - fs.copyFileSync(path.join(__dirname, "bin", "togo.js"), path.join(pkg, "bin", "togo.js")); - const name = process.platform === "win32" ? "togo.exe" : "togo"; - fs.writeFileSync(path.join(pkg, "install.js"), ` -const fs = require("fs"), path = require("path"); -module.exports.install = async () => { - fs.copyFileSync(${JSON.stringify(process.execPath)}, path.join(__dirname, "bin", ${JSON.stringify(name)})); - return true; -};`); - const run = (code) => require("child_process").spawnSync(process.execPath, - [path.join(pkg, "bin", "togo.js"), "-e", `process.exit(${code})`], { encoding: "utf8" }); - - const first = run(7); - assert.strictEqual(first.status, 7, first.stderr); - assert.match(first.stderr, /fetching it now/); - const second = run(3); - assert.strictEqual(second.status, 3, second.stderr); - assert.doesNotMatch(second.stderr, /fetching/, "an installed binary runs directly"); -}); From 3c41b8af657df162306c53cabb97ea6827309963 Mon Sep 17 00:00:00 2001 From: Fady Mondy Date: Tue, 13 Oct 2026 18:27:08 +0300 Subject: [PATCH 3/3] chore: require create-togo-app v0.2.4 --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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=