HBuilderX Vanilla 通过简洁的 REST API 提供构建服务,所有接口均基于 HTTP GET 请求。
基础信息
- 基础 URL:
http://localhost:13300(默认端口) - 请求方式: GET
- 响应格式: JSON
接口列表
健康检查
检查服务运行状态。
GET /
响应示例:
{
"status": "ok",
"message": "HBuilderX Vanilla API is running",
"timestamp": "2024-01-15T10:30:00Z"
}
构建项目
构建指定项目的 wgt 热更新包。
GET /build?project=<project_name>&vueVersion=<version>
请求参数:
| 参数名 | 类型 | 必需 | 默认值 | 说明 |
|---|---|---|---|---|
project | string | ✅ | - | 项目文件夹名称 |
vueVersion | string | ❌ | 3 | Vue 版本,可选值:2 或 3 |
请求示例:
# 构建Vue3项目
curl "http://localhost:13300/build?project=my-app&vueVersion=3"
# 构建Vue2项目
curl "http://localhost:13300/build?project=legacy-app&vueVersion=2"
# 使用默认Vue版本(3)
curl "http://localhost:13300/build?project=my-app"
使用示例
Shell 脚本
#!/bin/bash
# 配置
API_BASE="http://localhost:13300"
PROJECT_NAME="my-uni-app"
VUE_VERSION="3"
# 检查服务状态
echo "检查服务状态..."
response=$(curl -s "$API_BASE/")
if [[ $? -eq 0 ]]; then
echo "✅ 服务运行正常"
else
echo "❌ 服务不可用"
exit 1
fi
# 构建项目
echo "开始构建项目: $PROJECT_NAME"
response=$(curl -s "$API_BASE/build?project=$PROJECT_NAME&vueVersion=$VUE_VERSION")
# 解析响应
status=$(echo $response | jq -r '.status')
if [[ "$status" == "success" ]]; then
echo "✅ 构建成功"
output_path=$(echo $response | jq -r '.data.outputPath')
echo "📦 输出路径: $output_path"
else
echo "❌ 构建失败"
error_message=$(echo $response | jq -r '.message')
echo "错误信息: $error_message"
exit 1
fi
Python 脚本
import requests
import json
class HBuilderXVanillaClient:
def __init__(self, base_url="http://localhost:13300"):
self.base_url = base_url
def health_check(self):
"""检查服务健康状态"""
try:
response = requests.get(f"{self.base_url}/")
return response.json()
except Exception as e:
return {"status": "error", "message": str(e)}
def build_project(self, project_name, vue_version="3"):
"""构建项目"""
params = {
"project": project_name,
"vueVersion": vue_version
}
try:
response = requests.get(f"{self.base_url}/build", params=params)
return response.json()
except Exception as e:
return {"status": "error", "message": str(e)}
# 使用示例
client = HBuilderXVanillaClient()
# 检查服务状态
health = client.health_check()
print(f"服务状态:{health['status']}")
# 构建项目
result = client.build_project("my-app", "3")
if result["status"] == "success":
print(f"✅ 构建成功:{result['data']['outputPath']}")
else:
print(f"❌ 构建失败:{result['message']}")
Node.js 脚本
const axios = require('axios')
class HBuilderXVanillaClient {
constructor(baseUrl = 'http://localhost:13300') {
this.baseUrl = baseUrl
}
async healthCheck() {
try {
const response = await axios.get(`${this.baseUrl}/`)
return response.data
}
catch (error) {
return { status: 'error', message: error.message }
}
}
async buildProject(projectName, vueVersion = '3') {
try {
const response = await axios.get(`${this.baseUrl}/build`, {
params: {
project: projectName,
vueVersion
}
})
return response.data
}
catch (error) {
return { status: 'error', message: error.message }
}
}
}
// 使用示例
(async () => {
const client = new HBuilderXVanillaClient()
// 检查服务状态
const health = await client.healthCheck()
console.log(`服务状态:${health.status}`)
// 构建项目
const result = await client.buildProject('my-app', '3')
if (result.status === 'success') {
console.log(`✅ 构建成功:${result.data.outputPath}`)
}
else {
console.log(`❌ 构建失败:${result.message}`)
}
})()
最佳实践
1. 超时设置
构建过程可能需要较长时间,建议设置合适的超时时间:
# 设置60秒超时
curl --max-time 60 "$API_BASE/build?project=$PROJECT"
2. 并发控制
避免同时构建多个项目,这可能导致资源竞争和构建失败。
3. 日志记录
在生产环境中,建议记录所有 API 调用和响应,便于问题排查:
echo "[$(date)] 开始构建项目: $PROJECT" >> build.log
response=$(curl -s "$API_BASE/build?project=$PROJECT")
echo "[$(date)] 构建响应: $response" >> build.log