@@ -29,6 +29,14 @@ class GenerateCommand extends Command
29
29
30
30
private $ content = [];
31
31
32
+ /**
33
+ * Imports array where the key is the generated file path and the value is an array of imports.
34
+ * Each import is an array where the first element is the import path and the second element is an array of imported items.
35
+ *
36
+ * @var array<string, array<string, string[]>>
37
+ */
38
+ private $ imports = [];
39
+
32
40
public function __construct (
33
41
private Filesystem $ files ,
34
42
private Router $ router ,
@@ -115,6 +123,12 @@ private function writeContent(): void
115
123
foreach ($ this ->content as $ path => $ content ) {
116
124
$ this ->files ->ensureDirectoryExists (dirname ($ path ));
117
125
$ this ->files ->put ($ path , TypeScript::cleanUp (implode (PHP_EOL , $ content )));
126
+
127
+ // Prepend the imports to the file
128
+ if (isset ($ this ->imports [$ path ])) {
129
+ $ importLines = collect ($ this ->imports [$ path ])->map (fn ($ imports , $ key ) => "import { " .implode (', ' , array_unique ($ imports ))." } from ' {$ key }' " )->implode (PHP_EOL );
130
+ $ this ->files ->prepend ($ path , $ importLines .PHP_EOL );
131
+ }
118
132
}
119
133
120
134
$ this ->content = [];
@@ -216,7 +230,14 @@ private function appendCommonImports(Collection $routes, string $path, string $n
216
230
217
231
$ importBase = str_repeat ('/.. ' , substr_count ($ namespace , '. ' ) + 1 );
218
232
219
- $ this ->appendContent ($ path , 'import { ' .implode (', ' , $ imports )." } from '. {$ importBase }/wayfinder' " .PHP_EOL );
233
+ if (!isset ($ this ->imports [$ path ])) {
234
+ $ this ->imports [$ path ] = [];
235
+ }
236
+
237
+ $ this ->imports [$ path ][". {$ importBase }/wayfinder " ] = [
238
+ ...($ this ->imports [$ path ][". {$ importBase }/wayfinder " ] ?? []),
239
+ ...$ imports
240
+ ];
220
241
}
221
242
222
243
private function writeNamedMethodExport (Route $ route , string $ path ): void
0 commit comments