Skip to content

Commit cb7dadd

Browse files
committed
.
1 parent c9f4252 commit cb7dadd

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Transform Module Re-exports Organization
2+
3+
This example demonstrates how to use Codegen to automatically analyze and reorganize TypeScript module re-exports through shared directories. The script makes this process simple by handling all the tedious manual updates automatically.
4+
5+
> [!NOTE]
6+
> This codemod helps maintain clean module boundaries and improves code organization by centralizing shared exports.
7+
8+
## How the Migration Script Works
9+
10+
The script automates the entire reorganization process in a few key steps:
11+
12+
1. **Export Analysis**
13+
```python
14+
for export_stmt in file.export_statements:
15+
for export in export_stmt.exports:
16+
if export.is_reexport() and not export.is_external_export:
17+
all_reexports.append(export)
18+
```
19+
- Automatically identifies re-exports in shared directories
20+
- Analyzes export patterns and dependencies
21+
- Uses Codegen's intelligent code analysis engine
22+
23+
2. **Shared File Management**
24+
```python
25+
resolved_public_file = export.resolved_symbol.filepath.replace("src/", "src/shared/")
26+
if not codebase.has_file(resolved_public_file):
27+
target_file = codebase.create_file(resolved_public_file, sync=True)
28+
```
29+
- Creates or updates shared export files
30+
- Maintains proper file structure
31+
- Handles path resolution automatically
32+
33+
3. **Import Updates**
34+
```python
35+
# Updates imports to use new shared paths
36+
new_path = usage.file.ts_config.translate_import_path(resolved_public_file)
37+
new_import = f'import {{ {name} }} from "{new_path}"'
38+
```
39+
- Updates all import statements to use new paths
40+
- Maintains proper TypeScript path resolution
41+
- Handles different import types (normal, type)
42+
43+
## Why This Makes Organization Easy
44+
45+
1. **Zero Manual Updates**
46+
- Codegen SDK handles all file creation and updates
47+
- No tedious export management
48+
49+
2. **Consistent Structure**
50+
- Ensures all shared exports follow the same pattern
51+
- Maintains clean module boundaries
52+
53+
3. **Safe Transformations**
54+
- Validates changes before applying them
55+
- Preserves existing functionality
56+
57+
## Common Re-export Patterns
58+
59+
### Module to Shared Exports
60+
```typescript
61+
// Before: Direct module import
62+
import { validateEmail } from '../module_a/src/functions';
63+
64+
// After: Import through shared
65+
import { validateEmail } from '../module_a/src/shared';
66+
```
67+
68+
### Export Consolidation
69+
```typescript
70+
// Before: Multiple export files
71+
export { foo } from './foo';
72+
export { bar } from './bar';
73+
74+
// After: Consolidated in shared
75+
export * from '../functions';
76+
```
77+
78+
## Key Benefits to Note
79+
80+
1. **Better Module Boundaries**
81+
- Clear public API for each module
82+
- Centralized shared functionality
83+
84+
2. **Improved Maintainability**
85+
- Easier to track dependencies
86+
- Simplified import paths
87+
88+
3. **Code Organization**
89+
- Consistent export structure
90+
- Reduced import complexity
91+
92+
93+
The script will:
94+
1. 🎯 Start the reexport organization
95+
2. 📁 Analyze shared directories
96+
3. 🔄 Process and update exports
97+
4. ✨ Create shared export files
98+
5. 🧹 Clean up redundant exports
99+
100+
## Learn More
101+
102+
- [TypeScript Modules](https://www.typescriptlang.org/docs/handbook/modules.html)
103+
- [Export/Import Documentation](https://www.typescriptlang.org/docs/handbook/modules.html#export)
104+
- [Codegen Documentation](https://docs.codegen.com)
105+
- [Tutorial on Analyzing and Organizing Re-exports](https://docs.codegen.com/tutorials/managing-typescript-exports)
106+
- [More on exports ](https://docs.codegen.com/building-with-codegen/exports)
107+
## Contributing
108+
109+
Feel free to submit issues and enhancement requests!

examples/analize_reexports/run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import codegen
12
from codegen import Codebase
23
from codegen.sdk.typescript.file import TSFile, TSImport
34
from codegen.sdk.enums import ProgrammingLanguage
45

56
processed_imports = set()
6-
7+
@codegen.function("reexport_management"
78
def run(codebase: Codebase):
89
print("🚀 Starting reexport analysis...")
910
for file in codebase.files:
@@ -39,7 +40,6 @@ def run(codebase: Codebase):
3940
)
4041

4142
# Ensure the "public" file exists
42-
print(f" resolved_public_file: {resolved_public_file}")
4343
if not codebase.has_file(resolved_public_file):
4444
print(f"✨ Creating new public file: {resolved_public_file}")
4545
target_file = codebase.create_file(resolved_public_file, sync=True)

0 commit comments

Comments
 (0)