file-based-routing-cli
v1.2.3
Published
CLI tool for file-based routing in React projects
Downloads
10
Maintainers
Readme
File-Based Routing CLI for React
A command-line tool that enables file-based routing in React projects, similar to Next.js routing but for standard React applications. This tool automatically generates and manages routes based on your file structure in the pages directory.
Features
🚀 Quick Setup
- Automatically installs and configures
react-router-dom - Sets up the necessary routing infrastructure
- Modifies your
App.jsx/tsxto include routing configuration - Creates a
pagesdirectory for your route components - Supports both JavaScript and TypeScript projects
📁 Advanced File-Based Routing
- Creates routes based on file names in the
pagesdirectory - Supports dynamic routes with bracket notation (
[id].jsx) - Route groups with parentheses notation (
(auth)/login.jsx) - Index routes for clean directory-based routing
- Automatically generates route components with proper naming
- Real-time file watching and route updates
Installation
You can install the package globally:
npm install -g file-based-routing-cliOr use it directly with npx:
npx file-based-routing-cli [command]Usage
Initialize Your Project
fbr initThis command:
- Creates a
pagesdirectory - Installs
react-router-domif not present - Sets up the routing configuration
- Modifies
App.jsx/tsxto include the router
Watch for Changes
fbr watchThis command:
- Watches the
pagesdirectory for file changes - Automatically generates route components for new files
- Updates the routing configuration when files are added or removed
- Provides real-time feedback in the terminal
File Structure Example
your-react-app/
├── src/
│ ├── App.jsx
│ └── routing.jsx (auto-generated)
├── pages/
│ ├── index.jsx → /
│ ├── about.jsx → /about
│ ├── contact.jsx → /contact
│ ├── (auth)/
│ │ ├── login.jsx → /login
│ │ └── register.jsx → /register
│ ├── (dashboard)/
│ │ ├── settings.jsx → /settings
│ │ └── profile.jsx → /profile
│ └── blog/
│ ├── index.jsx → /blog
│ └── [id].jsx → /blog/:idRouting Features
📁 Dynamic Routes
Use bracket notation for dynamic route parameters:
pages/blog/[id].jsx→/blog/:idpages/user/[userId]/posts/[postId].jsx→/user/:userId/posts/:postId
Dynamic route components automatically include useParams hook and parameter display:
// Generated for pages/blog/[id].jsx
import { useParams } from "react-router-dom";
export default function BlogDynamicId() {
const params = useParams();
const id = params.id;
return (
<div>
<h1>BlogDynamicId Page</h1>
<div>
<h2>Route Parameters:</h2>
<p>
<strong>id:</strong> {id}
</p>
</div>
</div>
);
}📂 Route Groups
Use parentheses to group routes without affecting the URL structure:
pages/(auth)/login.jsx→/login(not/auth/login)pages/(dashboard)/settings.jsx→/settings(not/dashboard/settings)pages/(marketing)/about.jsx→/about(not/marketing/about)
Route groups are perfect for organizing related pages while keeping clean URLs.
🏠 Index Routes
Index files create routes for their parent directory:
pages/index.jsx→/(homepage)pages/blog/index.jsx→/blogpages/(auth)/index.jsx→/(route groups are ignored)pages/(dashboard)/settings/index.jsx→/settings
Component Generation
When you create a new file in the pages directory, the CLI automatically generates a component with this structure:
Component Naming
The CLI generates unique component names based on the file path:
pages/about.jsx→Aboutpages/blog/index.jsx→BlogIndexpages/(auth)/login.jsx→AuthLoginpages/blog/[id].jsx→BlogDynamicIdpages/(dashboard)/user/[id].jsx→DashboardUserDynamicId
Requirements
- Node.js 14 or higher
- React project using npm
- React Router DOM v6+
License
MIT
