@weapp-tailwindcss/hbuilderx-runner 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.en.md +35 -0
- package/README.md +36 -0
- package/dist/index.cjs +560 -0
- package/dist/index.d.ts +144 -0
- package/dist/index.mjs +499 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 ice breaker
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.en.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# @weapp-tailwindcss/hbuilderx-runner
|
|
2
|
+
|
|
3
|
+
Stable HBuilderX CLI runner utilities for local e2e workflows, demo scripts, and future CLI wrappers.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import {
|
|
7
|
+
closeProject,
|
|
8
|
+
openProject,
|
|
9
|
+
resolveHBuilderXCliInfo,
|
|
10
|
+
startLaunch,
|
|
11
|
+
} from '@weapp-tailwindcss/hbuilderx-runner'
|
|
12
|
+
|
|
13
|
+
const cli = await resolveHBuilderXCliInfo()
|
|
14
|
+
console.log(cli.path, cli.source, cli.isRunning)
|
|
15
|
+
|
|
16
|
+
await closeProject({ cwd: projectRoot, allowFailure: true })
|
|
17
|
+
await openProject({ cwd: projectRoot })
|
|
18
|
+
|
|
19
|
+
const launch = startLaunch({
|
|
20
|
+
cwd: projectRoot,
|
|
21
|
+
platform: 'app-android',
|
|
22
|
+
args: ['--deviceId', 'emulator-5554'],
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
await launch.stop()
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The package only stabilizes the HBuilderX invocation layer. It does not handle Tailwind or mini program style transformation.
|
|
29
|
+
|
|
30
|
+
## Scope
|
|
31
|
+
|
|
32
|
+
- Resolve HBuilderX CLI from a running HBuilderX instance, `HBUILDERX_CLI_PATH`, or default install paths.
|
|
33
|
+
- Wrap `project open/close`, `launch`, long-running processes, timeouts, recent logs, and process-tree cleanup.
|
|
34
|
+
- Classify common failures into `HBuilderXCommandError`, including project recognition failures, config load failures, missing Android/iOS/Harmony toolchains, and timeouts.
|
|
35
|
+
- Provide local Android `adb`, iOS Xcode/simulator, and Harmony `hdc` probes for e2e and demo scripts.
|
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# @weapp-tailwindcss/hbuilderx-runner
|
|
2
|
+
|
|
3
|
+
HBuilderX CLI 调用辅助包,用于本地 e2e、demo 脚本和后续 CLI 封装。
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import {
|
|
7
|
+
closeProject,
|
|
8
|
+
openProject,
|
|
9
|
+
resolveHBuilderXCliInfo,
|
|
10
|
+
startLaunch,
|
|
11
|
+
} from '@weapp-tailwindcss/hbuilderx-runner'
|
|
12
|
+
|
|
13
|
+
const cli = await resolveHBuilderXCliInfo()
|
|
14
|
+
console.log(cli.path, cli.source, cli.isRunning)
|
|
15
|
+
|
|
16
|
+
await closeProject({ cwd: projectRoot, allowFailure: true })
|
|
17
|
+
await openProject({ cwd: projectRoot })
|
|
18
|
+
|
|
19
|
+
const launch = startLaunch({
|
|
20
|
+
cwd: projectRoot,
|
|
21
|
+
platform: 'app-android',
|
|
22
|
+
args: ['--deviceId', 'emulator-5554'],
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
// 业务侧等待产物或探针后主动停止。
|
|
26
|
+
await launch.stop()
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
该包只负责 HBuilderX 调用层稳定性,不处理 Tailwind 或小程序样式转译。
|
|
30
|
+
|
|
31
|
+
## 能力边界
|
|
32
|
+
|
|
33
|
+
- 从正在运行的 HBuilderX、`HBUILDERX_CLI_PATH` 和默认安装路径解析 CLI。
|
|
34
|
+
- 封装 `project open/close`、`launch`、长驻运行、超时、最近日志与进程树清理。
|
|
35
|
+
- 将常见失败归类为可诊断的 `HBuilderXCommandError`,例如项目识别错误、配置加载失败、Android/iOS/Harmony 工具链缺失和命令超时。
|
|
36
|
+
- 提供 Android `adb`、iOS Xcode/simulator、Harmony `hdc` 的本地工具链探测函数,供 e2e 或 demo 脚本在运行前快速失败。
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
23
|
+
//#endregion
|
|
24
|
+
let node_fs_promises = require("node:fs/promises");
|
|
25
|
+
node_fs_promises = __toESM(node_fs_promises);
|
|
26
|
+
let node_child_process = require("node:child_process");
|
|
27
|
+
let node_path = require("node:path");
|
|
28
|
+
node_path = __toESM(node_path);
|
|
29
|
+
let node_process = require("node:process");
|
|
30
|
+
node_process = __toESM(node_process);
|
|
31
|
+
//#region src/fs.ts
|
|
32
|
+
async function fileExists(file) {
|
|
33
|
+
try {
|
|
34
|
+
await node_fs_promises.default.access(file);
|
|
35
|
+
return true;
|
|
36
|
+
} catch {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async function readUtf8(file) {
|
|
41
|
+
return await node_fs_promises.default.readFile(file, "utf8");
|
|
42
|
+
}
|
|
43
|
+
function wait(ms) {
|
|
44
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
//#region src/logs.ts
|
|
48
|
+
const PROJECT_NOT_UNI_APP_RE = /不是\s*uni-app\s*项目|not\s+a\s+uni-app\s+project/i;
|
|
49
|
+
const PROJECT_TYPE_UNSUPPORTED_RE = /项目类型为(.+?),暂不支持|project\s+type.+unsupported/i;
|
|
50
|
+
const CONFIG_LOAD_FAILED_RE = /failed to load config|error when starting dev server|Failed to resolve entry for package/i;
|
|
51
|
+
const CLI_NOT_FOUND_RE = /未找到\s*HBuilderX\s*CLI|HBUILDERX_CLI_PATH|command not found|ENOENT/i;
|
|
52
|
+
const ANDROID_TOOLCHAIN_MISSING_RE = /adb(?:\.exe)?(?:\s*[::].*)?(?:not found|ENOENT|不是内部或外部命令)|Android SDK|platform-tools/i;
|
|
53
|
+
const IOS_TOOLCHAIN_MISSING_RE = /xcrun|xcode-select|xcodebuild|simctl|DEVELOPER_DIR|iOS 模拟器|Xcode/i;
|
|
54
|
+
const HARMONY_TOOLCHAIN_MISSING_RE = /(?:^|\s)hdc[\s::]|Harmony|DevEco|OpenHarmony/i;
|
|
55
|
+
function createLogBuffer(maxChunks = 160) {
|
|
56
|
+
const logs = [];
|
|
57
|
+
return {
|
|
58
|
+
logs,
|
|
59
|
+
push(chunk) {
|
|
60
|
+
logs.push(chunk.toString());
|
|
61
|
+
if (logs.length > maxChunks) logs.splice(0, logs.length - maxChunks);
|
|
62
|
+
},
|
|
63
|
+
text() {
|
|
64
|
+
return logs.join("");
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function collectProcessOutput(child, maxChunks = 160) {
|
|
69
|
+
const buffer = createLogBuffer(maxChunks);
|
|
70
|
+
child.stdout?.on("data", (chunk) => buffer.push(chunk));
|
|
71
|
+
child.stderr?.on("data", (chunk) => buffer.push(chunk));
|
|
72
|
+
return buffer.logs;
|
|
73
|
+
}
|
|
74
|
+
function classifyHBuilderXOutput(output) {
|
|
75
|
+
if (ANDROID_TOOLCHAIN_MISSING_RE.test(output)) return {
|
|
76
|
+
kind: "android-toolchain-missing",
|
|
77
|
+
message: "当前环境缺少 Android 运行所需工具链。",
|
|
78
|
+
hint: "请安装 Android SDK platform-tools,并确认 adb 可用;需要指定设备时设置 E2E_HBUILDERX_ANDROID_DEVICE_ID。"
|
|
79
|
+
};
|
|
80
|
+
if (IOS_TOOLCHAIN_MISSING_RE.test(output)) return {
|
|
81
|
+
kind: "ios-toolchain-missing",
|
|
82
|
+
message: "当前环境缺少 iOS 模拟器或 Xcode 工具链。",
|
|
83
|
+
hint: "请安装完整 Xcode,确认 xcrun simctl/xcodebuild 可用,并完成 Xcode 首次启动组件安装。"
|
|
84
|
+
};
|
|
85
|
+
if (HARMONY_TOOLCHAIN_MISSING_RE.test(output)) return {
|
|
86
|
+
kind: "harmony-toolchain-missing",
|
|
87
|
+
message: "当前环境缺少 Harmony 运行所需工具链。",
|
|
88
|
+
hint: "请安装 DevEco Studio,或设置 HDC_PATH/E2E_HBUILDERX_HARMONY_HDC_PATH 指向 hdc。"
|
|
89
|
+
};
|
|
90
|
+
if (CLI_NOT_FOUND_RE.test(output)) return {
|
|
91
|
+
kind: "cli-not-found",
|
|
92
|
+
message: "未找到 HBuilderX CLI。",
|
|
93
|
+
hint: "请启动/安装 HBuilderX,或设置 HBUILDERX_CLI_PATH 指向 HBuilderX CLI。"
|
|
94
|
+
};
|
|
95
|
+
if (PROJECT_NOT_UNI_APP_RE.test(output)) return {
|
|
96
|
+
kind: "project-not-uni-app",
|
|
97
|
+
message: "HBuilderX 将当前目录识别为非 uni-app 项目。",
|
|
98
|
+
hint: "请确认传入的是 HBuilderX 可识别的项目根目录,并检查 manifest.json、pages.json 与导入状态。"
|
|
99
|
+
};
|
|
100
|
+
const unsupported = output.match(PROJECT_TYPE_UNSUPPORTED_RE);
|
|
101
|
+
if (unsupported) return {
|
|
102
|
+
kind: "project-type-unsupported",
|
|
103
|
+
message: `HBuilderX 不支持当前项目类型${unsupported[1] ? `:${unsupported[1].trim()}` : ""}。`,
|
|
104
|
+
hint: "请确认项目已按目标平台导入,必要时先 close/open 项目后再 launch。"
|
|
105
|
+
};
|
|
106
|
+
if (CONFIG_LOAD_FAILED_RE.test(output)) return {
|
|
107
|
+
kind: "config-load-failed",
|
|
108
|
+
message: "HBuilderX/Vite 配置加载失败。",
|
|
109
|
+
hint: "请先构建 workspace 依赖并检查 vite.config.ts 中的包入口。输出日志里通常包含具体 import。"
|
|
110
|
+
};
|
|
111
|
+
return {
|
|
112
|
+
kind: "unknown",
|
|
113
|
+
message: "HBuilderX 命令失败,未匹配到已知分类。"
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function createTimeoutIssue() {
|
|
117
|
+
return {
|
|
118
|
+
kind: "timeout",
|
|
119
|
+
message: "HBuilderX 命令在限定时间内没有结束。",
|
|
120
|
+
hint: "请检查 HBuilderX 是否卡在项目导入、设备选择、运行基座安装或平台编译阶段;最近日志通常包含停住的位置。"
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function formatRecentLogs(logs, maxLength = 6e3) {
|
|
124
|
+
const output = logs.join("");
|
|
125
|
+
return output.length > maxLength ? output.slice(-maxLength) : output;
|
|
126
|
+
}
|
|
127
|
+
//#endregion
|
|
128
|
+
//#region src/process.ts
|
|
129
|
+
var HBuilderXCommandError = class extends Error {
|
|
130
|
+
result;
|
|
131
|
+
constructor(message, result) {
|
|
132
|
+
super(message);
|
|
133
|
+
this.name = "HBuilderXCommandError";
|
|
134
|
+
this.result = result;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
function killProcessTree(child, signal = "SIGTERM") {
|
|
138
|
+
const pid = child.pid;
|
|
139
|
+
if (!pid || child.exitCode != null) return;
|
|
140
|
+
if (node_process.default.platform === "win32") {
|
|
141
|
+
(0, node_child_process.spawnSync)("taskkill", [
|
|
142
|
+
"/pid",
|
|
143
|
+
String(pid),
|
|
144
|
+
"/t",
|
|
145
|
+
"/f"
|
|
146
|
+
], {
|
|
147
|
+
stdio: "ignore",
|
|
148
|
+
windowsHide: true
|
|
149
|
+
});
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
try {
|
|
153
|
+
node_process.default.kill(-pid, signal);
|
|
154
|
+
} catch {
|
|
155
|
+
try {
|
|
156
|
+
child.kill(signal);
|
|
157
|
+
} catch {}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function createCommandResult(options, exit, logs, timedOut = false) {
|
|
161
|
+
const output = formatRecentLogs(logs);
|
|
162
|
+
return {
|
|
163
|
+
command: options.command,
|
|
164
|
+
args: options.args,
|
|
165
|
+
cwd: options.cwd,
|
|
166
|
+
exit,
|
|
167
|
+
logs,
|
|
168
|
+
output,
|
|
169
|
+
issue: timedOut ? createTimeoutIssue() : classifyHBuilderXOutput(output)
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function buildCommandError(prefix, options, exit, logs, timedOut = false) {
|
|
173
|
+
const result = createCommandResult(options, exit, logs, timedOut);
|
|
174
|
+
return new HBuilderXCommandError([
|
|
175
|
+
`${prefix}: ${options.command} ${options.args.join(" ")}`,
|
|
176
|
+
`cwd=${options.cwd}`,
|
|
177
|
+
`exit=${exit.signal ?? exit.code}`,
|
|
178
|
+
`issue=${result.issue.kind}: ${result.issue.message}`,
|
|
179
|
+
result.issue.hint ? `hint=${result.issue.hint}` : "",
|
|
180
|
+
result.output
|
|
181
|
+
].filter(Boolean).join("\n"), result);
|
|
182
|
+
}
|
|
183
|
+
function spawnCommand(options) {
|
|
184
|
+
const child = (0, node_child_process.spawn)(options.command, options.args, {
|
|
185
|
+
cwd: options.cwd,
|
|
186
|
+
detached: options.detached ?? node_process.default.platform !== "win32",
|
|
187
|
+
shell: node_process.default.platform === "win32",
|
|
188
|
+
stdio: options.stdio === "inherit" ? "inherit" : [
|
|
189
|
+
"ignore",
|
|
190
|
+
"pipe",
|
|
191
|
+
"pipe"
|
|
192
|
+
],
|
|
193
|
+
env: {
|
|
194
|
+
...node_process.default.env,
|
|
195
|
+
...options.env,
|
|
196
|
+
NODE_OPTIONS: options.env?.NODE_OPTIONS ?? node_process.default.env.NODE_OPTIONS ?? "--max-old-space-size=8192"
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
const logs = options.stdio === "inherit" ? [] : collectProcessOutput(child);
|
|
200
|
+
let exit;
|
|
201
|
+
const closed = new Promise((resolve) => {
|
|
202
|
+
child.on("close", (code, signal) => {
|
|
203
|
+
exit = {
|
|
204
|
+
code,
|
|
205
|
+
signal
|
|
206
|
+
};
|
|
207
|
+
resolve(exit);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
return {
|
|
211
|
+
child,
|
|
212
|
+
logs,
|
|
213
|
+
command: options.command,
|
|
214
|
+
args: options.args,
|
|
215
|
+
cwd: options.cwd,
|
|
216
|
+
closed,
|
|
217
|
+
ensureRunning() {
|
|
218
|
+
if (exit && exit.code !== 0) throw buildCommandError("命令提前退出", options, exit, logs);
|
|
219
|
+
},
|
|
220
|
+
async stop(signal = "SIGTERM") {
|
|
221
|
+
killProcessTree(child, signal);
|
|
222
|
+
await Promise.race([closed, wait(5e3)]);
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
async function runCommand(options) {
|
|
227
|
+
const spawned = spawnCommand(options);
|
|
228
|
+
let timer;
|
|
229
|
+
let timedOut = false;
|
|
230
|
+
try {
|
|
231
|
+
const exit = await (options.timeoutMs == null ? spawned.closed : Promise.race([spawned.closed, new Promise((resolve) => {
|
|
232
|
+
timer = setTimeout(() => {
|
|
233
|
+
timedOut = true;
|
|
234
|
+
killProcessTree(spawned.child);
|
|
235
|
+
resolve({
|
|
236
|
+
code: null,
|
|
237
|
+
signal: "SIGTERM"
|
|
238
|
+
});
|
|
239
|
+
}, options.timeoutMs);
|
|
240
|
+
})]));
|
|
241
|
+
const result = createCommandResult(options, exit, spawned.logs, timedOut);
|
|
242
|
+
if (exit.code === 0 || options.allowFailure) return result;
|
|
243
|
+
throw buildCommandError(options.timeoutMs != null && exit.code == null ? "命令超时" : "命令失败", options, exit, spawned.logs, timedOut);
|
|
244
|
+
} finally {
|
|
245
|
+
if (timer) clearTimeout(timer);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
function createProcessExitTracker(child) {
|
|
249
|
+
let exit;
|
|
250
|
+
return {
|
|
251
|
+
closed: new Promise((resolve) => {
|
|
252
|
+
child.on("close", (code, signal) => {
|
|
253
|
+
exit = {
|
|
254
|
+
code,
|
|
255
|
+
signal
|
|
256
|
+
};
|
|
257
|
+
resolve(exit);
|
|
258
|
+
});
|
|
259
|
+
}),
|
|
260
|
+
ensureRunning(logs) {
|
|
261
|
+
if (exit && exit.code !== 0) {
|
|
262
|
+
const result = createCommandResult({
|
|
263
|
+
command: "hbuilderx",
|
|
264
|
+
args: ["launch"],
|
|
265
|
+
cwd: node_process.default.cwd()
|
|
266
|
+
}, exit, logs);
|
|
267
|
+
throw new HBuilderXCommandError(`HBuilderX launch 提前退出\nissue=${result.issue.kind}: ${result.issue.message}\n${result.output}`, result);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
//#endregion
|
|
273
|
+
//#region src/hbuilderx.ts
|
|
274
|
+
const macOSDefaultCli = "/Applications/HBuilderX.app/Contents/MacOS/cli";
|
|
275
|
+
function getDefaultHBuilderXCliCandidates(env = node_process.default.env) {
|
|
276
|
+
return [env.HBUILDERX_CLI_PATH, node_process.default.platform === "darwin" ? macOSDefaultCli : void 0].filter((item) => Boolean(item));
|
|
277
|
+
}
|
|
278
|
+
function extractHBuilderXMacOSExecutable(line) {
|
|
279
|
+
return line.match(/(\/.*?HBuilderX[^/]*\.app\/Contents\/MacOS\/HBuilderX)(?:\s|$)/i)?.[1];
|
|
280
|
+
}
|
|
281
|
+
function extractHBuilderXWindowsExecutable(line) {
|
|
282
|
+
const executable = line.split(",").at(-1)?.trim();
|
|
283
|
+
return executable?.toLowerCase().endsWith("hbuilderx.exe") ? executable : void 0;
|
|
284
|
+
}
|
|
285
|
+
function extractHBuilderXExecutableFromProcessOutput(output, platform = node_process.default.platform) {
|
|
286
|
+
for (const line of output.split(/\r?\n/)) {
|
|
287
|
+
const executable = platform === "win32" ? extractHBuilderXWindowsExecutable(line) : extractHBuilderXMacOSExecutable(line);
|
|
288
|
+
if (executable) return executable;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
async function findRunningHBuilderXCli(platform = node_process.default.platform) {
|
|
292
|
+
const result = platform === "win32" ? (0, node_child_process.spawnSync)("wmic", [
|
|
293
|
+
"process",
|
|
294
|
+
"where",
|
|
295
|
+
"name='HBuilderX.exe'",
|
|
296
|
+
"get",
|
|
297
|
+
"executablepath",
|
|
298
|
+
"/format:csv"
|
|
299
|
+
], {
|
|
300
|
+
encoding: "utf8",
|
|
301
|
+
windowsHide: true
|
|
302
|
+
}) : (0, node_child_process.spawnSync)("ps", [
|
|
303
|
+
"-ax",
|
|
304
|
+
"-o",
|
|
305
|
+
"command="
|
|
306
|
+
], { encoding: "utf8" });
|
|
307
|
+
if (result.error || result.status !== 0) return;
|
|
308
|
+
const executable = extractHBuilderXExecutableFromProcessOutput(`${result.stdout ?? ""}${result.stderr ?? ""}`, platform);
|
|
309
|
+
if (!executable || !await fileExists(executable)) return;
|
|
310
|
+
const cli = node_path.default.join(node_path.default.dirname(executable), platform === "win32" ? "cli.exe" : "cli");
|
|
311
|
+
return await fileExists(cli) ? cli : void 0;
|
|
312
|
+
}
|
|
313
|
+
function resolveCandidateSource(candidate, env) {
|
|
314
|
+
if (candidate === env.HBUILDERX_CLI_PATH) return "env";
|
|
315
|
+
if (candidate === macOSDefaultCli) return "default-path";
|
|
316
|
+
return "candidate";
|
|
317
|
+
}
|
|
318
|
+
async function resolveHBuilderXCliInfo(candidates, env = node_process.default.env) {
|
|
319
|
+
if (!candidates) {
|
|
320
|
+
const runningCli = await findRunningHBuilderXCli();
|
|
321
|
+
if (runningCli) return {
|
|
322
|
+
path: runningCli,
|
|
323
|
+
isRunning: true,
|
|
324
|
+
source: "running-process"
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
const items = candidates ?? getDefaultHBuilderXCliCandidates(env);
|
|
328
|
+
for (const item of items) if (await fileExists(item)) return {
|
|
329
|
+
path: item,
|
|
330
|
+
isRunning: false,
|
|
331
|
+
source: resolveCandidateSource(item, env)
|
|
332
|
+
};
|
|
333
|
+
throw new Error("未找到 HBuilderX CLI。请先安装 HBuilderX,或设置 HBUILDERX_CLI_PATH=/path/to/cli。");
|
|
334
|
+
}
|
|
335
|
+
async function resolveHBuilderXCli(candidates) {
|
|
336
|
+
return (await resolveHBuilderXCliInfo(candidates)).path;
|
|
337
|
+
}
|
|
338
|
+
function createHBuilderXEnv(options = {}) {
|
|
339
|
+
return {
|
|
340
|
+
...options.env,
|
|
341
|
+
HBUILDERX_CLI_PATH: options.hbuilderxCliPath ?? options.env?.HBUILDERX_CLI_PATH ?? node_process.default.env.HBUILDERX_CLI_PATH ?? getDefaultHBuilderXCliCandidates()[0],
|
|
342
|
+
NODE_OPTIONS: options.env?.NODE_OPTIONS ?? node_process.default.env.NODE_OPTIONS ?? "--max-old-space-size=8192"
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
function hbuilderxPnpmArgs(args) {
|
|
346
|
+
return [
|
|
347
|
+
"exec",
|
|
348
|
+
"hbuilderx",
|
|
349
|
+
...args
|
|
350
|
+
];
|
|
351
|
+
}
|
|
352
|
+
async function runPnpmCommand(options) {
|
|
353
|
+
return await runCommand({
|
|
354
|
+
command: "pnpm",
|
|
355
|
+
args: options.args,
|
|
356
|
+
cwd: options.cwd,
|
|
357
|
+
timeoutMs: options.timeoutMs,
|
|
358
|
+
allowFailure: options.allowFailure,
|
|
359
|
+
env: createHBuilderXEnv(options)
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
function spawnPnpmCommand(options) {
|
|
363
|
+
return spawnCommand({
|
|
364
|
+
command: "pnpm",
|
|
365
|
+
args: options.args,
|
|
366
|
+
cwd: options.cwd,
|
|
367
|
+
env: createHBuilderXEnv(options)
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
async function closeProject(options) {
|
|
371
|
+
return await runPnpmCommand({
|
|
372
|
+
...options,
|
|
373
|
+
args: hbuilderxPnpmArgs([
|
|
374
|
+
"project",
|
|
375
|
+
"close",
|
|
376
|
+
"--path",
|
|
377
|
+
node_path.default.resolve(options.cwd)
|
|
378
|
+
]),
|
|
379
|
+
allowFailure: options.allowFailure ?? true
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
async function openProject(options) {
|
|
383
|
+
return await runPnpmCommand({
|
|
384
|
+
...options,
|
|
385
|
+
args: hbuilderxPnpmArgs([
|
|
386
|
+
"project",
|
|
387
|
+
"open",
|
|
388
|
+
"--path",
|
|
389
|
+
node_path.default.resolve(options.cwd)
|
|
390
|
+
])
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
async function prepareProject(options) {
|
|
394
|
+
await closeProject({
|
|
395
|
+
...options,
|
|
396
|
+
allowFailure: true
|
|
397
|
+
});
|
|
398
|
+
return await openProject(options);
|
|
399
|
+
}
|
|
400
|
+
function createLaunchArgs(options) {
|
|
401
|
+
const args = [
|
|
402
|
+
"launch",
|
|
403
|
+
options.platform,
|
|
404
|
+
"--project",
|
|
405
|
+
node_path.default.resolve(options.cwd),
|
|
406
|
+
...options.args ?? []
|
|
407
|
+
];
|
|
408
|
+
if (options.compile != null && !args.includes("--compile")) args.push("--compile", String(options.compile));
|
|
409
|
+
if (options.runtimeLog != null && !args.includes("--runtime-log")) args.push("--runtime-log", String(options.runtimeLog));
|
|
410
|
+
return hbuilderxPnpmArgs(args);
|
|
411
|
+
}
|
|
412
|
+
async function launchProject(options) {
|
|
413
|
+
return await runPnpmCommand({
|
|
414
|
+
...options,
|
|
415
|
+
args: createLaunchArgs(options)
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
function startLaunch(options) {
|
|
419
|
+
return spawnPnpmCommand({
|
|
420
|
+
...options,
|
|
421
|
+
args: createLaunchArgs(options)
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
//#endregion
|
|
425
|
+
//#region src/toolchains.ts
|
|
426
|
+
function runTool(command, args) {
|
|
427
|
+
const result = (0, node_child_process.spawnSync)(command, args, { encoding: "utf8" });
|
|
428
|
+
return {
|
|
429
|
+
ok: result.status === 0,
|
|
430
|
+
output: `${result.stdout ?? ""}${result.stderr ?? ""}`.trim(),
|
|
431
|
+
status: result.status,
|
|
432
|
+
signal: result.signal
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
function resolveAdbCommand(env = node_process.default.env) {
|
|
436
|
+
const adb = runTool("adb", ["version"]);
|
|
437
|
+
if (adb.ok) return {
|
|
438
|
+
command: "adb",
|
|
439
|
+
output: adb.output,
|
|
440
|
+
env: {}
|
|
441
|
+
};
|
|
442
|
+
const candidates = [
|
|
443
|
+
env.ANDROID_HOME,
|
|
444
|
+
env.ANDROID_SDK_ROOT,
|
|
445
|
+
env.HOME ? node_path.default.join(env.HOME, "Library/Android/sdk") : void 0,
|
|
446
|
+
"/opt/android-sdk",
|
|
447
|
+
"/usr/local/share/android-sdk"
|
|
448
|
+
].filter((item) => Boolean(item));
|
|
449
|
+
for (const sdkRoot of candidates) {
|
|
450
|
+
const adbPath = node_path.default.join(sdkRoot, "platform-tools", node_process.default.platform === "win32" ? "adb.exe" : "adb");
|
|
451
|
+
const candidate = runTool(adbPath, ["version"]);
|
|
452
|
+
if (candidate.ok) return {
|
|
453
|
+
command: adbPath,
|
|
454
|
+
output: candidate.output,
|
|
455
|
+
env: {
|
|
456
|
+
ANDROID_HOME: env.ANDROID_HOME ?? sdkRoot,
|
|
457
|
+
ANDROID_SDK_ROOT: env.ANDROID_SDK_ROOT ?? sdkRoot,
|
|
458
|
+
PATH: `${node_path.default.dirname(adbPath)}${node_path.default.delimiter}${env.PATH ?? ""}`
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
return {
|
|
463
|
+
command: "adb",
|
|
464
|
+
output: adb.output,
|
|
465
|
+
env: void 0
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
function assertAndroidToolchain(env = node_process.default.env) {
|
|
469
|
+
const adb = resolveAdbCommand(env);
|
|
470
|
+
if (!adb.env && adb.command === "adb") throw new Error([
|
|
471
|
+
"当前机器缺少 Android App E2E 所需的 adb,无法运行 HBuilderX app-android E2E。",
|
|
472
|
+
`adb: ${adb.output || "not found"}`,
|
|
473
|
+
"请先安装 Android SDK platform-tools,并确认 adb 可在 PATH 中访问。",
|
|
474
|
+
"如需指定设备,请设置 E2E_HBUILDERX_ANDROID_DEVICE_ID。"
|
|
475
|
+
].join("\n"));
|
|
476
|
+
return adb.env;
|
|
477
|
+
}
|
|
478
|
+
function assertIosSimulatorToolchain(env = node_process.default.env) {
|
|
479
|
+
if (node_process.default.platform !== "darwin") throw new Error("HBuilderX iOS 模拟器 E2E 只能在 macOS 本地运行。");
|
|
480
|
+
const xcodeSelect = runTool("xcode-select", ["-p"]);
|
|
481
|
+
const simctl = runTool("xcrun", ["--find", "simctl"]);
|
|
482
|
+
const xcodebuild = runTool("xcrun", ["--find", "xcodebuild"]);
|
|
483
|
+
const firstLaunchStatus = xcodebuild.ok ? runTool("xcodebuild", ["-checkFirstLaunchStatus"]) : void 0;
|
|
484
|
+
if (!simctl.ok || !xcodebuild.ok || firstLaunchStatus?.ok === false) throw new Error([
|
|
485
|
+
"当前机器缺少 iOS 模拟器所需的完整 Xcode 工具链,无法运行 HBuilderX app-ios E2E。",
|
|
486
|
+
`xcode-select: ${xcodeSelect.output || "unknown"}`,
|
|
487
|
+
`DEVELOPER_DIR: ${env.DEVELOPER_DIR || "未设置"}`,
|
|
488
|
+
`simctl: ${simctl.output || "not found"}`,
|
|
489
|
+
`xcodebuild: ${xcodebuild.output || "not found"}`,
|
|
490
|
+
`firstLaunchStatus: ${firstLaunchStatus?.output || (firstLaunchStatus?.ok === false ? "not ready" : "unknown")}`,
|
|
491
|
+
"请安装完整 Xcode,并执行 sudo xcode-select -s /Applications/Xcode.app/Contents/Developer,或用 DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer 临时指定后重试。",
|
|
492
|
+
"如果 firstLaunchStatus 不是 ready,请先打开 Xcode 完成首次组件安装,或执行 sudo xcodebuild -runFirstLaunch。"
|
|
493
|
+
].join("\n"));
|
|
494
|
+
}
|
|
495
|
+
function resolveHdcCommand(env = node_process.default.env) {
|
|
496
|
+
const candidates = [
|
|
497
|
+
env.DEMO_VISUAL_HARMONY_HDC_PATH,
|
|
498
|
+
env.E2E_HBUILDERX_HARMONY_HDC_PATH,
|
|
499
|
+
env.HDC_PATH,
|
|
500
|
+
"hdc",
|
|
501
|
+
node_process.default.platform === "darwin" ? "/Applications/DevEco-Studio.app/Contents/sdk/default/openharmony/toolchains/hdc" : void 0
|
|
502
|
+
].filter((item) => Boolean(item));
|
|
503
|
+
for (const candidate of candidates) if (runTool(candidate, ["version"]).ok) return candidate;
|
|
504
|
+
throw new Error([
|
|
505
|
+
"当前机器缺少 Harmony App E2E 所需的 hdc,无法运行 HBuilderX app-harmony E2E。",
|
|
506
|
+
"请安装 DevEco Studio,或设置 DEMO_VISUAL_HARMONY_HDC_PATH=/path/to/hdc。",
|
|
507
|
+
"如需指定设备,请设置 E2E_HBUILDERX_HARMONY_DEVICE_ID 或 DEMO_VISUAL_HARMONY_DEVICE_ID。"
|
|
508
|
+
].join("\n"));
|
|
509
|
+
}
|
|
510
|
+
function assertHarmonyToolchain(env = node_process.default.env) {
|
|
511
|
+
const hdc = resolveHdcCommand(env);
|
|
512
|
+
const result = runTool(hdc, ["list", "targets"]);
|
|
513
|
+
if (!result.ok) {
|
|
514
|
+
const issue = classifyHBuilderXOutput(result.output);
|
|
515
|
+
throw new Error([
|
|
516
|
+
"当前 Harmony hdc 无法列出设备,无法运行 HBuilderX app-harmony E2E。",
|
|
517
|
+
result.output || `exit=${result.status} signal=${result.signal ?? "none"}`,
|
|
518
|
+
`issue=${issue.kind}: ${issue.message}`,
|
|
519
|
+
"请确认 Harmony 模拟器或真机已连接。"
|
|
520
|
+
].join("\n"));
|
|
521
|
+
}
|
|
522
|
+
return hdc;
|
|
523
|
+
}
|
|
524
|
+
//#endregion
|
|
525
|
+
exports.HBuilderXCommandError = HBuilderXCommandError;
|
|
526
|
+
exports.assertAndroidToolchain = assertAndroidToolchain;
|
|
527
|
+
exports.assertHarmonyToolchain = assertHarmonyToolchain;
|
|
528
|
+
exports.assertIosSimulatorToolchain = assertIosSimulatorToolchain;
|
|
529
|
+
exports.classifyHBuilderXOutput = classifyHBuilderXOutput;
|
|
530
|
+
exports.closeProject = closeProject;
|
|
531
|
+
exports.collectProcessOutput = collectProcessOutput;
|
|
532
|
+
exports.createHBuilderXEnv = createHBuilderXEnv;
|
|
533
|
+
exports.createLaunchArgs = createLaunchArgs;
|
|
534
|
+
exports.createLogBuffer = createLogBuffer;
|
|
535
|
+
exports.createProcessExitTracker = createProcessExitTracker;
|
|
536
|
+
exports.createTimeoutIssue = createTimeoutIssue;
|
|
537
|
+
exports.extractHBuilderXExecutableFromProcessOutput = extractHBuilderXExecutableFromProcessOutput;
|
|
538
|
+
exports.extractHBuilderXMacOSExecutable = extractHBuilderXMacOSExecutable;
|
|
539
|
+
exports.extractHBuilderXWindowsExecutable = extractHBuilderXWindowsExecutable;
|
|
540
|
+
exports.fileExists = fileExists;
|
|
541
|
+
exports.findRunningHBuilderXCli = findRunningHBuilderXCli;
|
|
542
|
+
exports.formatRecentLogs = formatRecentLogs;
|
|
543
|
+
exports.getDefaultHBuilderXCliCandidates = getDefaultHBuilderXCliCandidates;
|
|
544
|
+
exports.hbuilderxPnpmArgs = hbuilderxPnpmArgs;
|
|
545
|
+
exports.killProcessTree = killProcessTree;
|
|
546
|
+
exports.launchProject = launchProject;
|
|
547
|
+
exports.openProject = openProject;
|
|
548
|
+
exports.prepareProject = prepareProject;
|
|
549
|
+
exports.readUtf8 = readUtf8;
|
|
550
|
+
exports.resolveAdbCommand = resolveAdbCommand;
|
|
551
|
+
exports.resolveHBuilderXCli = resolveHBuilderXCli;
|
|
552
|
+
exports.resolveHBuilderXCliInfo = resolveHBuilderXCliInfo;
|
|
553
|
+
exports.resolveHdcCommand = resolveHdcCommand;
|
|
554
|
+
exports.runCommand = runCommand;
|
|
555
|
+
exports.runPnpmCommand = runPnpmCommand;
|
|
556
|
+
exports.runTool = runTool;
|
|
557
|
+
exports.spawnCommand = spawnCommand;
|
|
558
|
+
exports.spawnPnpmCommand = spawnPnpmCommand;
|
|
559
|
+
exports.startLaunch = startLaunch;
|
|
560
|
+
exports.wait = wait;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { ChildProcess } from "node:child_process";
|
|
2
|
+
import { Buffer } from "node:buffer";
|
|
3
|
+
|
|
4
|
+
//#region src/fs.d.ts
|
|
5
|
+
declare function fileExists(file: string): Promise<boolean>;
|
|
6
|
+
declare function readUtf8(file: string): Promise<string>;
|
|
7
|
+
declare function wait(ms: number): Promise<unknown>;
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/types.d.ts
|
|
10
|
+
type HBuilderXMiniProgramPlatform = 'mp-weixin' | 'mp-alipay' | 'mp-baidu' | 'mp-toutiao';
|
|
11
|
+
type HBuilderXAppPlatform = 'app-android' | 'app-ios' | 'app-harmony';
|
|
12
|
+
type HBuilderXPlatform = HBuilderXMiniProgramPlatform | HBuilderXAppPlatform;
|
|
13
|
+
type HBuilderXIssueKind = 'cli-not-found' | 'project-not-uni-app' | 'project-type-unsupported' | 'config-load-failed' | 'android-toolchain-missing' | 'ios-toolchain-missing' | 'harmony-toolchain-missing' | 'timeout' | 'process-exit' | 'unknown';
|
|
14
|
+
interface HBuilderXIssue {
|
|
15
|
+
kind: HBuilderXIssueKind;
|
|
16
|
+
message: string;
|
|
17
|
+
hint?: string;
|
|
18
|
+
}
|
|
19
|
+
interface CommandExit {
|
|
20
|
+
code: number | null;
|
|
21
|
+
signal: NodeJS.Signals | null;
|
|
22
|
+
}
|
|
23
|
+
interface HBuilderXCommandOptions {
|
|
24
|
+
command: string;
|
|
25
|
+
args: string[];
|
|
26
|
+
cwd: string;
|
|
27
|
+
timeoutMs?: number;
|
|
28
|
+
env?: Record<string, string | undefined>;
|
|
29
|
+
allowFailure?: boolean;
|
|
30
|
+
detached?: boolean;
|
|
31
|
+
stdio?: 'pipe' | 'inherit';
|
|
32
|
+
}
|
|
33
|
+
interface HBuilderXCommandResult {
|
|
34
|
+
command: string;
|
|
35
|
+
args: string[];
|
|
36
|
+
cwd: string;
|
|
37
|
+
exit: CommandExit;
|
|
38
|
+
logs: string[];
|
|
39
|
+
output: string;
|
|
40
|
+
issue: HBuilderXIssue;
|
|
41
|
+
}
|
|
42
|
+
interface SpawnedHBuilderXCommand {
|
|
43
|
+
child: ChildProcess;
|
|
44
|
+
logs: string[];
|
|
45
|
+
command: string;
|
|
46
|
+
args: string[];
|
|
47
|
+
cwd: string;
|
|
48
|
+
stop: (signal?: NodeJS.Signals) => Promise<void>;
|
|
49
|
+
closed: Promise<CommandExit>;
|
|
50
|
+
ensureRunning: () => void;
|
|
51
|
+
}
|
|
52
|
+
interface HBuilderXProjectOptions {
|
|
53
|
+
cwd: string;
|
|
54
|
+
hbuilderxCliPath?: string;
|
|
55
|
+
env?: Record<string, string | undefined>;
|
|
56
|
+
timeoutMs?: number;
|
|
57
|
+
allowFailure?: boolean;
|
|
58
|
+
}
|
|
59
|
+
interface HBuilderXLaunchOptions extends HBuilderXProjectOptions {
|
|
60
|
+
platform: HBuilderXPlatform;
|
|
61
|
+
args?: string[];
|
|
62
|
+
compile?: boolean;
|
|
63
|
+
runtimeLog?: boolean;
|
|
64
|
+
}
|
|
65
|
+
interface ToolResult {
|
|
66
|
+
ok: boolean;
|
|
67
|
+
output: string;
|
|
68
|
+
status: number | null;
|
|
69
|
+
signal: NodeJS.Signals | null;
|
|
70
|
+
}
|
|
71
|
+
interface AndroidToolchain {
|
|
72
|
+
command: string;
|
|
73
|
+
output: string;
|
|
74
|
+
env: Record<string, string> | undefined;
|
|
75
|
+
}
|
|
76
|
+
type HBuilderXCliResolutionSource = 'running-process' | 'env' | 'default-path' | 'candidate';
|
|
77
|
+
interface HBuilderXCliResolution {
|
|
78
|
+
path: string;
|
|
79
|
+
isRunning: boolean;
|
|
80
|
+
source: HBuilderXCliResolutionSource;
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/hbuilderx.d.ts
|
|
84
|
+
declare function getDefaultHBuilderXCliCandidates(env?: NodeJS.ProcessEnv): string[];
|
|
85
|
+
declare function extractHBuilderXMacOSExecutable(line: string): string | undefined;
|
|
86
|
+
declare function extractHBuilderXWindowsExecutable(line: string): string | undefined;
|
|
87
|
+
declare function extractHBuilderXExecutableFromProcessOutput(output: string, platform?: NodeJS.Platform): string | undefined;
|
|
88
|
+
declare function findRunningHBuilderXCli(platform?: NodeJS.Platform): Promise<string | undefined>;
|
|
89
|
+
declare function resolveHBuilderXCliInfo(candidates?: string[], env?: NodeJS.ProcessEnv): Promise<HBuilderXCliResolution>;
|
|
90
|
+
declare function resolveHBuilderXCli(candidates?: string[]): Promise<string>;
|
|
91
|
+
declare function createHBuilderXEnv(options?: {
|
|
92
|
+
hbuilderxCliPath?: string;
|
|
93
|
+
env?: Record<string, string | undefined>;
|
|
94
|
+
}): {
|
|
95
|
+
HBUILDERX_CLI_PATH: string | undefined;
|
|
96
|
+
NODE_OPTIONS: string;
|
|
97
|
+
};
|
|
98
|
+
declare function hbuilderxPnpmArgs(args: string[]): string[];
|
|
99
|
+
declare function runPnpmCommand(options: HBuilderXProjectOptions & {
|
|
100
|
+
args: string[];
|
|
101
|
+
}): Promise<HBuilderXCommandResult>;
|
|
102
|
+
declare function spawnPnpmCommand(options: HBuilderXProjectOptions & {
|
|
103
|
+
args: string[];
|
|
104
|
+
}): SpawnedHBuilderXCommand;
|
|
105
|
+
declare function closeProject(options: HBuilderXProjectOptions): Promise<HBuilderXCommandResult>;
|
|
106
|
+
declare function openProject(options: HBuilderXProjectOptions): Promise<HBuilderXCommandResult>;
|
|
107
|
+
declare function prepareProject(options: HBuilderXProjectOptions): Promise<HBuilderXCommandResult>;
|
|
108
|
+
declare function createLaunchArgs(options: HBuilderXLaunchOptions): string[];
|
|
109
|
+
declare function launchProject(options: HBuilderXLaunchOptions): Promise<HBuilderXCommandResult>;
|
|
110
|
+
declare function startLaunch(options: HBuilderXLaunchOptions): SpawnedHBuilderXCommand;
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region src/logs.d.ts
|
|
113
|
+
declare function createLogBuffer(maxChunks?: number): {
|
|
114
|
+
logs: string[];
|
|
115
|
+
push(chunk: Buffer | string): void;
|
|
116
|
+
text(): string;
|
|
117
|
+
};
|
|
118
|
+
declare function collectProcessOutput(child: ChildProcess, maxChunks?: number): string[];
|
|
119
|
+
declare function classifyHBuilderXOutput(output: string): HBuilderXIssue;
|
|
120
|
+
declare function createTimeoutIssue(): HBuilderXIssue;
|
|
121
|
+
declare function formatRecentLogs(logs: string[], maxLength?: number): string;
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region src/process.d.ts
|
|
124
|
+
declare class HBuilderXCommandError extends Error {
|
|
125
|
+
readonly result: HBuilderXCommandResult;
|
|
126
|
+
constructor(message: string, result: HBuilderXCommandResult);
|
|
127
|
+
}
|
|
128
|
+
declare function killProcessTree(child: ChildProcess, signal?: NodeJS.Signals): void;
|
|
129
|
+
declare function spawnCommand(options: HBuilderXCommandOptions): SpawnedHBuilderXCommand;
|
|
130
|
+
declare function runCommand(options: HBuilderXCommandOptions): Promise<HBuilderXCommandResult>;
|
|
131
|
+
declare function createProcessExitTracker(child: ChildProcess): {
|
|
132
|
+
closed: Promise<CommandExit>;
|
|
133
|
+
ensureRunning(logs: string[]): void;
|
|
134
|
+
};
|
|
135
|
+
//#endregion
|
|
136
|
+
//#region src/toolchains.d.ts
|
|
137
|
+
declare function runTool(command: string, args: string[]): ToolResult;
|
|
138
|
+
declare function resolveAdbCommand(env?: NodeJS.ProcessEnv): AndroidToolchain;
|
|
139
|
+
declare function assertAndroidToolchain(env?: NodeJS.ProcessEnv): Record<string, string> | undefined;
|
|
140
|
+
declare function assertIosSimulatorToolchain(env?: NodeJS.ProcessEnv): void;
|
|
141
|
+
declare function resolveHdcCommand(env?: NodeJS.ProcessEnv): string;
|
|
142
|
+
declare function assertHarmonyToolchain(env?: NodeJS.ProcessEnv): string;
|
|
143
|
+
//#endregion
|
|
144
|
+
export { AndroidToolchain, CommandExit, HBuilderXAppPlatform, HBuilderXCliResolution, HBuilderXCliResolutionSource, HBuilderXCommandError, HBuilderXCommandOptions, HBuilderXCommandResult, HBuilderXIssue, HBuilderXIssueKind, HBuilderXLaunchOptions, HBuilderXMiniProgramPlatform, HBuilderXPlatform, HBuilderXProjectOptions, SpawnedHBuilderXCommand, ToolResult, assertAndroidToolchain, assertHarmonyToolchain, assertIosSimulatorToolchain, classifyHBuilderXOutput, closeProject, collectProcessOutput, createHBuilderXEnv, createLaunchArgs, createLogBuffer, createProcessExitTracker, createTimeoutIssue, extractHBuilderXExecutableFromProcessOutput, extractHBuilderXMacOSExecutable, extractHBuilderXWindowsExecutable, fileExists, findRunningHBuilderXCli, formatRecentLogs, getDefaultHBuilderXCliCandidates, hbuilderxPnpmArgs, killProcessTree, launchProject, openProject, prepareProject, readUtf8, resolveAdbCommand, resolveHBuilderXCli, resolveHBuilderXCliInfo, resolveHdcCommand, runCommand, runPnpmCommand, runTool, spawnCommand, spawnPnpmCommand, startLaunch, wait };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import process from "node:process";
|
|
5
|
+
//#region src/fs.ts
|
|
6
|
+
async function fileExists(file) {
|
|
7
|
+
try {
|
|
8
|
+
await fs.access(file);
|
|
9
|
+
return true;
|
|
10
|
+
} catch {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
async function readUtf8(file) {
|
|
15
|
+
return await fs.readFile(file, "utf8");
|
|
16
|
+
}
|
|
17
|
+
function wait(ms) {
|
|
18
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/logs.ts
|
|
22
|
+
const PROJECT_NOT_UNI_APP_RE = /不是\s*uni-app\s*项目|not\s+a\s+uni-app\s+project/i;
|
|
23
|
+
const PROJECT_TYPE_UNSUPPORTED_RE = /项目类型为(.+?),暂不支持|project\s+type.+unsupported/i;
|
|
24
|
+
const CONFIG_LOAD_FAILED_RE = /failed to load config|error when starting dev server|Failed to resolve entry for package/i;
|
|
25
|
+
const CLI_NOT_FOUND_RE = /未找到\s*HBuilderX\s*CLI|HBUILDERX_CLI_PATH|command not found|ENOENT/i;
|
|
26
|
+
const ANDROID_TOOLCHAIN_MISSING_RE = /adb(?:\.exe)?(?:\s*[::].*)?(?:not found|ENOENT|不是内部或外部命令)|Android SDK|platform-tools/i;
|
|
27
|
+
const IOS_TOOLCHAIN_MISSING_RE = /xcrun|xcode-select|xcodebuild|simctl|DEVELOPER_DIR|iOS 模拟器|Xcode/i;
|
|
28
|
+
const HARMONY_TOOLCHAIN_MISSING_RE = /(?:^|\s)hdc[\s::]|Harmony|DevEco|OpenHarmony/i;
|
|
29
|
+
function createLogBuffer(maxChunks = 160) {
|
|
30
|
+
const logs = [];
|
|
31
|
+
return {
|
|
32
|
+
logs,
|
|
33
|
+
push(chunk) {
|
|
34
|
+
logs.push(chunk.toString());
|
|
35
|
+
if (logs.length > maxChunks) logs.splice(0, logs.length - maxChunks);
|
|
36
|
+
},
|
|
37
|
+
text() {
|
|
38
|
+
return logs.join("");
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function collectProcessOutput(child, maxChunks = 160) {
|
|
43
|
+
const buffer = createLogBuffer(maxChunks);
|
|
44
|
+
child.stdout?.on("data", (chunk) => buffer.push(chunk));
|
|
45
|
+
child.stderr?.on("data", (chunk) => buffer.push(chunk));
|
|
46
|
+
return buffer.logs;
|
|
47
|
+
}
|
|
48
|
+
function classifyHBuilderXOutput(output) {
|
|
49
|
+
if (ANDROID_TOOLCHAIN_MISSING_RE.test(output)) return {
|
|
50
|
+
kind: "android-toolchain-missing",
|
|
51
|
+
message: "当前环境缺少 Android 运行所需工具链。",
|
|
52
|
+
hint: "请安装 Android SDK platform-tools,并确认 adb 可用;需要指定设备时设置 E2E_HBUILDERX_ANDROID_DEVICE_ID。"
|
|
53
|
+
};
|
|
54
|
+
if (IOS_TOOLCHAIN_MISSING_RE.test(output)) return {
|
|
55
|
+
kind: "ios-toolchain-missing",
|
|
56
|
+
message: "当前环境缺少 iOS 模拟器或 Xcode 工具链。",
|
|
57
|
+
hint: "请安装完整 Xcode,确认 xcrun simctl/xcodebuild 可用,并完成 Xcode 首次启动组件安装。"
|
|
58
|
+
};
|
|
59
|
+
if (HARMONY_TOOLCHAIN_MISSING_RE.test(output)) return {
|
|
60
|
+
kind: "harmony-toolchain-missing",
|
|
61
|
+
message: "当前环境缺少 Harmony 运行所需工具链。",
|
|
62
|
+
hint: "请安装 DevEco Studio,或设置 HDC_PATH/E2E_HBUILDERX_HARMONY_HDC_PATH 指向 hdc。"
|
|
63
|
+
};
|
|
64
|
+
if (CLI_NOT_FOUND_RE.test(output)) return {
|
|
65
|
+
kind: "cli-not-found",
|
|
66
|
+
message: "未找到 HBuilderX CLI。",
|
|
67
|
+
hint: "请启动/安装 HBuilderX,或设置 HBUILDERX_CLI_PATH 指向 HBuilderX CLI。"
|
|
68
|
+
};
|
|
69
|
+
if (PROJECT_NOT_UNI_APP_RE.test(output)) return {
|
|
70
|
+
kind: "project-not-uni-app",
|
|
71
|
+
message: "HBuilderX 将当前目录识别为非 uni-app 项目。",
|
|
72
|
+
hint: "请确认传入的是 HBuilderX 可识别的项目根目录,并检查 manifest.json、pages.json 与导入状态。"
|
|
73
|
+
};
|
|
74
|
+
const unsupported = output.match(PROJECT_TYPE_UNSUPPORTED_RE);
|
|
75
|
+
if (unsupported) return {
|
|
76
|
+
kind: "project-type-unsupported",
|
|
77
|
+
message: `HBuilderX 不支持当前项目类型${unsupported[1] ? `:${unsupported[1].trim()}` : ""}。`,
|
|
78
|
+
hint: "请确认项目已按目标平台导入,必要时先 close/open 项目后再 launch。"
|
|
79
|
+
};
|
|
80
|
+
if (CONFIG_LOAD_FAILED_RE.test(output)) return {
|
|
81
|
+
kind: "config-load-failed",
|
|
82
|
+
message: "HBuilderX/Vite 配置加载失败。",
|
|
83
|
+
hint: "请先构建 workspace 依赖并检查 vite.config.ts 中的包入口。输出日志里通常包含具体 import。"
|
|
84
|
+
};
|
|
85
|
+
return {
|
|
86
|
+
kind: "unknown",
|
|
87
|
+
message: "HBuilderX 命令失败,未匹配到已知分类。"
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function createTimeoutIssue() {
|
|
91
|
+
return {
|
|
92
|
+
kind: "timeout",
|
|
93
|
+
message: "HBuilderX 命令在限定时间内没有结束。",
|
|
94
|
+
hint: "请检查 HBuilderX 是否卡在项目导入、设备选择、运行基座安装或平台编译阶段;最近日志通常包含停住的位置。"
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function formatRecentLogs(logs, maxLength = 6e3) {
|
|
98
|
+
const output = logs.join("");
|
|
99
|
+
return output.length > maxLength ? output.slice(-maxLength) : output;
|
|
100
|
+
}
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/process.ts
|
|
103
|
+
var HBuilderXCommandError = class extends Error {
|
|
104
|
+
result;
|
|
105
|
+
constructor(message, result) {
|
|
106
|
+
super(message);
|
|
107
|
+
this.name = "HBuilderXCommandError";
|
|
108
|
+
this.result = result;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
function killProcessTree(child, signal = "SIGTERM") {
|
|
112
|
+
const pid = child.pid;
|
|
113
|
+
if (!pid || child.exitCode != null) return;
|
|
114
|
+
if (process.platform === "win32") {
|
|
115
|
+
spawnSync("taskkill", [
|
|
116
|
+
"/pid",
|
|
117
|
+
String(pid),
|
|
118
|
+
"/t",
|
|
119
|
+
"/f"
|
|
120
|
+
], {
|
|
121
|
+
stdio: "ignore",
|
|
122
|
+
windowsHide: true
|
|
123
|
+
});
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
try {
|
|
127
|
+
process.kill(-pid, signal);
|
|
128
|
+
} catch {
|
|
129
|
+
try {
|
|
130
|
+
child.kill(signal);
|
|
131
|
+
} catch {}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function createCommandResult(options, exit, logs, timedOut = false) {
|
|
135
|
+
const output = formatRecentLogs(logs);
|
|
136
|
+
return {
|
|
137
|
+
command: options.command,
|
|
138
|
+
args: options.args,
|
|
139
|
+
cwd: options.cwd,
|
|
140
|
+
exit,
|
|
141
|
+
logs,
|
|
142
|
+
output,
|
|
143
|
+
issue: timedOut ? createTimeoutIssue() : classifyHBuilderXOutput(output)
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function buildCommandError(prefix, options, exit, logs, timedOut = false) {
|
|
147
|
+
const result = createCommandResult(options, exit, logs, timedOut);
|
|
148
|
+
return new HBuilderXCommandError([
|
|
149
|
+
`${prefix}: ${options.command} ${options.args.join(" ")}`,
|
|
150
|
+
`cwd=${options.cwd}`,
|
|
151
|
+
`exit=${exit.signal ?? exit.code}`,
|
|
152
|
+
`issue=${result.issue.kind}: ${result.issue.message}`,
|
|
153
|
+
result.issue.hint ? `hint=${result.issue.hint}` : "",
|
|
154
|
+
result.output
|
|
155
|
+
].filter(Boolean).join("\n"), result);
|
|
156
|
+
}
|
|
157
|
+
function spawnCommand(options) {
|
|
158
|
+
const child = spawn(options.command, options.args, {
|
|
159
|
+
cwd: options.cwd,
|
|
160
|
+
detached: options.detached ?? process.platform !== "win32",
|
|
161
|
+
shell: process.platform === "win32",
|
|
162
|
+
stdio: options.stdio === "inherit" ? "inherit" : [
|
|
163
|
+
"ignore",
|
|
164
|
+
"pipe",
|
|
165
|
+
"pipe"
|
|
166
|
+
],
|
|
167
|
+
env: {
|
|
168
|
+
...process.env,
|
|
169
|
+
...options.env,
|
|
170
|
+
NODE_OPTIONS: options.env?.NODE_OPTIONS ?? process.env.NODE_OPTIONS ?? "--max-old-space-size=8192"
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
const logs = options.stdio === "inherit" ? [] : collectProcessOutput(child);
|
|
174
|
+
let exit;
|
|
175
|
+
const closed = new Promise((resolve) => {
|
|
176
|
+
child.on("close", (code, signal) => {
|
|
177
|
+
exit = {
|
|
178
|
+
code,
|
|
179
|
+
signal
|
|
180
|
+
};
|
|
181
|
+
resolve(exit);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
return {
|
|
185
|
+
child,
|
|
186
|
+
logs,
|
|
187
|
+
command: options.command,
|
|
188
|
+
args: options.args,
|
|
189
|
+
cwd: options.cwd,
|
|
190
|
+
closed,
|
|
191
|
+
ensureRunning() {
|
|
192
|
+
if (exit && exit.code !== 0) throw buildCommandError("命令提前退出", options, exit, logs);
|
|
193
|
+
},
|
|
194
|
+
async stop(signal = "SIGTERM") {
|
|
195
|
+
killProcessTree(child, signal);
|
|
196
|
+
await Promise.race([closed, wait(5e3)]);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
async function runCommand(options) {
|
|
201
|
+
const spawned = spawnCommand(options);
|
|
202
|
+
let timer;
|
|
203
|
+
let timedOut = false;
|
|
204
|
+
try {
|
|
205
|
+
const exit = await (options.timeoutMs == null ? spawned.closed : Promise.race([spawned.closed, new Promise((resolve) => {
|
|
206
|
+
timer = setTimeout(() => {
|
|
207
|
+
timedOut = true;
|
|
208
|
+
killProcessTree(spawned.child);
|
|
209
|
+
resolve({
|
|
210
|
+
code: null,
|
|
211
|
+
signal: "SIGTERM"
|
|
212
|
+
});
|
|
213
|
+
}, options.timeoutMs);
|
|
214
|
+
})]));
|
|
215
|
+
const result = createCommandResult(options, exit, spawned.logs, timedOut);
|
|
216
|
+
if (exit.code === 0 || options.allowFailure) return result;
|
|
217
|
+
throw buildCommandError(options.timeoutMs != null && exit.code == null ? "命令超时" : "命令失败", options, exit, spawned.logs, timedOut);
|
|
218
|
+
} finally {
|
|
219
|
+
if (timer) clearTimeout(timer);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function createProcessExitTracker(child) {
|
|
223
|
+
let exit;
|
|
224
|
+
return {
|
|
225
|
+
closed: new Promise((resolve) => {
|
|
226
|
+
child.on("close", (code, signal) => {
|
|
227
|
+
exit = {
|
|
228
|
+
code,
|
|
229
|
+
signal
|
|
230
|
+
};
|
|
231
|
+
resolve(exit);
|
|
232
|
+
});
|
|
233
|
+
}),
|
|
234
|
+
ensureRunning(logs) {
|
|
235
|
+
if (exit && exit.code !== 0) {
|
|
236
|
+
const result = createCommandResult({
|
|
237
|
+
command: "hbuilderx",
|
|
238
|
+
args: ["launch"],
|
|
239
|
+
cwd: process.cwd()
|
|
240
|
+
}, exit, logs);
|
|
241
|
+
throw new HBuilderXCommandError(`HBuilderX launch 提前退出\nissue=${result.issue.kind}: ${result.issue.message}\n${result.output}`, result);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
//#endregion
|
|
247
|
+
//#region src/hbuilderx.ts
|
|
248
|
+
const macOSDefaultCli = "/Applications/HBuilderX.app/Contents/MacOS/cli";
|
|
249
|
+
function getDefaultHBuilderXCliCandidates(env = process.env) {
|
|
250
|
+
return [env.HBUILDERX_CLI_PATH, process.platform === "darwin" ? macOSDefaultCli : void 0].filter((item) => Boolean(item));
|
|
251
|
+
}
|
|
252
|
+
function extractHBuilderXMacOSExecutable(line) {
|
|
253
|
+
return line.match(/(\/.*?HBuilderX[^/]*\.app\/Contents\/MacOS\/HBuilderX)(?:\s|$)/i)?.[1];
|
|
254
|
+
}
|
|
255
|
+
function extractHBuilderXWindowsExecutable(line) {
|
|
256
|
+
const executable = line.split(",").at(-1)?.trim();
|
|
257
|
+
return executable?.toLowerCase().endsWith("hbuilderx.exe") ? executable : void 0;
|
|
258
|
+
}
|
|
259
|
+
function extractHBuilderXExecutableFromProcessOutput(output, platform = process.platform) {
|
|
260
|
+
for (const line of output.split(/\r?\n/)) {
|
|
261
|
+
const executable = platform === "win32" ? extractHBuilderXWindowsExecutable(line) : extractHBuilderXMacOSExecutable(line);
|
|
262
|
+
if (executable) return executable;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
async function findRunningHBuilderXCli(platform = process.platform) {
|
|
266
|
+
const result = platform === "win32" ? spawnSync("wmic", [
|
|
267
|
+
"process",
|
|
268
|
+
"where",
|
|
269
|
+
"name='HBuilderX.exe'",
|
|
270
|
+
"get",
|
|
271
|
+
"executablepath",
|
|
272
|
+
"/format:csv"
|
|
273
|
+
], {
|
|
274
|
+
encoding: "utf8",
|
|
275
|
+
windowsHide: true
|
|
276
|
+
}) : spawnSync("ps", [
|
|
277
|
+
"-ax",
|
|
278
|
+
"-o",
|
|
279
|
+
"command="
|
|
280
|
+
], { encoding: "utf8" });
|
|
281
|
+
if (result.error || result.status !== 0) return;
|
|
282
|
+
const executable = extractHBuilderXExecutableFromProcessOutput(`${result.stdout ?? ""}${result.stderr ?? ""}`, platform);
|
|
283
|
+
if (!executable || !await fileExists(executable)) return;
|
|
284
|
+
const cli = path.join(path.dirname(executable), platform === "win32" ? "cli.exe" : "cli");
|
|
285
|
+
return await fileExists(cli) ? cli : void 0;
|
|
286
|
+
}
|
|
287
|
+
function resolveCandidateSource(candidate, env) {
|
|
288
|
+
if (candidate === env.HBUILDERX_CLI_PATH) return "env";
|
|
289
|
+
if (candidate === macOSDefaultCli) return "default-path";
|
|
290
|
+
return "candidate";
|
|
291
|
+
}
|
|
292
|
+
async function resolveHBuilderXCliInfo(candidates, env = process.env) {
|
|
293
|
+
if (!candidates) {
|
|
294
|
+
const runningCli = await findRunningHBuilderXCli();
|
|
295
|
+
if (runningCli) return {
|
|
296
|
+
path: runningCli,
|
|
297
|
+
isRunning: true,
|
|
298
|
+
source: "running-process"
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
const items = candidates ?? getDefaultHBuilderXCliCandidates(env);
|
|
302
|
+
for (const item of items) if (await fileExists(item)) return {
|
|
303
|
+
path: item,
|
|
304
|
+
isRunning: false,
|
|
305
|
+
source: resolveCandidateSource(item, env)
|
|
306
|
+
};
|
|
307
|
+
throw new Error("未找到 HBuilderX CLI。请先安装 HBuilderX,或设置 HBUILDERX_CLI_PATH=/path/to/cli。");
|
|
308
|
+
}
|
|
309
|
+
async function resolveHBuilderXCli(candidates) {
|
|
310
|
+
return (await resolveHBuilderXCliInfo(candidates)).path;
|
|
311
|
+
}
|
|
312
|
+
function createHBuilderXEnv(options = {}) {
|
|
313
|
+
return {
|
|
314
|
+
...options.env,
|
|
315
|
+
HBUILDERX_CLI_PATH: options.hbuilderxCliPath ?? options.env?.HBUILDERX_CLI_PATH ?? process.env.HBUILDERX_CLI_PATH ?? getDefaultHBuilderXCliCandidates()[0],
|
|
316
|
+
NODE_OPTIONS: options.env?.NODE_OPTIONS ?? process.env.NODE_OPTIONS ?? "--max-old-space-size=8192"
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
function hbuilderxPnpmArgs(args) {
|
|
320
|
+
return [
|
|
321
|
+
"exec",
|
|
322
|
+
"hbuilderx",
|
|
323
|
+
...args
|
|
324
|
+
];
|
|
325
|
+
}
|
|
326
|
+
async function runPnpmCommand(options) {
|
|
327
|
+
return await runCommand({
|
|
328
|
+
command: "pnpm",
|
|
329
|
+
args: options.args,
|
|
330
|
+
cwd: options.cwd,
|
|
331
|
+
timeoutMs: options.timeoutMs,
|
|
332
|
+
allowFailure: options.allowFailure,
|
|
333
|
+
env: createHBuilderXEnv(options)
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
function spawnPnpmCommand(options) {
|
|
337
|
+
return spawnCommand({
|
|
338
|
+
command: "pnpm",
|
|
339
|
+
args: options.args,
|
|
340
|
+
cwd: options.cwd,
|
|
341
|
+
env: createHBuilderXEnv(options)
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
async function closeProject(options) {
|
|
345
|
+
return await runPnpmCommand({
|
|
346
|
+
...options,
|
|
347
|
+
args: hbuilderxPnpmArgs([
|
|
348
|
+
"project",
|
|
349
|
+
"close",
|
|
350
|
+
"--path",
|
|
351
|
+
path.resolve(options.cwd)
|
|
352
|
+
]),
|
|
353
|
+
allowFailure: options.allowFailure ?? true
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
async function openProject(options) {
|
|
357
|
+
return await runPnpmCommand({
|
|
358
|
+
...options,
|
|
359
|
+
args: hbuilderxPnpmArgs([
|
|
360
|
+
"project",
|
|
361
|
+
"open",
|
|
362
|
+
"--path",
|
|
363
|
+
path.resolve(options.cwd)
|
|
364
|
+
])
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
async function prepareProject(options) {
|
|
368
|
+
await closeProject({
|
|
369
|
+
...options,
|
|
370
|
+
allowFailure: true
|
|
371
|
+
});
|
|
372
|
+
return await openProject(options);
|
|
373
|
+
}
|
|
374
|
+
function createLaunchArgs(options) {
|
|
375
|
+
const args = [
|
|
376
|
+
"launch",
|
|
377
|
+
options.platform,
|
|
378
|
+
"--project",
|
|
379
|
+
path.resolve(options.cwd),
|
|
380
|
+
...options.args ?? []
|
|
381
|
+
];
|
|
382
|
+
if (options.compile != null && !args.includes("--compile")) args.push("--compile", String(options.compile));
|
|
383
|
+
if (options.runtimeLog != null && !args.includes("--runtime-log")) args.push("--runtime-log", String(options.runtimeLog));
|
|
384
|
+
return hbuilderxPnpmArgs(args);
|
|
385
|
+
}
|
|
386
|
+
async function launchProject(options) {
|
|
387
|
+
return await runPnpmCommand({
|
|
388
|
+
...options,
|
|
389
|
+
args: createLaunchArgs(options)
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
function startLaunch(options) {
|
|
393
|
+
return spawnPnpmCommand({
|
|
394
|
+
...options,
|
|
395
|
+
args: createLaunchArgs(options)
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
//#endregion
|
|
399
|
+
//#region src/toolchains.ts
|
|
400
|
+
function runTool(command, args) {
|
|
401
|
+
const result = spawnSync(command, args, { encoding: "utf8" });
|
|
402
|
+
return {
|
|
403
|
+
ok: result.status === 0,
|
|
404
|
+
output: `${result.stdout ?? ""}${result.stderr ?? ""}`.trim(),
|
|
405
|
+
status: result.status,
|
|
406
|
+
signal: result.signal
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
function resolveAdbCommand(env = process.env) {
|
|
410
|
+
const adb = runTool("adb", ["version"]);
|
|
411
|
+
if (adb.ok) return {
|
|
412
|
+
command: "adb",
|
|
413
|
+
output: adb.output,
|
|
414
|
+
env: {}
|
|
415
|
+
};
|
|
416
|
+
const candidates = [
|
|
417
|
+
env.ANDROID_HOME,
|
|
418
|
+
env.ANDROID_SDK_ROOT,
|
|
419
|
+
env.HOME ? path.join(env.HOME, "Library/Android/sdk") : void 0,
|
|
420
|
+
"/opt/android-sdk",
|
|
421
|
+
"/usr/local/share/android-sdk"
|
|
422
|
+
].filter((item) => Boolean(item));
|
|
423
|
+
for (const sdkRoot of candidates) {
|
|
424
|
+
const adbPath = path.join(sdkRoot, "platform-tools", process.platform === "win32" ? "adb.exe" : "adb");
|
|
425
|
+
const candidate = runTool(adbPath, ["version"]);
|
|
426
|
+
if (candidate.ok) return {
|
|
427
|
+
command: adbPath,
|
|
428
|
+
output: candidate.output,
|
|
429
|
+
env: {
|
|
430
|
+
ANDROID_HOME: env.ANDROID_HOME ?? sdkRoot,
|
|
431
|
+
ANDROID_SDK_ROOT: env.ANDROID_SDK_ROOT ?? sdkRoot,
|
|
432
|
+
PATH: `${path.dirname(adbPath)}${path.delimiter}${env.PATH ?? ""}`
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
return {
|
|
437
|
+
command: "adb",
|
|
438
|
+
output: adb.output,
|
|
439
|
+
env: void 0
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
function assertAndroidToolchain(env = process.env) {
|
|
443
|
+
const adb = resolveAdbCommand(env);
|
|
444
|
+
if (!adb.env && adb.command === "adb") throw new Error([
|
|
445
|
+
"当前机器缺少 Android App E2E 所需的 adb,无法运行 HBuilderX app-android E2E。",
|
|
446
|
+
`adb: ${adb.output || "not found"}`,
|
|
447
|
+
"请先安装 Android SDK platform-tools,并确认 adb 可在 PATH 中访问。",
|
|
448
|
+
"如需指定设备,请设置 E2E_HBUILDERX_ANDROID_DEVICE_ID。"
|
|
449
|
+
].join("\n"));
|
|
450
|
+
return adb.env;
|
|
451
|
+
}
|
|
452
|
+
function assertIosSimulatorToolchain(env = process.env) {
|
|
453
|
+
if (process.platform !== "darwin") throw new Error("HBuilderX iOS 模拟器 E2E 只能在 macOS 本地运行。");
|
|
454
|
+
const xcodeSelect = runTool("xcode-select", ["-p"]);
|
|
455
|
+
const simctl = runTool("xcrun", ["--find", "simctl"]);
|
|
456
|
+
const xcodebuild = runTool("xcrun", ["--find", "xcodebuild"]);
|
|
457
|
+
const firstLaunchStatus = xcodebuild.ok ? runTool("xcodebuild", ["-checkFirstLaunchStatus"]) : void 0;
|
|
458
|
+
if (!simctl.ok || !xcodebuild.ok || firstLaunchStatus?.ok === false) throw new Error([
|
|
459
|
+
"当前机器缺少 iOS 模拟器所需的完整 Xcode 工具链,无法运行 HBuilderX app-ios E2E。",
|
|
460
|
+
`xcode-select: ${xcodeSelect.output || "unknown"}`,
|
|
461
|
+
`DEVELOPER_DIR: ${env.DEVELOPER_DIR || "未设置"}`,
|
|
462
|
+
`simctl: ${simctl.output || "not found"}`,
|
|
463
|
+
`xcodebuild: ${xcodebuild.output || "not found"}`,
|
|
464
|
+
`firstLaunchStatus: ${firstLaunchStatus?.output || (firstLaunchStatus?.ok === false ? "not ready" : "unknown")}`,
|
|
465
|
+
"请安装完整 Xcode,并执行 sudo xcode-select -s /Applications/Xcode.app/Contents/Developer,或用 DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer 临时指定后重试。",
|
|
466
|
+
"如果 firstLaunchStatus 不是 ready,请先打开 Xcode 完成首次组件安装,或执行 sudo xcodebuild -runFirstLaunch。"
|
|
467
|
+
].join("\n"));
|
|
468
|
+
}
|
|
469
|
+
function resolveHdcCommand(env = process.env) {
|
|
470
|
+
const candidates = [
|
|
471
|
+
env.DEMO_VISUAL_HARMONY_HDC_PATH,
|
|
472
|
+
env.E2E_HBUILDERX_HARMONY_HDC_PATH,
|
|
473
|
+
env.HDC_PATH,
|
|
474
|
+
"hdc",
|
|
475
|
+
process.platform === "darwin" ? "/Applications/DevEco-Studio.app/Contents/sdk/default/openharmony/toolchains/hdc" : void 0
|
|
476
|
+
].filter((item) => Boolean(item));
|
|
477
|
+
for (const candidate of candidates) if (runTool(candidate, ["version"]).ok) return candidate;
|
|
478
|
+
throw new Error([
|
|
479
|
+
"当前机器缺少 Harmony App E2E 所需的 hdc,无法运行 HBuilderX app-harmony E2E。",
|
|
480
|
+
"请安装 DevEco Studio,或设置 DEMO_VISUAL_HARMONY_HDC_PATH=/path/to/hdc。",
|
|
481
|
+
"如需指定设备,请设置 E2E_HBUILDERX_HARMONY_DEVICE_ID 或 DEMO_VISUAL_HARMONY_DEVICE_ID。"
|
|
482
|
+
].join("\n"));
|
|
483
|
+
}
|
|
484
|
+
function assertHarmonyToolchain(env = process.env) {
|
|
485
|
+
const hdc = resolveHdcCommand(env);
|
|
486
|
+
const result = runTool(hdc, ["list", "targets"]);
|
|
487
|
+
if (!result.ok) {
|
|
488
|
+
const issue = classifyHBuilderXOutput(result.output);
|
|
489
|
+
throw new Error([
|
|
490
|
+
"当前 Harmony hdc 无法列出设备,无法运行 HBuilderX app-harmony E2E。",
|
|
491
|
+
result.output || `exit=${result.status} signal=${result.signal ?? "none"}`,
|
|
492
|
+
`issue=${issue.kind}: ${issue.message}`,
|
|
493
|
+
"请确认 Harmony 模拟器或真机已连接。"
|
|
494
|
+
].join("\n"));
|
|
495
|
+
}
|
|
496
|
+
return hdc;
|
|
497
|
+
}
|
|
498
|
+
//#endregion
|
|
499
|
+
export { HBuilderXCommandError, assertAndroidToolchain, assertHarmonyToolchain, assertIosSimulatorToolchain, classifyHBuilderXOutput, closeProject, collectProcessOutput, createHBuilderXEnv, createLaunchArgs, createLogBuffer, createProcessExitTracker, createTimeoutIssue, extractHBuilderXExecutableFromProcessOutput, extractHBuilderXMacOSExecutable, extractHBuilderXWindowsExecutable, fileExists, findRunningHBuilderXCli, formatRecentLogs, getDefaultHBuilderXCliCandidates, hbuilderxPnpmArgs, killProcessTree, launchProject, openProject, prepareProject, readUtf8, resolveAdbCommand, resolveHBuilderXCli, resolveHBuilderXCliInfo, resolveHdcCommand, runCommand, runPnpmCommand, runTool, spawnCommand, spawnPnpmCommand, startLaunch, wait };
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@weapp-tailwindcss/hbuilderx-runner",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Stable HBuilderX CLI runner utilities for weapp-tailwindcss local e2e workflows.",
|
|
5
|
+
"author": "ice breaker <1324318532@qq.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/sonofmagic/weapp-tailwindcss.git",
|
|
10
|
+
"directory": "packages/hbuilderx-runner"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/sonofmagic/weapp-tailwindcss/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"hbuilderx",
|
|
17
|
+
"uni-app",
|
|
18
|
+
"uni-app-x",
|
|
19
|
+
"android",
|
|
20
|
+
"ios",
|
|
21
|
+
"harmony"
|
|
22
|
+
],
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.mjs",
|
|
28
|
+
"require": "./dist/index.cjs"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"main": "./dist/index.cjs",
|
|
32
|
+
"module": "./dist/index.mjs",
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"files": [
|
|
35
|
+
"dist"
|
|
36
|
+
],
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"dev": "tsdown --watch --sourcemap",
|
|
46
|
+
"build": "tsdown",
|
|
47
|
+
"test": "vitest run",
|
|
48
|
+
"test:dev": "vitest",
|
|
49
|
+
"release": "pnpm publish",
|
|
50
|
+
"lint": "eslint .",
|
|
51
|
+
"lint:fix": "eslint . --fix"
|
|
52
|
+
}
|
|
53
|
+
}
|