68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
import os
|
|
import re
|
|
from pathlib import Path
|
|
|
|
# Base directory
|
|
base_dir = Path("/Users/velikho/Desktop/WORKING/Base/services/iam-service/src")
|
|
|
|
# Find all files that use getErrorMessage
|
|
for root, dirs, files in os.walk(base_dir):
|
|
# Skip test directories
|
|
if '__tests__' in root or 'node_modules' in root:
|
|
continue
|
|
|
|
for file in files:
|
|
if not (file.endswith('.controller.ts') or file.endswith('.middleware.ts') or file.endswith('.service.ts')):
|
|
continue
|
|
if file.endswith('.test.ts') or file.endswith('.e2e.ts'):
|
|
continue
|
|
|
|
filepath = Path(root) / file
|
|
|
|
# Read file content
|
|
with open(filepath, 'r', encoding='utf-8') as f:
|
|
content = f.read()
|
|
|
|
# Check if file uses getErrorMessage
|
|
if 'getErrorMessage(' not in content:
|
|
continue
|
|
|
|
# Check if import already exists
|
|
if re.search(r'import.*getErrorMessage.*from.*error-utils', content):
|
|
continue
|
|
|
|
# Calculate relative path
|
|
rel_path = filepath.relative_to(base_dir)
|
|
depth = len(rel_path.parts) - 1 # -1 for the file itself
|
|
|
|
# Build relative import path
|
|
if depth == 1: # src/middlewares/
|
|
import_path = '../utils/error-utils'
|
|
elif depth == 2: # src/modules/xxx/
|
|
import_path = '../../utils/error-utils'
|
|
elif depth == 3: # src/modules/xxx/yyy/
|
|
import_path = '../../../utils/error-utils'
|
|
else:
|
|
import_path = '../../utils/error-utils' # default
|
|
|
|
# Find the last import line
|
|
lines = content.split('\n')
|
|
last_import_idx = -1
|
|
for i, line in enumerate(lines):
|
|
if line.strip().startswith('import '):
|
|
last_import_idx = i
|
|
|
|
if last_import_idx >= 0:
|
|
# Insert new import after last import
|
|
import_statement = f"import {{ getErrorMessage }} from '{import_path}';"
|
|
lines.insert(last_import_idx + 1, import_statement)
|
|
|
|
# Write back
|
|
with open(filepath, 'w', encoding='utf-8') as f:
|
|
f.write('\n'.join(lines))
|
|
|
|
print(f"Added import to {filepath}")
|
|
|
|
print("Done!")
|