99
1010class GenerateInterfaceUnionsCommand extends BaseCommand
1111{
12- protected $ signature = 'export- interface-unions:generate
12+ protected $ signature = 'synergi-types: interface-unions
1313 {--input=app/Models}
1414 {--output=resources/js/models}
1515 {--format}
1616 {--prettier=} ' ;
1717
18- protected $ description = 'Export models that implement an interface to your frontend . ' ;
18+ protected $ description = 'Export models that implement an interface as TypeScript union types . ' ;
1919
2020 public function __construct (
2121 protected Filesystem $ files
@@ -29,21 +29,19 @@ protected function process(): void
2929
3030 $ interfaces = $ this ->readInterfaces (
3131 path: $ this ->base (),
32- interfaces: config ('export -types.interfaces ' , [])
32+ interfaces: config ('synergi -types.interfaces ' , [])
3333 );
3434
35- $ tsContent = collect ($ interfaces )
36- ->map (function ($ classes , $ interface ) {
37- $ shortInterface = Str::afterLast ($ interface , '\\' );
38- $ classNames = collect ($ classes )
39- ->map (fn ($ class ) => '" ' . addslashes ($ class ) . '" ' )
40- ->implode (' | ' );
41-
42- return "export type {$ shortInterface } = {$ classNames }; " ;
43- })
44- ->implode ("\n\n" );
45-
46- $ tsContent .= "\n\nexport {}; \n" ;
35+ // All interface unions should be exported under App.Interfaces
36+ $ tsContent = "declare namespace App.Interfaces { \n" ;
37+ foreach ($ interfaces as $ interface => $ classes ) {
38+ $ shortInterface = Str::afterLast ($ interface , '\\' );
39+ $ classNames = collect ($ classes )
40+ ->map (fn ($ class ) => '" ' . addslashes ($ class ) . '" ' )
41+ ->implode (' | ' );
42+ $ tsContent .= " export type {$ shortInterface } = {$ classNames }; \n" ;
43+ }
44+ $ tsContent .= "} \n" ;
4745
4846 $ this ->files ->put ($ this ->tsFilePath ($ path ), $ tsContent );
4947 }
@@ -56,11 +54,11 @@ protected function readInterfaces(string $path, array $interfaces)
5654 $ paths = new RecursiveIteratorIterator (new RecursiveDirectoryIterator ($ path ));
5755
5856 $ rootNamespace = $ this ->determineRootNamespace ($ interfaces );
59-
57+
6058 return collect ($ interfaces )
6159 ->mapWithKeys (fn ($ interface ) => [
6260 $ this ->chopStart ($ interface , $ rootNamespace ) => collect ($ paths )
63- ->reject (fn ($ i ) => $ i ->isDir () || str_ends_with ($ i ->getRealPath (), '/.. ' ))
61+ ->reject (fn ($ i ) => ! $ i ->isFile () || ! str_ends_with ($ i ->getRealPath (), '.php ' ))
6462 ->map (fn ($ item ) => $ this ->fqcnFromPath ($ item ->getRealPath ()))
6563 ->filter (fn ($ i ) => is_subclass_of ($ i , $ interface ))
6664 ->values ()
0 commit comments