55processed_imports = set ()
66
77def run (codebase : Codebase ):
8- print ("Processing files in the codebase ..." )
8+ print ("🚀 Starting reexport analysis ..." )
99 for file in codebase .files :
10- print (f"Checking file: { file .filepath } " )
11-
1210 # Only process files under /src/shared
13- if "examples/analize_reexports" not in file .filepath :
14- print ("Skipping file not in the target directory." )
15- continue
16- if '/src/shared' not in file .filepath :
17- print ("Skipping file not in /src/shared." )
11+ if "examples/analize_reexports" not in file .filepath or '/src/shared' not in file .filepath :
1812 continue
1913
14+ print (f"📁 Analyzing: { file .filepath } " )
15+
2016 # Gather all reexports that are not external exports
2117 all_reexports = []
2218 for export_stmt in file .export_statements :
2319 for export in export_stmt .exports :
2420 if export .is_reexport () and not export .is_external_export :
2521 all_reexports .append (export )
2622
27- print (f"Found { len (all_reexports )} reexports in { file .filepath } " )
28-
29- # Skip if there are none
3023 if not all_reexports :
31- print ("No reexports found, moving to the next file." )
3224 continue
3325
26+ print (f"📦 Found { len (all_reexports )} reexports to process" )
27+
3428 for export in all_reexports :
3529 has_wildcard = False
3630
3731 # Replace "src/" with "src/shared/"
3832 resolved_public_file = export .resolved_symbol .filepath .replace ("src/" , "src/shared/" )
39- print (f"Resolved public file path: { resolved_public_file } " )
33+ print (f"🔄 Processing: { export . name } -> { resolved_public_file } " )
4034
4135 # Get relative path from the "public" file back to the original file
4236 relative_path = codebase .get_relative_path (
4337 from_file = resolved_public_file ,
4438 to_file = export .resolved_symbol .filepath
4539 )
46- print (f"Relative path: { relative_path } " )
4740
4841 # Ensure the "public" file exists
4942 if not codebase .has_file (resolved_public_file ):
50- print (f"Creating new file: { resolved_public_file } " )
43+ print (f"✨ Creating new public file: { resolved_public_file } " )
5144 target_file = codebase .create_file (resolved_public_file , sync = True )
5245 else :
53- print (f"File already exists: { resolved_public_file } " )
5446 target_file = codebase .get_file (resolved_public_file )
5547
5648 # If target file already has a wildcard export for this relative path, skip
5749 if target_file .has_export_statement_for_path (relative_path , "WILDCARD" ):
5850 has_wildcard = True
59- print ("Wildcard export already exists, skipping." )
6051 continue
6152
6253 # Compare "public" path to the local file's export.filepath
6354 if codebase ._remove_extension (resolved_public_file ) != codebase ._remove_extension (export .filepath ):
64- print ("Processing export..." )
65-
66- # A) Wildcard export, e.g. `export * from "..."`
55+ # A) Wildcard export
6756 if export .is_wildcard_export ():
6857 target_file .insert_before (f'export * from "{ relative_path } "' )
69- print (f"Inserted wildcard export for { relative_path } " )
58+ print (f"⭐ Added wildcard export for { relative_path } " )
7059
71- # B) Type export, e.g. `export type { Foo, Bar } from "..."`
60+ # B) Type export
7261 elif export .is_type_export ():
7362 statement = file .get_export_statement_for_path (relative_path , "TYPE" )
7463 if statement :
7564 if export .is_aliased ():
7665 statement .insert (0 , f"{ export .resolved_symbol .name } as { export .name } " )
7766 else :
7867 statement .insert (0 , f"{ export .name } " )
79- print (f"Inserted into existing type export statement for { relative_path } " )
68+ print (f"📝 Updated existing type export for { export . name } " )
8069 else :
8170 if export .is_aliased ():
8271 target_file .insert_before (
@@ -87,17 +76,17 @@ def run(codebase: Codebase):
8776 target_file .insert_before (
8877 f'export type {{ { export .name } }} from "{ relative_path } "'
8978 )
90- print (f"Inserted new type export statement for { relative_path } " )
79+ print (f"✨ Added new type export for { export . name } " )
9180
92- # C) Normal export, e.g. `export { Foo, Bar } from "..."`
81+ # C) Normal export
9382 else :
9483 statement = file .get_export_statement_for_path (relative_path , "EXPORT" )
9584 if statement :
9685 if export .is_aliased ():
9786 statement .insert (0 , f"{ export .resolved_symbol .name } as { export .name } " )
9887 else :
9988 statement .insert (0 , f"{ export .name } " )
100- print (f"Inserted into existing export statement for { relative_path } " )
89+ print (f"📝 Updated existing export for { export . name } " )
10190 else :
10291 if export .is_aliased ():
10392 target_file .insert_before (
@@ -108,14 +97,13 @@ def run(codebase: Codebase):
10897 target_file .insert_before (
10998 f'export {{ { export .name } }} from "{ relative_path } "'
11099 )
111- print (f"Inserted new export statement for { relative_path } " )
100+ print (f"✨ Added new export for { export . name } " )
112101
113- # Now update all import usages that refer to this export
102+ # Update import usages
114103 for usage in export .symbol_usages ():
115104 if isinstance (usage , TSImport ) and usage not in processed_imports :
116105 processed_imports .add (usage )
117106
118- # Translate the resolved_public_file to the usage file's TS config import path
119107 new_path = usage .file .ts_config .translate_import_path (resolved_public_file )
120108
121109 if has_wildcard and export .name != export .resolved_symbol .name :
@@ -130,20 +118,20 @@ def run(codebase: Codebase):
130118
131119 usage .file .insert_before (new_import )
132120 usage .remove ()
133- print (f"Updated import in { usage .file .filepath } " )
121+ print (f"🔄 Updated import in { usage .file .filepath } " )
134122
135- # Remove the old export from the original file
123+ # Remove old export
136124 export .remove ()
137- print (f"Removed old export from { export .filepath } " )
125+ print (f"🗑️ Removed old export from { export .filepath } " )
138126
139- # If the file ends up with no exports, remove it entirely
127+ # Clean up empty files
140128 if not file .export_statements and len (file .symbols ) == 0 :
141129 file .remove ()
142- print (f"Removed empty file: { file .filepath } " )
130+ print (f"🧹 Removed empty file: { file .filepath } " )
143131 codebase .commit ()
144132
145133if __name__ == "__main__" :
146- print ("Starting..." )
134+ print ("🎯 Starting reexport organization ..." )
147135 codebase = Codebase ("./" , programming_language = ProgrammingLanguage .TYPESCRIPT )
148136 run (codebase )
149- print ("Done!" )
137+ print ("✅ Done! All reexports organized successfully !" )
0 commit comments