import os
import json

ROOT = "D:/work/绿水青山/清道夫App/src"

# 1. 批量替换文件内容中的路径
for dirpath, _, filenames in os.walk(ROOT):
    for filename in filenames:
        if filename.endswith((".vue", ".ts", ".js", ".json")):
            filepath = os.path.join(dirpath, filename)
            try:
                with open(filepath, "r", encoding="utf-8") as f:
                    content = f.read()
                new_content = content.replace("subPackages/", "subpackages/")
                new_content = new_content.replace("subPackages", "subpackages")
                if new_content != content:
                    with open(filepath, "w", encoding="utf-8") as f:
                        f.write(new_content)
                    print(f"Updated content: {filepath}")
            except Exception as e:
                print(f"Skip {filepath}: {e}")

# 2. 单独处理 pages.json 的 subPackages 字段名需要保留（uni-app 配置字段名必须叫 subPackages）
pages_json = "D:/work/绿水青山/清道夫App/src/pages.json"
with open(pages_json, "r", encoding="utf-8") as f:
    data = json.load(f)

# root 路径改为 subpackages
for pkg in data.get("subPackages", []):
    if "root" in pkg:
        pkg["root"] = pkg["root"].replace("subPackages", "subpackages")

# preloadRule 里的 packages 也改
for rule in data.get("preloadRule", {}).values():
    if "packages" in rule:
        rule["packages"] = [p.replace("subPackages", "subpackages") for p in rule["packages"]]

with open(pages_json, "w", encoding="utf-8") as f:
    json.dump(data, f, ensure_ascii=False, indent=2)
print("Updated pages.json root paths")

print("Done")
