Skip to content

Commit 2a6b552

Browse files
author
guomingliang
committed
fix:支持uni-launch和hbuilderx命令
1 parent 6b76516 commit 2a6b552

File tree

4 files changed

+259
-97
lines changed

4 files changed

+259
-97
lines changed

README.md

Lines changed: 145 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,102 +12,216 @@
1212

1313
## 🛠️ 安装
1414

15+
### 全局安装(推荐)
16+
17+
```bash
18+
npm install -g @dcloudio/hbuilderx-cli
19+
```
20+
21+
全局安装后,可以直接在任何目录使用 `hbuilderx``uni-launch``uni-logcat``uni-test` 命令。
22+
1523
### 本地安装
1624

1725
```bash
1826
npm install @dcloudio/hbuilderx-cli --save-dev
1927
```
2028

29+
本地安装后,可以通过 npm scripts 使用,或在项目目录中使用 `npx` 命令。
30+
2131
## 🚀 快速开始
2232

2333
### 1. 环境准备
2434

25-
确保您已经安装了 **HBuilderX 4.87 或更高版本**。HBuilderX-cli 会自动检测已启动的 HBuilderX 进程。
26-
27-
> **⚠️ 版本要求**:本工具需要 HBuilderX 4.87 及以上版本,低于此版本将无法正常使用。
35+
确保您已经安装了 **HBuilderX**。HBuilderX-cli 会自动检测已启动的 HBuilderX 进程。
2836

2937
## 📖 命令使用
3038

31-
<!-- ### 开发命令 (uni-launch)
39+
### 直接调用 CLI (hbuilderx)
40+
41+
`hbuilderx` 命令是一个通用的 HBuilderX CLI 包装器,可以直接传递任何参数给 HBuilderX CLI。如果 HBuilderX 未运行,会自动先启动 HBuilderX。
42+
43+
```bash
44+
# 全局安装后,直接使用命令
45+
hbuilderx --version
46+
hbuilderx project open --path /path/to/project
47+
hbuilderx launch web --project /path/to/project
48+
hbuilderx logcat web --project /path/to/project
49+
50+
# 或者通过 npm scripts 使用(本地安装时)
51+
# 在 package.json 中配置:
52+
# "scripts": {
53+
# "hbuilderx": "hbuilderx"
54+
# }
55+
npm run hbuilderx -- --version
56+
npm run hbuilderx -- project open --path /path/to/project
57+
```
58+
59+
> **💡 提示**`hbuilderx` 命令会自动处理 HBuilderX 环境检测和启动,相当于直接调用 HBuilderX CLI,但更加便捷。
60+
61+
### 开发命令 (uni-launch)
62+
63+
> **⚠️ 版本要求**`uni-launch` 命令需要 **HBuilderX 5.0 或更高版本**,低于此版本将无法使用该命令。
64+
65+
`uni-launch` 命令用于启动各平台的开发环境,会自动处理项目打开和 HBuilderX 启动。
66+
67+
```bash
68+
# 全局安装后,直接使用命令
69+
uni-launch web
70+
uni-launch app-android --deviceId emulator-5554
71+
uni-launch app-ios --iosTarget simulator
72+
73+
# 或者通过 npm scripts 使用(本地安装时)
74+
# 在 package.json 中配置:
75+
# "scripts": {
76+
# "dev:web": "uni-launch web",
77+
# "dev:app-android": "uni-launch app-android",
78+
# "dev:app-ios": "uni-launch app-ios",
79+
# "dev:mp-weixin": "uni-launch mp-weixin"
80+
# }
81+
npm run dev:web
82+
npm run dev:app-android -- --deviceId emulator-5554
83+
npm run dev:app-ios -- --iosTarget simulator
84+
```
3285

3386
#### Web 平台
3487

3588
```bash
36-
# 使用默认浏览器
89+
# 使用内置浏览器
90+
uni-launch web
91+
# 或(本地安装时通过 npm scripts)
3792
npm run dev:web
3893

3994
# 使用 Chrome 浏览器
95+
uni-launch web --browser Chrome
96+
#
4097
npm run dev:web -- --browser Chrome
4198

4299
# 只编译不运行
100+
uni-launch web --compile true
101+
#
43102
npm run dev:web -- --compile true
44103
```
45104

46105
#### Android 平台
47106

48107
```bash
49108
# 使用默认设备
109+
uni-launch app-android
110+
# 或(本地安装时通过 npm scripts)
50111
npm run dev:app-android
51112

52113
# 指定设备
53-
npm run dev:app-android -- --serial emulator-5554
114+
uni-launch app-android --deviceId emulator-5554
115+
#
116+
npm run dev:app-android -- --deviceId emulator-5554
54117

55118
# 使用自定义基座
119+
uni-launch app-android --playground custom
120+
#
56121
npm run dev:app-android -- --playground custom
57122

58123
# 显示原生日志
124+
uni-launch app-android --native-log true
125+
#
59126
npm run dev:app-android -- --native-log true
60127

61128
# 编译错误后继续运行
129+
uni-launch app-android --continue-on-error true
130+
#
62131
npm run dev:app-android -- --continue-on-error true
63132
```
64133

65134
#### iOS 平台
66135

67136
```bash
68137
# 真机开发
138+
uni-launch app-ios --iosTarget device
139+
# 或(本地安装时通过 npm scripts)
69140
npm run dev:app-ios -- --iosTarget device
70141

71142
# 模拟器开发
143+
uni-launch app-ios --iosTarget simulator
144+
#
72145
npm run dev:app-ios -- --iosTarget simulator
73146

74147
# 指定设备
75-
npm run dev:app-ios -- --serial iPhone-15-Pro
148+
uni-launch app-ios --deviceId iPhone-15-Pro
149+
#
150+
npm run dev:app-ios -- --deviceId iPhone-15-Pro
76151
```
77152

78153
#### 小程序平台
79154

80155
```bash
81156
# 微信小程序(带运行时日志)
157+
uni-launch mp-weixin --runtime-log true
158+
# 或(本地安装时通过 npm scripts)
82159
npm run dev:mp-weixin -- --runtime-log true
83160

84161
# 支付宝小程序
162+
uni-launch mp-alipay --runtime-log true
163+
#
85164
npm run dev:mp-alipay -- --runtime-log true
86165

87166
# 抖音小程序
167+
uni-launch mp-toutiao --runtime-log true
168+
#
88169
npm run dev:mp-toutiao -- --runtime-log true
89-
``` -->
170+
```
90171

91172
### 日志查看命令 (uni-logcat)
92173

174+
> **⚠️ 版本要求**`uni-logcat` 命令需要 **HBuilderX 4.87 或更高版本**,低于此版本将无法使用该命令。
175+
176+
`uni-logcat` 命令用于查看各平台的运行日志,会自动处理项目打开和 HBuilderX 启动。
177+
93178
```bash
94-
# 查看 Web 日志
179+
# 全局安装后,直接使用命令
180+
uni-logcat web
181+
uni-logcat app-android --deviceId emulator-5554
182+
uni-logcat app-ios --iosTarget device
183+
uni-logcat mp-weixin
184+
185+
# 或者通过 npm scripts 使用(本地安装时)
186+
# 在 package.json 中配置:
187+
# "scripts": {
188+
# "logcat:web": "uni-logcat web",
189+
# "logcat:app-android": "uni-logcat app-android",
190+
# "logcat:app-ios": "uni-logcat app-ios",
191+
# "logcat:mp-weixin": "uni-logcat mp-weixin"
192+
# }
95193
npm run logcat:web
96-
97-
# 查看 Android 日志
98194
npm run logcat:app-android -- --deviceId emulator-5554
99-
100-
# 查看 iOS 日志
101195
npm run logcat:app-ios -- --iosTarget device
102-
103-
# 查看小程序日志
104196
npm run logcat:mp-weixin
105197
```
106198

107199
### 测试命令 (uni-test)
108200

201+
> **⚠️ 版本要求**`uni-test` 命令需要 **HBuilderX 4.87 或更高版本**,低于此版本将无法使用该命令。
202+
109203
> **⚠️ 重要提示**:使用测试功能前,需要先在 HBuilderX 中安装 [uni-app 自动化测试插件](https://ext.dcloud.net.cn/plugin?id=5708)
110204
205+
`uni-test` 命令用于运行自动化测试,会自动处理项目打开和 HBuilderX 启动。
206+
207+
```bash
208+
# 全局安装后,直接使用命令
209+
uni-test web --testcaseFile tests/login.test.js
210+
uni-test app-android --device_id emulator-5554
211+
uni-test app-ios --device_id iPhone-15-Pro
212+
213+
# 或者通过 npm scripts 使用(本地安装时)
214+
# 在 package.json 中配置:
215+
# "scripts": {
216+
# "test:web": "uni-test web",
217+
# "test:app-android": "uni-test app-android",
218+
# "test:app-ios": "uni-test app-ios"
219+
# }
220+
npm run test:web -- --testcaseFile tests/login.test.js
221+
npm run test:app-android -- --device_id emulator-5554
222+
npm run test:app-ios -- --device_id iPhone-15-Pro
223+
```
224+
111225
#### 安装测试插件
112226

113227
1. 打开 HBuilderX
@@ -118,15 +232,30 @@ npm run logcat:mp-weixin
118232

119233
```bash
120234
# Web 测试(支持 Chrome、Safari、Firefox,默认为 Chrome)
235+
uni-test web --testcaseFile tests/login.test.js
236+
# 或(本地安装时通过 npm scripts)
121237
npm run test:web -- --testcaseFile tests/login.test.js
238+
239+
uni-test web --browser Chrome --testcaseFile tests/login.test.js
240+
#
122241
npm run test:web -- --browser Chrome --testcaseFile tests/login.test.js
242+
243+
uni-test web --browser Safari --testcaseFile tests/login.test.js
244+
#
123245
npm run test:web -- --browser Safari --testcaseFile tests/login.test.js
246+
247+
uni-test web --browser Firefox --testcaseFile tests/login.test.js
248+
#
124249
npm run test:web -- --browser Firefox --testcaseFile tests/login.test.js
125250

126251
# Android 测试
252+
uni-test app-android --device_id emulator-5554
253+
#
127254
npm run test:app-android -- --device_id emulator-5554
128255

129256
# iOS 测试(仅支持模拟器)
257+
uni-test app-ios --device_id iPhone-15-Pro
258+
#
130259
npm run test:app-ios -- --device_id iPhone-15-Pro
131260
```
132261

@@ -180,7 +309,7 @@ export HBUILDERX_CLI_PATH="/path/to/hbuilderx/cli"
180309
cli --version
181310
```
182311

183-
本工具需要 **HBuilderX 4.87 或更高版本**,请更新到最新版本。
312+
本工具部分功能需要 **HBuilderX 4.87 或更高版本**,请更新到最新版本。
184313

185314
## 📚 更多信息
186315

bin/hbuilderx.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env node
2+
3+
const {
4+
checkHBuilderXEnvironment,
5+
executeCommand,
6+
handleCommandError
7+
} = require('../lib/hbuilderx');
8+
9+
const [...extraArgs] = process.argv.slice(2);
10+
11+
async function main() {
12+
try {
13+
const { path: hbuilderxCli, isRunning } = await checkHBuilderXEnvironment();
14+
15+
// 如果 HBuilderX 未运行,先执行 open
16+
if (!isRunning) {
17+
await executeCommand(hbuilderxCli, ['open']);
18+
}
19+
20+
await executeCommand(hbuilderxCli, [...extraArgs]);
21+
} catch (error) {
22+
handleCommandError(error, 'launch');
23+
}
24+
}
25+
26+
main();

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "@dcloudio/hbuilderx-cli",
3-
"version": "1.1.2",
3+
"version": "1.2.0",
44
"description": "HBuilderX CLI integration for uni-app development",
55
"main": "lib/hbuilderx.js",
66
"bin": {
7+
"hbuilderx": "./bin/hbuilderx.js",
78
"uni-launch": "./bin/uni-launch.js",
89
"uni-logcat": "./bin/uni-logcat.js",
910
"uni-test": "./bin/uni-test.js"

0 commit comments

Comments
 (0)