-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.py
More file actions
90 lines (71 loc) · 2.76 KB
/
Copy pathmigrate.py
File metadata and controls
90 lines (71 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
import os
import subprocess
import re
from pathlib import Path
def update_imports(file_path):
"""Update relative imports to @/ aliases in the file."""
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Replace relative imports in from statements with @/ aliases
content = re.sub(r'from\s+[\'"](?:\.\./)+(.+)[\'"]', r'from "@/\1"', content)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
def get_target_path(src_path):
"""Get the target path for App Router."""
path = Path(src_path)
relative_path = path.relative_to('src/pages')
if relative_path.parts[0] == 'api':
# API routes stay in app/api/
return Path('src/app') / relative_path
else:
# Page files
dirname = relative_path.parent
page_name = relative_path.stem # e.g., index, about, [page]
if dirname == Path('.'):
if page_name == 'index':
return Path('src/app/page.tsx')
else:
return Path('src/app') / page_name / 'page.tsx'
else:
if page_name == 'index':
return Path('src/app') / dirname / 'page.tsx'
else:
return Path('src/app') / dirname / page_name / 'page.tsx'
def main():
pages_dir = Path('src/pages')
app_dir = Path('src/app')
if not pages_dir.exists():
print("src/pages/ not found. Nothing to migrate.")
return
# Ensure src/app exists
app_dir.mkdir(parents=True, exist_ok=True)
# Collect all moves
moves = []
for root, dirs, files in os.walk(pages_dir):
for file in files:
if file.endswith(('.tsx', '.ts')):
src_path = Path(root) / file
target_path = get_target_path(src_path)
# Create target directory
target_path.parent.mkdir(parents=True, exist_ok=True)
moves.append((src_path, target_path))
# Execute moves
for src, dst in moves:
print(f"Moving {src} to {dst}")
if dst.exists():
print(f"Warning: {dst} already exists, skipping.")
continue
subprocess.run(['git', 'mv', str(src), str(dst)], check=True)
# Update imports if it's a page file
if dst.suffix == '.tsx':
update_imports(str(dst))
# Clean up empty directories
for root, dirs, files in os.walk(pages_dir, topdown=False):
for dir in dirs:
dir_path = Path(root) / dir
if not list(dir_path.iterdir()):
dir_path.rmdir()
print("Generic migration complete! Review and test the app.")
if __name__ == '__main__':
main()