-
Notifications
You must be signed in to change notification settings - Fork 31
feat: DI - Added first-class Dependency Injection support to FlutterInit with a new wizard option and generated code wiring. #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,7 @@ type TemplateContext = ScaffoldConfig & { | |
| usesDeviceInfoPlus: boolean | ||
| usesAppVersionUpdate: boolean | ||
| usesGeolocator: boolean | ||
| usesGetItDi: boolean | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -98,7 +99,8 @@ export async function generateFlutterScaffold(input: unknown) { | |
| } | ||
| } | ||
|
|
||
| const zipBuffer = await zipDirectory(workingDir) | ||
| const zipRootDir = context.flags.appSlug || "flutter-app" | ||
| const zipBuffer = await zipDirectory(workingDir, zipRootDir) | ||
|
Comment on lines
+102
to
+103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Zip root sanitization is permissive — consider tightening.
🛡️ Suggested hardening- const zipRoot = rootFolderName.trim().replace(/[/\\]/g, "") || "flutter-app"
+ const zipRoot =
+ rootFolderName
+ .trim()
+ .replace(/[/\\:*?"<>|\x00-\x1f]/g, "")
+ .replace(/\.+/g, ".")
+ .replace(/^\.+|\.+$/g, "") || "flutter-app"Also applies to: 321-334 🤖 Prompt for AI Agents |
||
| return zipBuffer | ||
| } finally { | ||
| await fs.rm(workingDir, { recursive: true, force: true }).catch(() => { }) | ||
|
|
@@ -170,6 +172,7 @@ function buildTemplateContext(config: ScaffoldConfig): TemplateContext { | |
| usesDeviceInfoPlus: config.misc.usesDeviceInfoPlus, | ||
| usesAppVersionUpdate: config.misc.usesAppVersionUpdate, | ||
| usesGeolocator: config.misc.usesGeolocator, | ||
| usesGetItDi: config.dependencyInjection === "get_it", | ||
| }, | ||
| } | ||
| } | ||
|
|
@@ -315,8 +318,9 @@ async function copyAndRenderDirectory( | |
| } | ||
| } | ||
|
|
||
| async function zipDirectory(dir: string) { | ||
| async function zipDirectory(dir: string, rootFolderName: string) { | ||
| const zip = new JSZip() | ||
| const zipRoot = rootFolderName.trim().replace(/[/\\]/g, "") || "flutter-app" | ||
|
|
||
| async function walk(current: string) { | ||
| const entries = await fs.readdir(current, { withFileTypes: true }) | ||
|
|
@@ -327,7 +331,7 @@ async function zipDirectory(dir: string) { | |
| await walk(fullPath) | ||
| } else if (entry.isFile()) { | ||
| const data = await fs.readFile(fullPath) | ||
| zip.file(relPath, data) | ||
| zip.file(`${zipRoot}/${relPath}`, data) | ||
| } | ||
| } | ||
| } | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import 'package:{{flags.appSnake}}/src/{{#if (eq architecture "layer-first")}}domain/repositories/{{else if (eq architecture "mvc")}}services/{{else if (eq architecture "mvvm")}}data/repositories/{{else}}features/auth/domain/repositories/{{/if}}auth_repository.dart'; | ||
| import 'package:{{flags.appSnake}}/src/{{#if (eq architecture "layer-first")}}data/repositories/{{else if (eq architecture "mvc")}}services/{{else if (eq architecture "mvvm")}}data/repositories/{{else}}features/auth/data/repositories/{{/if}}auth_repository_impl.dart'; | ||
| {{#if flags.usesGetItDi}} | ||
| import 'package:{{flags.appSnake}}/src/services/auth_service.dart'; | ||
| import 'package:get_it/get_it.dart'; | ||
|
|
||
| final sl = GetIt.instance; | ||
| {{/if}} | ||
|
|
||
| Future<void> initDependencies() async { | ||
| {{#if flags.usesGetItDi}} | ||
| if (!sl.isRegistered<AuthService>()) { | ||
| sl.registerLazySingleton<AuthService>(() => AuthService.instance); | ||
| } | ||
|
|
||
| if (!sl.isRegistered<AuthRepository>()) { | ||
| sl.registerLazySingleton<AuthRepository>( | ||
| () => AuthRepositoryImpl(authService: sl<AuthService>()), | ||
| ); | ||
| } | ||
| {{/if}} | ||
| } | ||
|
|
||
| AuthRepository createAuthRepository() { | ||
| {{#if flags.usesGetItDi}} | ||
| return sl<AuthRepository>(); | ||
| {{else}} | ||
| return AuthRepositoryImpl(); | ||
| {{/if}} | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,9 @@ dependencies: | |
| # State Management | ||
| fpdart: ^1.2.0 | ||
| equatable: ^2.0.7 | ||
| {{#if flags.usesGetItDi}} | ||
| get_it: ^8.0.3 | ||
| {{/if}} | ||
|
Comment on lines
28
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major 🧩 Analysis chain🌐 Web query:
💡 Result: The latest stable version of the get_it Dart/Flutter package on pub.dev is 9.2.1. Citations:
Move
♻️ Proposed fix # State Management
fpdart: ^1.2.0
equatable: ^2.0.7
- {{`#if` flags.usesGetItDi}}
- get_it: ^8.0.3
- {{/if}}
{{`#if` flags.usesFlutterHooks}}
flutter_hooks: ^0.20.5
{{/if}}
@@
{{`#if` flags.isMobX}}
mobx: ^2.6.0
flutter_mobx: ^2.3.0
{{/if}}
+
+ # Dependency Injection
+ {{`#if` flags.usesGetItDi}}
+ get_it: ^9.0.0
+ {{/if}}🤖 Prompt for AI Agents
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @noeljabraham kindly fix this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| {{#if flags.usesFlutterHooks}} | ||
| flutter_hooks: ^0.20.5 | ||
| {{/if}} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid nesting a
<button>inside a<label>(invalid HTML / a11y).The HTML spec disallows interactive descendants other than a single labelable control inside
<label>. Nesting the info<button>inside the label can trigger accessibility warnings and inconsistent click behavior across browsers (some will still activate the radio even withpreventDefault/stopPropagation). Prefer a sibling layout: wrap the row in a<div>and keep the<label>scoped to the radio + text only.♻️ Proposed refactor
📝 Committable suggestion
🤖 Prompt for AI Agents