@@ -78,7 +78,9 @@ public function handle()
78
78
if (! $ this ->option ('skip-routes ' )) {
79
79
$ this ->files ->deleteDirectory ($ this ->base ());
80
80
81
- $ named = $ routes ->filter (fn (Route $ route ) => $ route ->name () && ! Str::endsWith ($ route ->name (), '. ' ))->groupBy (fn (Route $ route ) => $ route ->name ());
81
+ $ named = $ routes ->filter (
82
+ fn (Route $ route ) => $ route ->name () && ! Str::endsWith ($ route ->name (), '. ' ) && ! Str::startsWith ($ route ->name (), 'generated:: ' )
83
+ )->groupBy (fn (Route $ route ) => $ route ->name ());
82
84
83
85
$ named ->each ($ this ->writeNamedFile (...));
84
86
$ named ->undot ()->each ($ this ->writeBarrelFiles (...));
@@ -98,7 +100,9 @@ private function appendContent($path, $content): void
98
100
{
99
101
$ this ->content [$ path ] ??= [];
100
102
101
- $ this ->content [$ path ][] = $ content ;
103
+ if (! in_array ($ content , $ this ->content [$ path ])) {
104
+ $ this ->content [$ path ][] = $ content ;
105
+ }
102
106
}
103
107
104
108
private function prependContent ($ path , $ content ): void
@@ -112,7 +116,6 @@ private function writeContent(): void
112
116
{
113
117
foreach ($ this ->content as $ path => $ content ) {
114
118
$ this ->files ->ensureDirectoryExists (dirname ($ path ));
115
-
116
119
$ this ->files ->put ($ path , TypeScript::cleanUp (implode (PHP_EOL , $ content )));
117
120
}
118
121
@@ -193,25 +196,15 @@ private function writeControllerMethodExport(Route $route, string $path): void
193
196
194
197
private function writeNamedFile (Collection $ routes , string $ namespace ): void
195
198
{
196
- $ path = join_paths ($ this ->base (), ...explode ('. ' , $ namespace )).'.ts ' ;
199
+ $ parts = explode ('. ' , $ namespace );
200
+ array_pop ($ parts );
201
+ $ parts [] = 'index ' ;
202
+
203
+ $ path = join_paths ($ this ->base (), ...$ parts ).'.ts ' ;
197
204
198
205
$ this ->appendCommonImports ($ routes , $ path , $ namespace );
199
206
200
207
$ routes ->each (fn (Route $ route ) => $ this ->writeNamedMethodExport ($ route , $ path ));
201
-
202
- $ imports = $ routes ->map (fn (Route $ route ) => $ route ->namedMethod ())->implode (', ' );
203
-
204
- $ basename = basename ($ path , '.ts ' );
205
-
206
- $ base = TypeScript::safeMethod ($ basename , 'Route ' );
207
-
208
- if ($ base !== $ imports ) {
209
- $ this ->appendContent ($ path , "const {$ base } = { {$ imports } } \n" );
210
- }
211
-
212
- if ($ base !== 'index ' ) {
213
- $ this ->appendContent ($ path , "export default {$ base }" );
214
- }
215
208
}
216
209
217
210
private function appendCommonImports (Collection $ routes , string $ path , string $ namespace ): void
@@ -224,7 +217,7 @@ private function appendCommonImports(Collection $routes, string $path, string $n
224
217
225
218
$ importBase = str_repeat ('/.. ' , substr_count ($ namespace , '. ' ) + 1 );
226
219
227
- $ this ->appendContent ($ path , 'import { ' .implode (', ' , $ imports )." } from '. {$ importBase }/wayfinder' \n" );
220
+ $ this ->appendContent ($ path , 'import { ' .implode (', ' , $ imports )." } from '. {$ importBase }/wayfinder' " . PHP_EOL );
228
221
}
229
222
230
223
private function writeNamedMethodExport (Route $ route , string $ path ): void
@@ -255,13 +248,25 @@ private function writeBarrelFiles(array|Collection $children, string $parent): v
255
248
256
249
$ indexPath = join_paths ($ this ->base (), $ parent , 'index.ts ' );
257
250
258
- $ childKeys = $ children ->keys ()->mapWithKeys (fn ($ child ) => [$ normalizeToCamelCase ($ child ) => $ child ]);
251
+ $ childKeys = $ children ->keys ()->mapWithKeys (fn ($ child ) => [
252
+ $ child => [
253
+ 'safe ' => TypeScript::safeMethod ($ normalizeToCamelCase ($ child ), 'Method ' ),
254
+ 'normalized ' => $ normalizeToCamelCase ($ child ),
255
+ ],
256
+ ]);
259
257
260
- $ imports = $ childKeys ->filter (fn ($ child , $ key ) => $ key !== 'index ' )->map (fn ($ child , $ key ) => "import {$ key } from './ {$ child }' " )->implode (PHP_EOL );
258
+ if (! ($ this ->content [$ indexPath ] ?? false )) {
259
+ $ imports = $ childKeys ->filter (fn ($ _ , $ key ) => $ key !== 'index ' )->map (fn ($ alias , $ key ) => "import {$ alias ['safe ' ]} from './ {$ key }' " )->implode (PHP_EOL );
260
+ } else {
261
+ $ keysWithGrandkids = $ children ->filter (fn ($ grandChildren ) => ! array_is_list (collect ($ grandChildren )->all ()));
262
+ $ imports = $ childKeys ->only ($ keysWithGrandkids ->keys ())->map (fn ($ alias , $ key ) => "import {$ alias ['safe ' ]} from './ {$ key }' " )->implode (PHP_EOL );
263
+ }
261
264
262
- $ this ->prependContent ($ indexPath , $ imports );
265
+ if ($ imports ) {
266
+ $ this ->prependContent ($ indexPath , $ imports );
267
+ }
263
268
264
- $ keys = $ childKeys ->keys ()-> map (fn ($ key ) => str_repeat (' ' , 4 ).$ key )->implode (', ' .PHP_EOL );
269
+ $ keys = $ childKeys ->map (fn ($ alias , $ key ) => str_repeat (' ' , 4 ).implode ( ' : ' , array_unique ([ $ alias [ ' normalized ' ], $ alias [ ' safe ' ]])) )->implode (', ' .PHP_EOL );
265
270
266
271
$ varExport = $ normalizeToCamelCase (Str::afterLast ($ parent , DIRECTORY_SEPARATOR ));
267
272
0 commit comments