Skip to content

Latest commit

 

History

History
373 lines (300 loc) · 9.97 KB

File metadata and controls

373 lines (300 loc) · 9.97 KB

Ercode CLI

Pub Version License

A powerful Flutter code generator for rapid CRUD module development

FeaturesInstallationQuick StartDocumentationExamples


🚀 Features

Ercode CLI is a command-line tool that automates the creation of Flutter CRUD modules based on simple JSON configurations. Say goodbye to repetitive boilerplate code!

  • Generate Complete Modules: Creates models, repositories, controllers, and views
  • 📝 JSON-Based Config: Simple, declarative module definitions
  • 🏗️ Clean Architecture: Follows best practices and separation of concerns
  • 🎨 Pre-built Widgets: Common UI components included
  • 🔧 Flexible: Generate all or specific components
  • 📦 Auto Setup: Installs dependencies and configures your project
  • 🖼️ Image Support: Built-in image upload handling
  • 🌐 API Ready: Integrated API client with error handling
  • 🎯 NEW: Cubit State Management: Modern reactive state management with Flutter Bloc
  • 💾 NEW: Offline-First: Local caching with Hive for offline support

📦 Installation

# Using Dart
dart pub global activate ercode_cli 

# Or using Flutter
flutter pub global activate ercode_cli

Verify installation:

ercode

🎯 Quick Start

1. Initialize Your Project

ercode init

This will:

  • Install required dependencies (dio, validators, image_picker, etc.)
  • Create helper files and reusable widgets
  • Set up API client and state management
  • Configure your main.dart

2. Configure API Settings

Edit lib/helpers/constants.dart:

const baseUrl = 'https://your-api.com/api';
const apiToken = 'your-token'; // Optional

3. Create a JSON Configuration

Create generator/book.json:

{
  "name": "book",
  "stateManagement": "cubit",
  "offlineFirst": true,
  "fields": [
    {
      "name": "id",
      "type": "int",
      "primary": true,
      "hidden": true
    },
    {
      "name": "cover",
      "type": "String",
      "input": "image",
      "list": true
    },
    {
      "name": "name",
      "type": "String",
      "list": true
    },
    {
      "name": "author",
      "type": "String",
      "list": true
    },
    {
      "name": "genre",
      "type": "String"
    },
    {
      "name": "release_year",
      "type": "int"
    },
    {
      "name": "description",
      "type": "String"
    }
  ]
}

4. Generate Code

ercode generate generator/book.json

Done! 🎉 Your complete CRUD module is ready:

│   ├── book_cubit.dart
│   └── book_state.dart
├── data/
│   ├── book.dart              # Model with JSON serialization
│   └── book_repository.dart   # API integration
└── views/
    ├── book_view.dart         # Reactive BlocBuilder UI
    └── book_view_item.dart

Features included:

  • ✅ Reactive UI with Cubit
  • ✅ Offline-first with local caching
  • ✅ Pull-to-refresh
  • ✅ Error handling:
  • NEW_FEATURES.md - 🎉 Cubit & Offline-First Guide
  • DOCUMENTATION.md - Complete technical documentation
  • TUTORIAL_ID.md - Tutorial lengkap Bahasa Indonesia

Quick links:


## 📖 Documentation

For complete documentation, see **[DOCUMENTATION.md](DOCUMENTATION.md)**

Quick links:
- [Module Configuration](DOCUMENTATION.md#json-configuration)
- [Field Parameters](DOCUMENTATION.md#field-parameters)
- [Advanced Usage](DOCUMENTATION.md#advanced-usage)
- [stateManagement` | ❌ No | stateful | State management: `stateful` or `cubit` |
| `offlineFirst` | ❌ No | false | Enable offline-first with local caching |
| `Examples](DOCUMENTATION.md#examples)
- [Troubleshooting](DOCUMENTATION.md#troubleshooting)

## 📚 Configuration Reference

### Module Parameters

| Parameter | Required | Default | Description |
|-----------|----------|---------|-------------|
| `name` | ✅ Yes | - | Module name (used for folders and files) |
| `api` | ❌ No | Same as name | API endpoint path |
| `modelName` | ❌ No | Same as name | Model class name |
| `only` | ❌ No | [] | Limit generation: `model`, `repository`, `list`, `add`, `detail` |
| `fields` | ✅ Yes | - | Array of field configurations |
Modern App with Cubit + Offline

```json
{
  "name": "product",
  "stateManagement": "cubit",
  "offlineFirst": true,
  "fields": [
    {"name": "id", "type": "int", "primary": true, "hidden": true},
    {"name": "image", "type": "String", "input": "image", "list": true},
    {"name": "name", "type": "String", "list": true},
    {"name": "price", "type": "int", "list": true},
    {"name": "description", "type": "String"}
  ]
}

Simple Blog Module

{stateManagement": "cubit",
  "offlineFirst": true,
  "
  "name": "post",
  "stateManagement": "cubi---------|---------|-------------|
| `name` | ✅ Yes | - | Field name (snake_case recommended) |
| `type` | ❌ No | String | Data type: `String`, `int`, `bool` |
| `input` | ❌ No | text | Input type: `text`, `image` |
| `hidden` | ❌ No | false | Hide from create/edit forms |
| `primary` | ❌ No | false | Mark as primary key (auto-skipped in forms) |
| `list` | ❌ No | false | Show in list view |

## 💡 Examples

### Simple Blog Module

```json
{
  "name": "post",
  "fields": [
    {"name": "id", "type": "int", "primary": true, "hidden": true},
    {"name": "title", "type": "String", "list": true},
    {"name": "content", "type": "String"},
    {"name": "author", "type": "String", "list": true}
  ]
}

E-commerce Product

{
  "name": "product",
  "api": "products/v1",
  "fields": [
    {"name": "id", "type": "int", "primary": true, "hidden": true},
    {"name": "image", "type": "String", "input": "image", "list": true},
    {"name": "name", "type": "String", "list": true},
    {"name": "price", "type": "int", "list": true},
    {"name": "description", "type": "String"},
    {"name": "stock", "type": "int"}
  ]
}

Model Only Generation

{
  "name": "category",
  "only": ["model", "repository"],
  "fields": [
    {"name": "id", "type": "int", "primary": true},
    {"name": "name", "type": "String"}
  ]
}

🛠️ Commands

Cubit (when stateManagement: "cubit")

  • State management with Flutter Bloc
  • Reactive UI with BlocBuilder
  • Clear state definitions (Loading, Loaded, Error, Empty)
  • Separation of business logic and UI

Offline-First (when offlineFirst: true)

  • Auto-caching with Hive
  • Instant loading from local storage
  • Fallback to cache on network error
  • Background sync with server
  • Last sync timestamp tracking

List View

  • Display data with pull-to-refresh
  • Loading and error states
  • Navigation to add/detail views
  • Shows only fields marked with "list": true
  • Reactive UI (with Cubit) or StatefulWidget

Generate Module

ercode generate <path-to-json>

# Examples:
ercode generate generator/book.json
ercode generate config/user.json

# Force overwrite existing files
ercode generate generator/book.json force

🔧 Generated Components

Each module generation creates:

Model

  • Data class with all fields
  • JSON serialization (toJson/fromJson)
  • Map conversion (toMap/fromMap)
  • Automatic timestamp fields (createdAt, updatedAt, deletedAt)

Repository

  • getData() - Fetch list with pagination
  • getDetail(id) - Get single record
  • save(url, params) - Create/Update
  • delete(id) - Delete record

List View

  • Display data with pull-to-refresh
  • Loading and error states
  • Navigation to add/detail views
  • Shows only fields marked with "list": true

Add/Edit View

  • Form with validation
  • Image upload for image fields
  • Auto-populated in edit mode
  • Success/error handling

Detail View

  • Display all field information
  • Edit and delete actions
  • Confirmation dialogs

🏗️ Project Structure

After initialization, your project will have:

lib/
├── helpers/
│   ├── api.dart              # HTTP client wrapper
│   ├── constants.dart        # API configuration
│   ├── state_util.dart       # State management
│   ├── validator.dart        # Form validation
│   └── rb_helpers.dart       # Utilities
├── models/
│   └── response_data.dart    # API response model
├── widgets/
│   ├── edit_text.dart        # Custom text input
│   ├── pick_image.dart       # Image picker
│   ├── loading_layout.dart   # Loading state
│   ├── error_layout.dart     # Error display
│   └── ...                   # More widgets
└── modules/
    └── {your_modules}/       # Generated modules

📋 Requirements

  • Dart SDK >= 2.18.6 < 3.0.0
  • Flutter (latest stable version)

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

📄 License

This project is licensed under the terms included in the LICENSE file.

🔗 Links

📞 Support

If you find this project helpful, please give it a ⭐ on GitHub!

For questions or issues, please open an issue on GitHub.


Made with ❤️ for Flutter developers