Files
opc-web/scratch/replace_alerts.py

27 lines
1.1 KiB
Python
Raw Normal View History

import os
import re
def replace_alerts(directory):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".vue"):
path = os.path.join(root, file)
with open(path, 'r', encoding='utf-8') as f:
content = f.read()
if "alert(" in content:
# Check if ElMessage is already imported
if "import { ElMessage } from 'element-plus'" not in content:
# Add import after <script setup...
content = re.sub(r'(<script setup.*?>)', r'\1\nimport { ElMessage } from "element-plus";', content)
# Replace alert('msg') with ElMessage.info('msg')
# This is simple and might need refinement for complex cases
content = re.sub(r'alert\((.*?)\)', r'ElMessage.info(\1)', content)
with open(path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"Updated {path}")
replace_alerts("src")