modurize
Version:
Intelligent CLI tool to scaffold dynamic, context-aware modules for Node.js apps with smart CRUD generation and database integration
308 lines (240 loc) โข 7.67 kB
Markdown
# modurize
**Intelligent CLI tool to scaffold dynamic, context-aware modules for Node.js apps.**
## ๐ Features
- **๐ง Smart Project Analysis**: Automatically detects framework, database, and code patterns
- **๐ฏ Intelligent CRUD Generation**: Creates context-aware modules based on module names
- **๐ Database Integration**: Generates appropriate models for MongoDB, PostgreSQL, MySQL, Prisma
- **๐ Dynamic Templates**: Adapts to your project's existing patterns and conventions
- **โก Interactive Mode**: Guided setup with intelligent defaults
- **๐ง Configuration System**: Project-specific settings via `.modurizerc.json`
- **๐ TypeScript Support**: Full type safety with proper interfaces and types
- **๐งช Testing Templates**: Jest test files with comprehensive coverage
- **๐จ Multiple Frameworks**: Express, Fastify, Koa support
- **๐ Project Analysis**: `modurize analyze` to understand your project
- **๐ซ Prevents Overwriting**: Safe generation with conflict detection
## ๐ฆ Installation
```bash
npm install -g modurize
```
## ๐ ๏ธ Usage
### Basic Usage
```bash
modurize <moduleName> [options]
```
### Project Analysis
```bash
modurize analyze
```
This analyzes your project and provides intelligent recommendations.
### Interactive Mode
```bash
modurize --interactive
# or simply
modurize
```
### Configuration
Create a configuration file for your project:
```bash
modurize init --typescript --tests --class
```
## ๐ Options
- `<moduleName>`: The name of your new module (e.g., `auth`, `user`, `product`)
- `--class`: Generate class-based controllers/services
- `--func`: Generate function-based files (default)
- `--typescript`: Generate TypeScript files (.ts extension)
- `--tests`: Include test files
- `--output <dir>`: Custom output directory (default: src/modules)
- `--interactive`: Use interactive prompts
- `--dry-run`: Show what would be generated without creating files
- `--dynamic`: Use project analysis for intelligent generation (default)
- `--static`: Use static templates (ignore project analysis)
- `--help, -h`: Show help message
- `--version, -v`: Show version
## ๐ง Intelligent Features
### Smart CRUD Generation
modurize analyzes your module name and generates appropriate fields:
```bash
modurize user # Generates user fields (email, name, password, role)
modurize product # Generates product fields (name, price, category, stock)
modurize order # Generates order fields (userId, items, total, status)
modurize category # Generates category fields (name, description, slug)
```
### Database-Aware Models
Automatically generates appropriate models based on detected database:
- **MongoDB**: Mongoose schemas with proper indexing
- **PostgreSQL**: Sequelize models with relationships
- **MySQL**: Sequelize models with MySQL-specific types
- **Prisma**: Prisma schema definitions
- **Generic**: Framework-agnostic interfaces
### Context-Aware Generation
- Detects existing code patterns and follows them
- Matches naming conventions (kebab, camel, pascal)
- Adapts to your project's TypeScript usage
- Follows existing file structure patterns
## ๐ Examples
### Basic Module Generation
```bash
modurize auth
```
Generates intelligent auth module with:
- User authentication fields
- Password validation
- Role-based access control
- Proper error handling
### TypeScript Module with Database
```bash
modurize user --typescript --tests
```
If your project uses MongoDB, generates:
- Mongoose schema with TypeScript interfaces
- Proper indexing for email and name fields
- Virtual fields for ID transformation
- Comprehensive CRUD operations
### Project Analysis
```bash
modurize analyze
```
Output:
```
๐ Project Analysis Results:
Framework: express
Database: mongodb
Code Style: class
TypeScript: Yes
Existing Modules: 3
๐ก Recommendations:
Use TypeScript: Yes
Use Classes: Yes
Include Tests: Yes
Database Integration: Yes
Database Type: mongodb
๐ Next Steps:
Run: modurize <moduleName> --typescript --class --tests
```
## โ๏ธ Configuration
Create a `.modurizerc.json` file in your project root:
```json
{
"defaultStyle": "func",
"defaultOutput": "src/modules",
"useTypeScript": true,
"includeTests": true,
"templates": {
"controller": true,
"service": true,
"routes": true,
"model": true,
"middleware": true,
"validator": true
}
}
```
## ๐งฉ Dynamic Templates
### User Module Example
When you run `modurize user`, it generates:
**Controller with intelligent CRUD:**
```typescript
export class UserController {
static async getAll(req: Request, res: Response): Promise<Response> {
// Smart filtering and pagination
}
static async create(req: Request, res: Response): Promise<Response> {
// Password hashing, email validation
}
static async update(req: Request, res: Response): Promise<Response> {
// Partial updates, validation
}
}
```
**MongoDB Model with proper schema:**
```typescript
const userSchema = new Schema({
email: { type: String, required: true, unique: true },
name: { type: String, required: true },
password: { type: String, required: true },
role: { type: String, required: true, default: 'user' },
isActive: { type: Boolean, required: true, default: true }
}, {
timestamps: true
});
// Smart indexing
userSchema.index({ email: 1 }, { unique: true });
userSchema.index({ name: 1 });
```
**Intelligent Validation:**
```typescript
export function validateUserInput(data: any): boolean {
// Email format validation
// Password strength requirements
// Required field checking
// Type validation
}
```
### Product Module Example
When you run `modurize product`, it generates:
**Product-specific fields:**
```typescript
interface Product {
id?: string;
name: string;
description?: string;
price: number;
category: string;
stock: number;
isActive: boolean;
createdAt?: Date;
updatedAt?: Date;
}
```
**Business logic in service:**
```typescript
export class ProductService {
static async updateStock(id: string, quantity: number) {
// Stock management logic
}
static async findByCategory(category: string) {
// Category filtering
}
}
```
## ๐ Project Analysis Features
### Framework Detection
- Express, Fastify, Koa
- Auto-detects from package.json
### Database Detection
- MongoDB (mongoose, mongodb)
- PostgreSQL (sequelize, pg)
- MySQL (mysql2, mysql)
- SQLite (sqlite3)
- Prisma
### Code Pattern Analysis
- Class vs Function style
- TypeScript usage
- Naming conventions
- File structure patterns
- Existing module analysis
## ๐ฏ Use Cases
### E-commerce Project
```bash
modurize user --typescript --tests
modurize product --typescript --tests
modurize order --typescript --tests
modurize category --typescript --tests
```
### Blog Platform
```bash
modurize post --typescript --tests
modurize author --typescript --tests
modurize comment --typescript --tests
modurize tag --typescript --tests
```
### API Service
```bash
modurize api-key --typescript --tests
modurize endpoint --typescript --tests
modurize rate-limit --typescript --tests
```
## ๐ค Contributing
Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.
## ๐ License
MIT ยฉ Code Singer