first commit
This commit is contained in:
113
frontend/react.py
Normal file
113
frontend/react.py
Normal file
@@ -0,0 +1,113 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
React 项目初始化模块
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
def run_command_interactive(cmd, cwd, description):
|
||||
"""运行命令并支持用户交互"""
|
||||
print(f"\n{'=' * 60}")
|
||||
print(f"执行: {description}")
|
||||
print(f"{'=' * 60}")
|
||||
print(f"💡 提示: 此步骤需要您进行交互操作")
|
||||
print(f"{'=' * 60}\n")
|
||||
|
||||
try:
|
||||
result = subprocess.run(cmd, cwd=cwd, shell=True)
|
||||
|
||||
print(f"\n{'=' * 60}")
|
||||
if result.returncode == 0:
|
||||
print(f"✅ {description} - 完成")
|
||||
else:
|
||||
print(f"❌ {description} - 失败 (错误码: {result.returncode})")
|
||||
print(f"{'=' * 60}\n")
|
||||
|
||||
return result.returncode == 0
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ 执行失败: {str(e)}")
|
||||
return False
|
||||
|
||||
|
||||
def run_command_with_progress(cmd, cwd, description):
|
||||
"""运行命令并显示实时输出"""
|
||||
try:
|
||||
print(f"\n{'=' * 60}")
|
||||
print(f"执行: {description}")
|
||||
print(f"{'=' * 60}")
|
||||
|
||||
process = subprocess.Popen(
|
||||
cmd,
|
||||
cwd=cwd,
|
||||
shell=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
bufsize=0
|
||||
)
|
||||
|
||||
for line in process.stdout:
|
||||
try:
|
||||
decoded_line = line.decode('utf-8', errors='replace')
|
||||
except:
|
||||
try:
|
||||
decoded_line = line.decode('gbk', errors='replace')
|
||||
except:
|
||||
decoded_line = line.decode('latin-1', errors='replace')
|
||||
print(decoded_line, end='')
|
||||
|
||||
process.wait()
|
||||
|
||||
if process.returncode == 0:
|
||||
print(f"\n{'=' * 60}")
|
||||
print(f"✅ {description} - 完成")
|
||||
print(f"{'=' * 60}\n")
|
||||
return True
|
||||
else:
|
||||
print(f"\n{'=' * 60}")
|
||||
print(f"❌ {description} - 失败 (错误码: {process.returncode})")
|
||||
print(f"{'=' * 60}\n")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ 执行失败: {str(e)}")
|
||||
return False
|
||||
|
||||
|
||||
def init_react_project(frontend_dir, project_name):
|
||||
"""初始化 React 项目"""
|
||||
print("\n🚀 初始化 React 项目...")
|
||||
print("\n⚠️ 重要提示:")
|
||||
print(" 1. 当询问 'Use rolldown-vite?' 时,建议选择 No")
|
||||
print(" 2. 当询问 'Install and start now?' 时,请选择 No(否则会启动服务器)")
|
||||
print(" 3. 我们会在之后单独安装依赖\n")
|
||||
|
||||
# 删除空目录,因为 create-vite 需要在不存在的目录中创建
|
||||
frontend_dir.rmdir()
|
||||
|
||||
# 使用 Vite 创建 React 项目
|
||||
cmd = f'npm create vite@latest {frontend_dir.name} -- --template react'
|
||||
success = run_command_interactive(cmd, frontend_dir.parent, "创建 React 项目结构")
|
||||
|
||||
if not success:
|
||||
print("\n💡 提示: 请确保已安装 Node.js 和 npm")
|
||||
print(" 下载地址: https://nodejs.org/")
|
||||
sys.exit(1)
|
||||
|
||||
# 检查目录是否创建成功
|
||||
if not frontend_dir.exists():
|
||||
print("❌ 项目目录创建失败")
|
||||
sys.exit(1)
|
||||
|
||||
# 检查是否已安装依赖
|
||||
node_modules = frontend_dir / "node_modules"
|
||||
if not node_modules.exists():
|
||||
print("\n⏳ 开始安装依赖...")
|
||||
success = run_command_with_progress("npm install", frontend_dir, "安装 React 项目依赖")
|
||||
|
||||
if not success:
|
||||
print("⚠️ 依赖安装失败,请稍后手动运行: cd {}-frontend && npm install".format(project_name))
|
||||
|
||||
print("\n✅ React 项目初始化成功")
|
||||
Reference in New Issue
Block a user