https://github.com/sanjeev2552/feature_gen_cli
A Dart CLI tool that generates clean-architecture feature modules for Flutter projects from a JSON schema.
https://github.com/sanjeev2552/feature_gen_cli
clean-architecture cli code-generation
Last synced: 3 months ago
JSON representation
A Dart CLI tool that generates clean-architecture feature modules for Flutter projects from a JSON schema.
- Host: GitHub
- URL: https://github.com/sanjeev2552/feature_gen_cli
- Owner: sanjeev2552
- License: mit
- Created: 2026-02-28T16:40:18.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2026-03-31T15:44:27.000Z (3 months ago)
- Last Synced: 2026-03-31T16:06:20.921Z (3 months ago)
- Topics: clean-architecture, cli, code-generation
- Language: Dart
- Homepage: https://pub.dev/packages/feature_gen_cli
- Size: 124 KB
- Stars: 6
- Watchers: 0
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Feature Gen
[](https://pub.dev/packages/feature_gen_cli) [](https://opensource.org/licenses/MIT) [](https://pub.dev/packages/feature_gen_cli/score)
Feature Gen is a Dart CLI that scaffolds clean-architecture feature modules for Flutter projects from a JSON schema.
## Quick Start
**Requirements:** Dart SDK `>=3.10.4`, a Flutter project with a valid `pubspec.yaml`, and `dart` on your PATH. Run the CLI from the Flutter project root so `pubspec.yaml` resolution works.
```bash
# 1. Install
dart pub global activate feature_gen_cli
# 2. Run
feature_gen_cli
# Example: feature_gen_cli user example/user_schema.json
# Overwrite existing generated files (optional)
feature_gen_cli --overwrite
```
Feature names should be lowercase `snake_case` (the generated folder is `lib/features//`).
### CLI Options
```bash
feature_gen_cli -o # overwrite existing generated files
feature_gen_cli --version # print package version
feature_gen_cli --help # show help
```
## Schema Reference
The schema is a single JSON file requiring three sections: `config`, `api.methods`, and `response`.
### Single-Response (one entity for the whole feature)
```json
{
"config": { "bloc": true, "riverpod": false },
"api": {
"methods": {
"getUser": {},
"updateUser": { "body": { "name": "string", "email": "string" } },
"deleteUser": { "params": { "id": "int" } }
}
},
"response": { "id": "int", "name": "string" }
}
```
### Multi-Response (different entities per method)
When different API methods return different types, define each response type by name and add a `"response"` key to each method that declares which type it returns.
```json
{
"config": { "bloc": true, "riverpod": false },
"api": {
"methods": {
"getUser": { "response": "user" },
"postSomeData": { "body": { "name": "string", "email": "string" }, "response": "token" },
"updateUser": { "body": { "name": "string" }, "response": "user" },
"deleteUser": { "params": { "id": "int" } }
}
},
"response": {
"user": { "id": 123, "name": "string", "email": "string" },
"token": { "accessToken": "string", "refreshToken": "string", "tokenType": "string" }
}
}
```
**Multi-response rules:**
- **Detection** — if all top-level values in `response` are objects, multi-response mode is activated automatically. No extra flag needed.
- **Per-method binding** — set `"response": ""` on any method to link it to a named entity.
- **List returns** — wrap the key in an array to return a list: `"response": ["user"]` → `Future>`.
- **Void methods** — omit `"response"` entirely and the method generates `Future` across all layers.
- **Backward compatible** — schemas with primitive values at the top level of `response` continue to work as single-response.
### Common Options
- **`config`**: Both keys (`bloc` and `riverpod`) are required; exactly one must be `true`.
- **`api.methods`**: Define endpoints (camelCase). Optionally include `params`, `body`, or `query` to generate `UseCase` and param classes.
- **`response`**: Define entity fields. Supported primitives: `"string"`, `"int"`, `"double"`, `"bool"`, `"list"`, `"map"`. Supports nested objects and arrays.
**Naming Convention Note:** `snake_case` keys in your configuration JSON are automatically generated into `camelCase` variables for your data classes, ensuring code idiomacy. Serialization layers accurately map variables back to their exact original JSON keys using Freezed `@JsonKey` configurations and custom overrides automatically.
## Generated Structure
Running the CLI produces a complete clean-architecture module in `lib/features//`:
```
lib/features//
├── data/
│ ├── datasources/ _remote_datasource.dart
│ ├── models/ _model.dart (one per entity in multi-response)
│ └── repositories/ _repository_impl.dart
└── domain/
├── entities/ _entity.dart (one per entity in multi-response)
├── repositories/ _repository.dart
└── usecases/ _usecase.dart
└── presentation/
├── bloc/ _bloc.dart, _event.dart, _state.dart
└── screen/ _screen.dart
```
In **multi-response mode**, the BLoC and Riverpod states get one typed success factory per method:
```dart
// generated user_state.dart
const factory UserState.getUserSuccess(UserEntity data) = _GetUserSuccessState;
const factory UserState.postSomeDataSuccess(TokenEntity data) = _PostSomeDataSuccessState;
const factory UserState.deleteUserSuccess() = _DeleteUserSuccessState;
```
The CLI automatically adds missing dependencies, runs `build_runner`, and formats the generated code.
## Overwrite Behavior
By default, the CLI only generates missing files and will not overwrite existing files. Use `--overwrite` (or `-o`) to force regeneration.
## Troubleshooting
- **Schema validation errors**: Ensure `config`, `api.methods`, and `response` exist, and that exactly one of `config.bloc` or `config.riverpod` is `true`.
- **`build_runner` failed**: Re-run it manually: `dart run build_runner build -d`.
## Support ❤️
If you find this package helpful, please consider giving it a like on [pub.dev](https://pub.dev/packages/feature_gen_cli) and adding a ⭐ star on [GitHub](https://github.com/sanjeev2552/feature_gen)! Your support is greatly appreciated.
## Contributing
- Install dependencies: `dart pub get`
- Run tests: `dart test`
- Format code: `dart format .`
Note: The test suite is pure unit tests and avoids running external commands.
## License
MIT