@@ -98,7 +98,9 @@ private function appendContent($path, $content): void
98
98
{
99
99
$ this ->content [$ path ] ??= [];
100
100
101
- $ this ->content [$ path ][] = $ content ;
101
+ if (! in_array ($ content , $ this ->content [$ path ])) {
102
+ $ this ->content [$ path ][] = $ content ;
103
+ }
102
104
}
103
105
104
106
private function prependContent ($ path , $ content ): void
@@ -112,7 +114,6 @@ private function writeContent(): void
112
114
{
113
115
foreach ($ this ->content as $ path => $ content ) {
114
116
$ this ->files ->ensureDirectoryExists (dirname ($ path ));
115
-
116
117
$ this ->files ->put ($ path , TypeScript::cleanUp (implode (PHP_EOL , $ content )));
117
118
}
118
119
@@ -193,25 +194,15 @@ private function writeControllerMethodExport(Route $route, string $path): void
193
194
194
195
private function writeNamedFile (Collection $ routes , string $ namespace ): void
195
196
{
196
- $ path = join_paths ($ this ->base (), ...explode ('. ' , $ namespace )).'.ts ' ;
197
+ $ parts = explode ('. ' , $ namespace );
198
+ array_pop ($ parts );
199
+ $ parts [] = 'index ' ;
200
+
201
+ $ path = join_paths ($ this ->base (), ...$ parts ).'.ts ' ;
197
202
198
203
$ this ->appendCommonImports ($ routes , $ path , $ namespace );
199
204
200
205
$ 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
206
}
216
207
217
208
private function appendCommonImports (Collection $ routes , string $ path , string $ namespace ): void
@@ -224,7 +215,7 @@ private function appendCommonImports(Collection $routes, string $path, string $n
224
215
225
216
$ importBase = str_repeat ('/.. ' , substr_count ($ namespace , '. ' ) + 1 );
226
217
227
- $ this ->appendContent ($ path , 'import { ' .implode (', ' , $ imports )." } from '. {$ importBase }/wayfinder' \n" );
218
+ $ this ->appendContent ($ path , 'import { ' .implode (', ' , $ imports )." } from '. {$ importBase }/wayfinder' " . PHP_EOL );
228
219
}
229
220
230
221
private function writeNamedMethodExport (Route $ route , string $ path ): void
@@ -255,13 +246,25 @@ private function writeBarrelFiles(array|Collection $children, string $parent): v
255
246
256
247
$ indexPath = join_paths ($ this ->base (), $ parent , 'index.ts ' );
257
248
258
- $ childKeys = $ children ->keys ()->mapWithKeys (fn ($ child ) => [$ normalizeToCamelCase ($ child ) => $ child ]);
249
+ $ childKeys = $ children ->keys ()->mapWithKeys (fn ($ child ) => [
250
+ $ child => [
251
+ 'safe ' => TypeScript::safeMethod ($ normalizeToCamelCase ($ child ), 'Method ' ),
252
+ 'normalized ' => $ normalizeToCamelCase ($ child ),
253
+ ],
254
+ ]);
259
255
260
- $ imports = $ childKeys ->filter (fn ($ child , $ key ) => $ key !== 'index ' )->map (fn ($ child , $ key ) => "import {$ key } from './ {$ child }' " )->implode (PHP_EOL );
256
+ if (! ($ this ->content [$ indexPath ] ?? false )) {
257
+ $ imports = $ childKeys ->filter (fn ($ _ , $ key ) => $ key !== 'index ' )->map (fn ($ alias , $ key ) => "import {$ alias ['safe ' ]} from './ {$ key }' " )->implode (PHP_EOL );
258
+ } else {
259
+ $ keysWithGrandkids = $ children ->filter (fn ($ grandChildren ) => ! array_is_list (collect ($ grandChildren )->all ()));
260
+ $ imports = $ childKeys ->only ($ keysWithGrandkids ->keys ())->map (fn ($ alias , $ key ) => "import {$ alias ['safe ' ]} from './ {$ key }' " )->implode (PHP_EOL );
261
+ }
261
262
262
- $ this ->prependContent ($ indexPath , $ imports );
263
+ if ($ imports ) {
264
+ $ this ->prependContent ($ indexPath , $ imports );
265
+ }
263
266
264
- $ keys = $ childKeys ->keys ()-> map (fn ($ key ) => str_repeat (' ' , 4 ).$ key )->implode (', ' .PHP_EOL );
267
+ $ keys = $ childKeys ->map (fn ($ alias , $ key ) => str_repeat (' ' , 4 ).implode ( ' : ' , array_unique ([ $ alias [ ' normalized ' ], $ alias [ ' safe ' ]])) )->implode (', ' .PHP_EOL );
265
268
266
269
$ varExport = $ normalizeToCamelCase (Str::afterLast ($ parent , DIRECTORY_SEPARATOR ));
267
270
0 commit comments