upload.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # _*_ coding: utf-8 _*_
  2. # @Time : 2025/7/24 17:15
  3. # @Author : Hank
  4. # @Version:V 0.1
  5. # @File : mini_interface.py
  6. # @desc :
  7. import os
  8. import re
  9. import time
  10. import subprocess
  11. import sys
  12. import platform
  13. # ----------------------- 执行说明 -------------------------
  14. # 单个更新
  15. # python update.py 1 wx1234567890abcdef
  16. # 批量更新,修改update_list
  17. # python update.py 2
  18. # ----------------------- 执行说明 -------------------------
  19. update_list = []
  20. # 更新列表
  21. update_list_hank = [
  22. "wxa7a33088566e1292","wxdabfa5e02785fe9b","wxa13166979d7011c4","wx8a8aae7f462df482","wx1344c35820ac688e","wx8bd40e8ce9deb2b6","wx9e90adce31181f7f","wx9d93825db1c7dcbe","wx3fe2e6c200ec7133","wx3800bb2ed4a326c5","wx60db1bff60cd771c"
  23. ]
  24. # update_list.extend(update_list_hank)
  25. update_list_alex = [
  26. "wxa721f4474e3d548a","wxc5e6b24889027032","wxef68681d5044ca05","wx706acdc696d025ad","wx8288d587b717897e","wx54a8d0e7b33c48c8","wx0138dee6d680c705","wx4420e2d55b0ede27","wx1eb74c589da1731f","wx6b11ce533c3aca92","wx5d607cf4fa663c72","wxbece4f28b6b71271","wxdd1bde4de9f6a6a0","wxbf40c735c0d263a5","wx3f464072571c1fd3","wx092045bdfe26a5ee","wx6c4bb5ef1e6c1e70"
  27. ]
  28. # update_list.extend(update_list_alex)
  29. # print(update_list)
  30. # 配置文件路径
  31. config_file = 'config/config.js'
  32. project_file = 'project.config.json'
  33. project_path = os.getcwd()
  34. # 根据平台设置 CLI 路径
  35. system = platform.system()
  36. if system == "Windows":
  37. cli_tool_path = r"C:\Users\hank\software\微信web开发者工具\cli.bat"
  38. # windows 项目路径
  39. project_path = r"C:\Users\hank\Git\mini_read_space_front"
  40. elif system == "Darwin": # macOS
  41. cli_tool_path = "/Applications/wechatwebdevtools.app/Contents/MacOS/cli"
  42. else:
  43. print(f"[ERROR] 不支持的操作系统: {system}")
  44. sys.exit(1)
  45. def replace_in_file(file_path, pattern, replacement):
  46. with open(file_path, 'r', encoding='utf-8') as file:
  47. content = file.read()
  48. new_content, count = re.subn(pattern, replacement, content)
  49. if count > 0:
  50. with open(file_path, 'w', encoding='utf-8') as file:
  51. file.write(new_content)
  52. else:
  53. print(f"[WARN] No matches found in {file_path} for pattern: {pattern}")
  54. def upload_project(appid, version, description):
  55. print(f"[INFO] 更新 appid: {appid}")
  56. replace_in_file(config_file, r'const current_appid = "[^"]*"', f'const current_appid = "{appid}"')
  57. replace_in_file(project_file, r'"appid":\s*"[^"]*"', f'"appid": "{appid}"')
  58. # 准备上传环境,清除 NODE_OPTIONS(兼容微信开发者工具 CLI)
  59. env = os.environ.copy()
  60. env.pop("NODE_OPTIONS", None) # 删除 NODE_OPTIONS,如果存在
  61. try:
  62. subprocess.run([
  63. cli_tool_path,
  64. "upload",
  65. "--project", project_path,
  66. "-v", version,
  67. "-d", description
  68. ], check=True, env=env)
  69. print(f"[SUCCESS] 上传完成: {appid}\n")
  70. except subprocess.CalledProcessError as e:
  71. print(f"[ERROR] 上传失败: {appid}\n{e}")
  72. def main(version,description):
  73. if len(sys.argv) < 2:
  74. print("用法: python update.py <模式> [appid] <开发者账号>")
  75. print("模式 1: 单个更新,需提供 appid")
  76. print("模式 2: 批量更新,使用 update_list")
  77. print("开发者账号 1:hank")
  78. print("开发者账号 2:alex")
  79. return
  80. mode = sys.argv[1]
  81. if mode == "1":
  82. if len(sys.argv) < 3:
  83. print("请提供 appid,例如: python update.py 1 wx1234567890abcdef")
  84. return
  85. appid = sys.argv[2]
  86. upload_project(appid, version, description)
  87. elif mode == "2":
  88. account = sys.argv[2]
  89. if account == "1":
  90. update_list = update_list_hank
  91. elif account == "2":
  92. update_list = update_list_alex
  93. else:
  94. print("无效开发者账号,只能为 1 或 2")
  95. return
  96. for appid in update_list:
  97. print(f"{appid} 开始更新")
  98. upload_project(appid, version, description)
  99. time.sleep(3)
  100. print(f"{appid} 结束更新\n")
  101. else:
  102. print("无效模式,只能为 1 或 2")
  103. return
  104. if __name__ == "__main__":
  105. version = "1.10.0"
  106. description = "v1.10.0 优化一些问题"
  107. main(version,description)