build.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 构建人设图谱可视化
  5. 步骤:
  6. 1. 通过环境变量传递数据路径给 Vite
  7. 2. npm run build 打包 Vue 项目(数据会被内联)
  8. 3. 复制到输出目录
  9. """
  10. import os
  11. import shutil
  12. import subprocess
  13. import sys
  14. from pathlib import Path
  15. # 项目路径
  16. project_root = Path(__file__).parent.parent.parent
  17. sys.path.insert(0, str(project_root))
  18. from script.data_processing.path_config import PathConfig
  19. def main():
  20. config = PathConfig()
  21. viz_dir = Path(__file__).parent
  22. print(f"账号: {config.account_name}")
  23. print()
  24. # 1. 确定数据文件路径
  25. persona_graph_file = config.intermediate_dir / "人设图谱.json"
  26. print(f"数据文件: {persona_graph_file}")
  27. if not persona_graph_file.exists():
  28. print(f"错误: 数据文件不存在!")
  29. sys.exit(1)
  30. # 2. 检查是否需要安装依赖
  31. node_modules = viz_dir / "node_modules"
  32. if not node_modules.exists():
  33. print("\n安装依赖...")
  34. subprocess.run(["npm", "install"], cwd=viz_dir, check=True)
  35. # 3. 构建 Vue 项目(通过环境变量传递数据路径)
  36. print("\n构建 Vue 项目...")
  37. env = os.environ.copy()
  38. env["GRAPH_DATA_PATH"] = str(persona_graph_file.absolute())
  39. subprocess.run(["npm", "run", "build"], cwd=viz_dir, env=env, check=True)
  40. # 4. 复制到输出目录
  41. dist_html = viz_dir / "dist" / "index.html"
  42. output_file = config.intermediate_dir / "人设图谱可视化.html"
  43. print(f"\n输出: {output_file}")
  44. shutil.copy(dist_html, output_file)
  45. print("\n完成!")
  46. if __name__ == "__main__":
  47. main()