import os import re import time import shutil # 跳转文件夹 base_dir = "D:\\Workspace\\Vue\\Vue2\\xz_web" os.chdir(base_dir) # 创建打包的out文件夹 out_dir = "out" # 重新创建out目录 shutil.rmtree(out_dir) os.makedirs(out_dir) out_sub_dir = "out\\sub" os.makedirs(out_sub_dir) area_array = [ {"area_name": "日喀则市", "area_id": "640675", "area_code": "540200000000", "folder_name": "rkz"}, {"area_name": "桑珠孜区", "area_id": "640676", "area_code": "540202000000", "folder_name": "szz"}, {"area_name": "南木林县", "area_id": "640865", "area_code": "540221000000", "folder_name": "nml"}, {"area_name": "江孜县", "area_id": "641029", "area_code": "540222000000", "folder_name": "jz"}, {"area_name": "定日县", "area_id": "641204", "area_code": "540223000000", "folder_name": "dr"}, {"area_name": "萨迦县", "area_id": "641393", "area_code": "540224000000", "folder_name": "sj"}, {"area_name": "拉孜县", "area_id": "641512", "area_code": "540225000000", "folder_name": "lz"}, {"area_name": "昂仁县", "area_id": "641622", "area_code": "540226000000", "folder_name": "ar"}, {"area_name": "谢通门县", "area_id": "641825", "area_code": "540227000000", "folder_name": "xtm"}, {"area_name": "白朗县", "area_id": "641940", "area_code": "540228000000", "folder_name": "bl"}, {"area_name": "仁布县", "area_id": "642063", "area_code": "540229000000", "folder_name": "rb"}, {"area_name": "康马县", "area_id": "642146", "area_code": "540230000000", "folder_name": "km"}, {"area_name": "定结县", "area_id": "642203", "area_code": "540231000000", "folder_name": "dj"}, {"area_name": "仲巴县", "area_id": "642284", "area_code": "540232000000", "folder_name": "zb"}, {"area_name": "亚东县", "area_id": "642356", "area_code": "540233000000", "folder_name": "yd"}, {"area_name": "吉隆县", "area_id": "642389", "area_code": "540234000000", "folder_name": "jl"}, {"area_name": "聂拉木县", "area_id": "642437", "area_code": "540235000000", "folder_name": "nlm"}, {"area_name": "萨嘎县", "area_id": "642489", "area_code": "540236000000", "folder_name": "sg"}, {"area_name": "岗巴县", "area_id": "642536", "area_code": "540237000000", "folder_name": "gb"}, ] # 打开文件并处理 common_js_file = f"{base_dir}\\src\\common\\components\\common.js" # 编译 def build(): for area in area_array: area_name = area["area_name"] area_id = area["area_id"] area_code = area["area_code"] folder_name = area["folder_name"] common_js_content = None # 读取文件 with open(common_js_file, "r", encoding="UTF-8") as file: common_js_content = file.read() # 修改区域 common_js_content = re.sub(r"areaName:\s*'.*'", f"areaName: '{area_name}'", common_js_content) common_js_content = re.sub(r"areaId:\s*'.*'", f"areaId: '{area_id}'", common_js_content) common_js_content = re.sub(r"areaCode:\s*'.*'", f"areaCode: '{area_code}'", common_js_content) print(common_js_content) # 保存文件 with open(common_js_file, "w", encoding="UTF-8") as file: file.write(common_js_content) # 编译 os.system("npm run build") # 修改文件夹名 os.rename("dist", folder_name) # 移动到out文件夹 if folder_name == "rkz": shutil.move(f"{folder_name}", f"{out_dir}") else: shutil.move(f"{folder_name}", f"{out_sub_dir}") start_time = int(round(time.time() * 1000)) build() end_time = int(round(time.time() * 1000)) print(f"end, use: {end_time - start_time}ms")