react-state-cli
v1.1.0
Published
π§ Effortless Redux boilerplate generation from initial state files - Generate Redux Toolkit slices, reducers, and TypeScript interfaces automatically
Maintainers
Readme
React State CLI
π§ Effortless Redux boilerplate generation from initial state files
A powerful CLI tool that automatically generates Redux Toolkit slices, reducers, TypeScript interfaces, and state management boilerplate from your initial state definitions. Say goodbye to repetitive Redux setup!
β¨ Features
- π Zero Configuration - Works out of the box with sensible defaults
- π TypeScript First - Generates fully typed Redux code
- π§ Redux Toolkit - Uses modern Redux patterns with RTK
- β‘ Lightning Fast - Instant boilerplate generation
- π― Smart Type Inference - Automatically infers types from your initial state
- π Organized Structure - Generates clean, maintainable file organization
- π Action Creators - Auto-generates setter actions for each state property
π¦ Installation
Global Installation (Recommended)
npm install -g react-state-cliLocal Installation
npm install --save-dev react-state-cliUsing npx (No Installation Required)
npx react-state-cli generate ./src/redux/product/initState.tsπ Quick Start
1. Create your initial state file
Create an initState.ts file that exports a default object:
// src/redux/product/initState.ts
export default {
items: [],
loading: false,
error: null,
selectedId: 0,
filters: {
category: '',
priceRange: [0, 1000]
}
}2. Generate Redux boilerplate
react-state-cli generate ./src/redux/product/initState.ts3. β¨ Magic happens!
The CLI will generate:
src/redux/product/
βββ initState.ts # Your original file
βββ slice.ts # Redux Toolkit slice
βββ reducers.ts # Exported reducer
βββ types.ts # TypeScript interfaces
βββ ...
src/redux/
βββ state.ts # Root state interface (if new)π Generated Code Examples
Generated Slice (slice.ts)
import { createSlice } from '@reduxjs/toolkit'
import { ProductState } from './types'
const initialState: ProductState = {
items: [],
loading: false,
error: null,
selectedId: 0,
filters: {
category: '',
priceRange: [0, 1000]
}
}
const productSlice = createSlice({
name: 'product',
initialState,
reducers: {
setItems: (state, action) => { state.items = action.payload },
setLoading: (state, action) => { state.loading = action.payload },
setError: (state, action) => { state.error = action.payload },
setSelectedId: (state, action) => { state.selectedId = action.payload },
setFilters: (state, action) => { state.filters = action.payload },
}
})
export const { setItems, setLoading, setError, setSelectedId, setFilters } = productSlice.actions
export default productSlice.reducerGenerated Types (types.ts)
export interface ProductState {
items: any[]
loading: boolean
error: any
selectedId: number
filters: object
}Generated Root State (state.ts)
import { ProductState } from './product/types'
export interface RootState {
product: ProductState
}π οΈ CLI Commands
generate
Generates Redux boilerplate from an initial state file.
react-state-cli generate <initStatePath>Arguments:
<initStatePath>- Path to yourinitState.tsfile (relative or absolute)
Examples:
# Relative path
react-state-cli generate ./src/redux/user/initState.ts
# Absolute path
react-state-cli generate /Users/dev/project/src/redux/auth/initState.ts
# Using npx
npx react-state-cli generate ./src/store/products/initState.tsπ Requirements
- Node.js >= 14.0.0
- TypeScript project setup
- Redux Toolkit in your dependencies
π― Use Cases
Perfect for:
- ποΈ New Redux slices - Quickly scaffold new state management
- π Rapid prototyping - Generate boilerplate for quick testing
- π Learning Redux - See best practices in generated code
- π’ Team consistency - Ensure uniform Redux patterns across teams
- β‘ Time-saving - Eliminate repetitive boilerplate writing
Example Project Structure
src/
βββ redux/
β βββ user/
β β βββ initState.ts
β β βββ slice.ts # Generated
β β βββ reducers.ts # Generated
β β βββ types.ts # Generated
β βββ products/
β β βββ initState.ts
β β βββ slice.ts # Generated
β β βββ reducers.ts # Generated
β β βββ types.ts # Generated
β βββ state.ts # Generated root state
βββ components/
βββ pages/βοΈ Configuration
Currently, the CLI uses sensible defaults and doesn't require configuration. Future versions may include:
- Custom template support
- Configurable naming conventions
- Output directory customization
- Advanced type inference options
π€ Contributing
We welcome contributions! Here's how to get started:
Development Setup
# Clone the repository
git clone https://github.com/Utsav-Virani/react-state-cli.git
cd react-state-cli
# Install dependencies
npm install
# Build the project
npm run build
# Link for local development
npm link
# Test your changes
react-state-cli generate ./examples/initState.tsProject Structure
react-state-cli/
βββ src/
β βββ index.ts # CLI entry point
β βββ generate.ts # Core generation logic
β βββ utils.ts # Utility functions
βββ templates/
β βββ slice.ts.tpl # Redux slice template
β βββ reducers.ts.tpl # Reducer export template
β βββ state.ts.tpl # Root state template
βββ dist/ # Compiled output
βββ package.jsonContributing Guidelines
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
π License
MIT License - see the LICENSE file for details.
π Issues & Support
- π Bug reports: GitHub Issues
- π‘ Feature requests: GitHub Issues
- π Documentation: GitHub Wiki
πΊοΈ Roadmap
- [ ] Custom templates - Support for user-defined templates
- [ ] Advanced type inference - Better handling of complex nested objects
- [ ] Multiple slice generation - Batch processing of multiple state files
- [ ] Integration plugins - VS Code extension, IDE plugins
- [ ] Configuration file -
.react-state-cli.jsonfor project settings - [ ] Testing utilities - Generate test files alongside Redux code
π FAQ
Q: Can I use this with existing Redux setups? A: Yes! The CLI generates standard Redux Toolkit code that integrates seamlessly with existing Redux stores.
Q: Does it work with JavaScript projects? A: Currently, the CLI is designed for TypeScript projects. JavaScript support is planned for future releases.
Q: Can I customize the generated code? A: The CLI uses EJS templates that can be modified. Future versions will support custom templates.
Q: What if my initial state has complex nested objects?
A: The CLI handles nested objects but may infer them as object type. You can manually refine the generated types.
Made with β€οΈ by Utsav Virani
Star β this repo if you find it useful!
