@undermuz/tanstack-router-generator
v1.0.1
Published
CLI tool that sets up TanStack Router in React or Nx projects.
Downloads
197
Maintainers
Readme
@undermuz/tanstack-router-generator
A lightweight CLI tool that automatically sets up TanStack Router in any React or Nx-based project. It installs required dependencies, generates routing structure, and configures a ready-to-use router setup.
🚀 Features
- Adds TanStack Router to any React project
- Generates a complete routing structure:
- Root layout
- Index page
- Nested page example
- Router configuration
- Router provider
<RouterProvider />
- Updates
package.jsonwith required dependencies - Works in Nx and non-Nx environments
- Zero configuration required
🛠 Usage
Initialize TanStack Router
Run the generator inside your React project:
# Simple react project
npx @undermuz/tanstack-router-generator@latest init
# NX-like project
npx @undermuz/tanstack-router-generator@latest init --project=./apps/web-app/src/appBy default, it installs routing files into:
<cwd>/src/app
<cwd>/package.jsonTo specify a custom path:
npx @undermuz/tanstack-router-generator init --project=apps/web-app/src/app
<cwd>/apps/web-app/src/app
<cwd>/package.jsonAdd a new route
After initialization, you can add new routes using:
npx @undermuz/tanstack-router-generator add-route <name>
<cwd>/src/app/routes/<name>Examples:
# Add a "settings" route (uses default src/app)
npx @undermuz/tanstack-router-generator add-route settings
# Add a "user-profile" route
npx @undermuz/tanstack-router-generator add-route user-profile
# Specify custom app path (routes directory will be created inside)
npx @undermuz/tanstack-router-generator add-route dashboard --project=apps/web-app/src/appThe command will:
- Create a new directory for the route inside the app's
routes/directory - Generate
page.tsxcomponent - Generate
routes.tsxwith route configuration - Automatically update the main
index.tsxto include the new route
📁 What gets generated
Initial structure
routes/
provider.tsx
layout.tsx
page.tsx
index.tsx
nested-page/
routes.tsx
page.tsxAdding new routes
When you add a new route using add-route, the following files are created:
routes/
my-new-route/
page.tsx # Route component
routes.tsx # Route configuration
index.tsx # (automatically updated)📚 Dependencies added automatically
- @tanstack/react-router
- @tanstack/react-router-devtools
- framer-motion
🧩 Requirements
- Node.js 18+
- npm or yarn
- React project (Vite, CRA, Nx, etc.)
📦 Installation (local development)
Clone the repository and link it globally:
npm install
npm linkNow the CLI is available system-wide:
tanstack-router-generator🧪 Testing
npm testRuns unit tests with Vitest. Tests cover:
- copyFiles — copying templates without overwriting existing files
- updatePackageJson — adding TanStack Router dependencies to
package.json - addRoute — route creation, name normalization,
index.tsxupdates, and error validation
Watch mode (re-run on file changes):
npm run test:watchCoverage report:
npm run test:coverage📄 License
MIT
