@weapp-tailwindcss/hbuilderx-runner 0.0.4 → 0.1.1
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/README.en.md +27 -10
- package/README.md +27 -10
- package/dist/index.cjs +449 -93
- package/dist/index.d.ts +67 -5
- package/dist/index.mjs +434 -94
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import fs from "node:fs/promises";
|
|
2
|
-
import { spawn, spawnSync } from "node:child_process";
|
|
3
2
|
import path from "node:path";
|
|
4
3
|
import process from "node:process";
|
|
4
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
5
5
|
//#region src/fs.ts
|
|
6
6
|
async function fileExists(file) {
|
|
7
7
|
try {
|
|
@@ -18,24 +18,180 @@ function wait(ms) {
|
|
|
18
18
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
19
19
|
}
|
|
20
20
|
//#endregion
|
|
21
|
+
//#region src/hbuilderx/discovery.ts
|
|
22
|
+
const macOSStableCli = "/Applications/HBuilderX.app/Contents/MacOS/cli";
|
|
23
|
+
const macOSAlphaCli = "/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli";
|
|
24
|
+
function unique(items) {
|
|
25
|
+
return [...new Set(items)];
|
|
26
|
+
}
|
|
27
|
+
function normalizeFile(file, platform = process.platform) {
|
|
28
|
+
const normalized = path.normalize(file);
|
|
29
|
+
return platform === "win32" ? normalized.toLowerCase() : normalized;
|
|
30
|
+
}
|
|
31
|
+
function resolveHBuilderXChannel(channel) {
|
|
32
|
+
const value = channel ?? "auto";
|
|
33
|
+
if (value === "auto" || value === "stable" || value === "alpha") return value;
|
|
34
|
+
throw new Error(`无效的 HBuilderX channel:${value}。可选值为 auto、stable、alpha。`);
|
|
35
|
+
}
|
|
36
|
+
function inferHBuilderXChannel(file) {
|
|
37
|
+
if (/HBuilderX(?:-|\s*)Alpha(?:\.app)?[\\/]/i.test(file) || /(?:^|[\\/])alpha(?:[\\/]|$)/i.test(file)) return "alpha";
|
|
38
|
+
if (/HBuilderX(?:\.app)?[\\/]/i.test(file) || /HBuilderX\.exe$/i.test(file)) return "stable";
|
|
39
|
+
return "unknown";
|
|
40
|
+
}
|
|
41
|
+
function getDefaultHBuilderXCliCandidates(env = process.env, channel = resolveHBuilderXChannel(env.HBUILDERX_CHANNEL), platform = process.platform) {
|
|
42
|
+
const defaults = platform === "darwin" ? channel === "stable" ? [macOSStableCli] : channel === "alpha" ? [macOSAlphaCli] : [macOSStableCli, macOSAlphaCli] : [];
|
|
43
|
+
return unique([env.HBUILDERX_CLI_PATH, ...defaults].filter((item) => Boolean(item)));
|
|
44
|
+
}
|
|
45
|
+
function extractHBuilderXMacOSExecutables(output) {
|
|
46
|
+
return unique(Array.from(output.matchAll(/(\/[^\r\n]*?HBuilderX(?:-Alpha|\s+Alpha)?\.app\/Contents\/MacOS\/HBuilderX)(?=\s|$)/gi), (match) => match[1]));
|
|
47
|
+
}
|
|
48
|
+
function extractHBuilderXWindowsExecutables(output) {
|
|
49
|
+
const executables = [];
|
|
50
|
+
for (const line of output.split(/\r?\n/)) {
|
|
51
|
+
const executable = line.split(",").at(-1)?.trim();
|
|
52
|
+
if (executable?.toLowerCase().endsWith("hbuilderx.exe")) executables.push(executable);
|
|
53
|
+
}
|
|
54
|
+
return unique(executables);
|
|
55
|
+
}
|
|
56
|
+
function extractHBuilderXExecutablesFromProcessOutput(output, platform = process.platform) {
|
|
57
|
+
return platform === "win32" ? extractHBuilderXWindowsExecutables(output) : extractHBuilderXMacOSExecutables(output);
|
|
58
|
+
}
|
|
59
|
+
function extractHBuilderXMacOSExecutable(line) {
|
|
60
|
+
return extractHBuilderXMacOSExecutables(line)[0];
|
|
61
|
+
}
|
|
62
|
+
function extractHBuilderXWindowsExecutable(line) {
|
|
63
|
+
return extractHBuilderXWindowsExecutables(line)[0];
|
|
64
|
+
}
|
|
65
|
+
function extractHBuilderXExecutableFromProcessOutput(output, platform = process.platform) {
|
|
66
|
+
return extractHBuilderXExecutablesFromProcessOutput(output, platform)[0];
|
|
67
|
+
}
|
|
68
|
+
async function findRunningHBuilderXCliCandidates(platform = process.platform) {
|
|
69
|
+
const result = platform === "win32" ? spawnSync("wmic", [
|
|
70
|
+
"process",
|
|
71
|
+
"where",
|
|
72
|
+
"name='HBuilderX.exe'",
|
|
73
|
+
"get",
|
|
74
|
+
"executablepath",
|
|
75
|
+
"/format:csv"
|
|
76
|
+
], {
|
|
77
|
+
encoding: "utf8",
|
|
78
|
+
windowsHide: true
|
|
79
|
+
}) : spawnSync("ps", [
|
|
80
|
+
"-ax",
|
|
81
|
+
"-o",
|
|
82
|
+
"command="
|
|
83
|
+
], { encoding: "utf8" });
|
|
84
|
+
if (result.error || result.status !== 0) return [];
|
|
85
|
+
const executables = extractHBuilderXExecutablesFromProcessOutput(`${result.stdout ?? ""}${result.stderr ?? ""}`, platform);
|
|
86
|
+
const candidates = [];
|
|
87
|
+
for (const executable of executables) {
|
|
88
|
+
if (!await fileExists(executable)) continue;
|
|
89
|
+
const cli = path.join(path.dirname(executable), platform === "win32" ? "cli.exe" : "cli");
|
|
90
|
+
if (await fileExists(cli)) candidates.push(cli);
|
|
91
|
+
}
|
|
92
|
+
return unique(candidates);
|
|
93
|
+
}
|
|
94
|
+
async function findRunningHBuilderXCli(platform = process.platform) {
|
|
95
|
+
return (await findRunningHBuilderXCliCandidates(platform))[0];
|
|
96
|
+
}
|
|
97
|
+
function sortByChannel(items) {
|
|
98
|
+
const weights = {
|
|
99
|
+
stable: 0,
|
|
100
|
+
alpha: 1,
|
|
101
|
+
unknown: 2
|
|
102
|
+
};
|
|
103
|
+
return [...items].sort((left, right) => weights[inferHBuilderXChannel(left)] - weights[inferHBuilderXChannel(right)]);
|
|
104
|
+
}
|
|
105
|
+
function selectHBuilderXCliCandidatesForChannel(items, channel) {
|
|
106
|
+
if (channel === "auto") return sortByChannel(items);
|
|
107
|
+
return items.filter((item) => inferHBuilderXChannel(item) === channel);
|
|
108
|
+
}
|
|
109
|
+
function resolveCandidateSource(candidate, env) {
|
|
110
|
+
if (candidate === env.HBUILDERX_CLI_PATH) return "env";
|
|
111
|
+
if (candidate === "/Applications/HBuilderX.app/Contents/MacOS/cli" || candidate === "/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli") return "default-path";
|
|
112
|
+
return "candidate";
|
|
113
|
+
}
|
|
114
|
+
async function firstExisting(items) {
|
|
115
|
+
for (const item of items) if (await fileExists(item)) return item;
|
|
116
|
+
}
|
|
117
|
+
async function resolveHBuilderXCliInfoFromOptions(options = {}) {
|
|
118
|
+
const env = options.env ?? process.env;
|
|
119
|
+
const channel = resolveHBuilderXChannel(options.channel ?? env.HBUILDERX_CHANNEL);
|
|
120
|
+
const running = await findRunningHBuilderXCliCandidates();
|
|
121
|
+
if (options.candidates) {
|
|
122
|
+
const candidate = await firstExisting(options.candidates);
|
|
123
|
+
if (candidate) return {
|
|
124
|
+
path: candidate,
|
|
125
|
+
isRunning: running.some((item) => normalizeFile(item) === normalizeFile(candidate)),
|
|
126
|
+
source: resolveCandidateSource(candidate, env),
|
|
127
|
+
channel: inferHBuilderXChannel(candidate)
|
|
128
|
+
};
|
|
129
|
+
throw new Error("未找到显式指定的 HBuilderX CLI candidate。");
|
|
130
|
+
}
|
|
131
|
+
if (env.HBUILDERX_CLI_PATH && await fileExists(env.HBUILDERX_CLI_PATH)) return {
|
|
132
|
+
path: env.HBUILDERX_CLI_PATH,
|
|
133
|
+
isRunning: running.some((item) => normalizeFile(item) === normalizeFile(env.HBUILDERX_CLI_PATH)),
|
|
134
|
+
source: "env",
|
|
135
|
+
channel: inferHBuilderXChannel(env.HBUILDERX_CLI_PATH)
|
|
136
|
+
};
|
|
137
|
+
const runningCandidate = selectHBuilderXCliCandidatesForChannel(running, channel)[0];
|
|
138
|
+
if (runningCandidate) return {
|
|
139
|
+
path: runningCandidate,
|
|
140
|
+
isRunning: true,
|
|
141
|
+
source: "running-process",
|
|
142
|
+
channel: inferHBuilderXChannel(runningCandidate)
|
|
143
|
+
};
|
|
144
|
+
const defaultCandidate = await firstExisting(getDefaultHBuilderXCliCandidates({}, channel));
|
|
145
|
+
if (defaultCandidate) return {
|
|
146
|
+
path: defaultCandidate,
|
|
147
|
+
isRunning: false,
|
|
148
|
+
source: "default-path",
|
|
149
|
+
channel: inferHBuilderXChannel(defaultCandidate)
|
|
150
|
+
};
|
|
151
|
+
throw new Error([`未找到 HBuilderX ${channel === "auto" ? "" : `${channel} `}CLI。`, "请安装 HBuilderX,或设置 HBUILDERX_CLI_PATH=/path/to/cli。"].join(""));
|
|
152
|
+
}
|
|
153
|
+
//#endregion
|
|
21
154
|
//#region src/logs.ts
|
|
22
155
|
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 = /项目类型为(
|
|
156
|
+
const PROJECT_TYPE_UNSUPPORTED_RE = /项目类型为([^,]+),暂不支持|是\s+([^,\s][^,]*)项目,暂不支持|project\s+type.+unsupported/i;
|
|
157
|
+
const PROJECT_TARGET_UNSUPPORTED_RE = /不支持运行到\s*(鸿蒙|Harmony)|(?:Harmony|OpenHarmony).+(?:target|platform).+unsupported/i;
|
|
24
158
|
const CONFIG_LOAD_FAILED_RE = /failed to load config|error when starting dev server|Failed to resolve entry for package/i;
|
|
25
159
|
const CLI_NOT_FOUND_RE = /未找到\s*HBuilderX\s*CLI|HBUILDERX_CLI_PATH|command not found|ENOENT/i;
|
|
160
|
+
const CLI_INSTANCE_MISMATCH_RE = /当前运行的cli与正在运行的HBuilderX不匹配|cli.+(?:does not match|mismatch).+HBuilderX|try.+HBuilderX.+(?:cli|MacOS)/i;
|
|
161
|
+
const CLI_HOST_AMBIGUOUS_RE = /multiple running hbuilderx|多个.+HBuilderX.+运行|listhost.+--\s*host|--host.+listhost/i;
|
|
26
162
|
const ANDROID_TOOLCHAIN_MISSING_RE = /adb(?:\.exe)?(?:\s*[::].*)?(?:not found|ENOENT|不是内部或外部命令)|Android SDK|platform-tools/i;
|
|
27
163
|
const IOS_TOOLCHAIN_MISSING_RE = /xcrun|xcode-select|xcodebuild|simctl|DEVELOPER_DIR|iOS 模拟器|Xcode/i;
|
|
28
|
-
const
|
|
164
|
+
const HARMONY_TOOLCHAIN_MISSING_RES = [
|
|
165
|
+
/hdc(?:\.exe)?(?:\s+(?:list\s+targets|target\s+list))?\s*(?:[::]\s*)?(?:failed|not found|ENOENT|command not found)/i,
|
|
166
|
+
/未找到\s*(?:DevEco|鸿蒙)[^\r\n]*(?:SDK|工具链)/i,
|
|
167
|
+
/DevEco Studio[^\r\n]*(?:not found|未安装)/i,
|
|
168
|
+
/HDC_PATH/i
|
|
169
|
+
];
|
|
170
|
+
const HARMONY_BUILD_FAILED_RE = /Failed to resolve OhmUrl|ArkTS Compiler Error|运行包制作失败|hvigor ERROR/i;
|
|
171
|
+
const IMPORTANT_LOG_RE = /\b(?:fatal|error)\b|失败|已停止运行/i;
|
|
172
|
+
const importantLogsByBuffer = /* @__PURE__ */ new WeakMap();
|
|
29
173
|
function createLogBuffer(maxChunks = 160) {
|
|
30
174
|
const logs = [];
|
|
175
|
+
const importantLogs = [];
|
|
176
|
+
const importantLogSet = /* @__PURE__ */ new Set();
|
|
177
|
+
importantLogsByBuffer.set(logs, importantLogs);
|
|
31
178
|
return {
|
|
32
179
|
logs,
|
|
33
180
|
push(chunk) {
|
|
34
|
-
|
|
181
|
+
const text = chunk.toString();
|
|
182
|
+
logs.push(text);
|
|
35
183
|
if (logs.length > maxChunks) logs.splice(0, logs.length - maxChunks);
|
|
184
|
+
for (const item of text.split(/\r?\n/)) {
|
|
185
|
+
if (!IMPORTANT_LOG_RE.test(item)) continue;
|
|
186
|
+
const line = item.trim();
|
|
187
|
+
if (!line || importantLogSet.has(line)) continue;
|
|
188
|
+
importantLogSet.add(line);
|
|
189
|
+
importantLogs.push(line);
|
|
190
|
+
if (importantLogs.length > 24) importantLogSet.delete(importantLogs.pop());
|
|
191
|
+
}
|
|
36
192
|
},
|
|
37
193
|
text() {
|
|
38
|
-
return logs
|
|
194
|
+
return formatRecentLogs(logs);
|
|
39
195
|
}
|
|
40
196
|
};
|
|
41
197
|
}
|
|
@@ -46,6 +202,16 @@ function collectProcessOutput(child, maxChunks = 160) {
|
|
|
46
202
|
return buffer.logs;
|
|
47
203
|
}
|
|
48
204
|
function classifyHBuilderXOutput(output) {
|
|
205
|
+
if (CLI_INSTANCE_MISMATCH_RE.test(output)) return {
|
|
206
|
+
kind: "cli-instance-mismatch",
|
|
207
|
+
message: "当前 HBuilderX CLI 与目标运行实例不匹配。",
|
|
208
|
+
hint: "请使用与目标 stable/alpha 实例对应的 CLI,并通过 listhost 与 --host 绑定运行实例。"
|
|
209
|
+
};
|
|
210
|
+
if (CLI_HOST_AMBIGUOUS_RE.test(output)) return {
|
|
211
|
+
kind: "cli-host-ambiguous",
|
|
212
|
+
message: "检测到多个可用的 HBuilderX 运行实例。",
|
|
213
|
+
hint: "请设置 HBUILDERX_HOST,或在 runner options 中显式传入 host。"
|
|
214
|
+
};
|
|
49
215
|
if (ANDROID_TOOLCHAIN_MISSING_RE.test(output)) return {
|
|
50
216
|
kind: "android-toolchain-missing",
|
|
51
217
|
message: "当前环境缺少 Android 运行所需工具链。",
|
|
@@ -56,11 +222,16 @@ function classifyHBuilderXOutput(output) {
|
|
|
56
222
|
message: "当前环境缺少 iOS 模拟器或 Xcode 工具链。",
|
|
57
223
|
hint: "请安装完整 Xcode,确认 xcrun simctl/xcodebuild 可用,并完成 Xcode 首次启动组件安装。"
|
|
58
224
|
};
|
|
59
|
-
if (
|
|
225
|
+
if (HARMONY_TOOLCHAIN_MISSING_RES.some((pattern) => pattern.test(output))) return {
|
|
60
226
|
kind: "harmony-toolchain-missing",
|
|
61
227
|
message: "当前环境缺少 Harmony 运行所需工具链。",
|
|
62
228
|
hint: "请安装 DevEco Studio,或设置 HDC_PATH/E2E_HBUILDERX_HARMONY_HDC_PATH 指向 hdc。"
|
|
63
229
|
};
|
|
230
|
+
if (HARMONY_BUILD_FAILED_RE.test(output)) return {
|
|
231
|
+
kind: "harmony-build-failed",
|
|
232
|
+
message: "HBuilderX 的 Harmony 工程构建失败。",
|
|
233
|
+
hint: "请查看保留的首个 ArkTS/hvigor 错误;这类错误发生在 HBuilderX 已生成 Harmony 工程之后。"
|
|
234
|
+
};
|
|
64
235
|
if (CLI_NOT_FOUND_RE.test(output)) return {
|
|
65
236
|
kind: "cli-not-found",
|
|
66
237
|
message: "未找到 HBuilderX CLI。",
|
|
@@ -71,12 +242,21 @@ function classifyHBuilderXOutput(output) {
|
|
|
71
242
|
message: "HBuilderX 将当前目录识别为非 uni-app 项目。",
|
|
72
243
|
hint: "请确认传入的是 HBuilderX 可识别的项目根目录,并检查 manifest.json、pages.json 与导入状态。"
|
|
73
244
|
};
|
|
74
|
-
const
|
|
75
|
-
if (
|
|
245
|
+
const unsupportedTarget = output.match(PROJECT_TARGET_UNSUPPORTED_RE);
|
|
246
|
+
if (unsupportedTarget) return {
|
|
76
247
|
kind: "project-type-unsupported",
|
|
77
|
-
message: `HBuilderX
|
|
78
|
-
hint: "
|
|
248
|
+
message: `HBuilderX 不支持当前运行目标${unsupportedTarget[1] ? `:${unsupportedTarget[1]}` : ""}。`,
|
|
249
|
+
hint: "请升级到支持该项目目标的 HBuilderX channel,并确认项目路径与导入记录一致。"
|
|
79
250
|
};
|
|
251
|
+
const unsupported = output.match(PROJECT_TYPE_UNSUPPORTED_RE);
|
|
252
|
+
if (unsupported) {
|
|
253
|
+
const projectType = unsupported[1] ?? unsupported[2];
|
|
254
|
+
return {
|
|
255
|
+
kind: "project-type-unsupported",
|
|
256
|
+
message: `HBuilderX 不支持当前项目类型${projectType ? `:${projectType.trim()}` : ""}。`,
|
|
257
|
+
hint: "请确认项目已按目标平台导入,必要时先 close/open 项目后再 launch。"
|
|
258
|
+
};
|
|
259
|
+
}
|
|
80
260
|
if (CONFIG_LOAD_FAILED_RE.test(output)) return {
|
|
81
261
|
kind: "config-load-failed",
|
|
82
262
|
message: "HBuilderX/Vite 配置加载失败。",
|
|
@@ -96,7 +276,9 @@ function createTimeoutIssue() {
|
|
|
96
276
|
}
|
|
97
277
|
function formatRecentLogs(logs, maxLength = 6e3) {
|
|
98
278
|
const output = logs.join("");
|
|
99
|
-
|
|
279
|
+
const recent = output.length > maxLength ? output.slice(-maxLength) : output;
|
|
280
|
+
const important = (importantLogsByBuffer.get(logs) ?? []).filter((line) => !recent.includes(line));
|
|
281
|
+
return important.length > 0 ? `关键错误:\n${important.join("\n")}\n\n最近日志:\n${recent}` : recent;
|
|
100
282
|
}
|
|
101
283
|
//#endregion
|
|
102
284
|
//#region src/process.ts
|
|
@@ -244,70 +426,249 @@ function createProcessExitTracker(child) {
|
|
|
244
426
|
};
|
|
245
427
|
}
|
|
246
428
|
//#endregion
|
|
247
|
-
//#region src/hbuilderx.ts
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
|
|
429
|
+
//#region src/hbuilderx/runner.ts
|
|
430
|
+
const ansiRE = new RegExp(`${String.fromCharCode(27)}\\[[\\d;]*m`, "g");
|
|
431
|
+
const defaultStartupTimeoutMs = 6e4;
|
|
432
|
+
const hostPollIntervalMs = 500;
|
|
433
|
+
const maxSessionLogChunks = 160;
|
|
434
|
+
function stripAnsi(value) {
|
|
435
|
+
return value.replace(ansiRE, "");
|
|
251
436
|
}
|
|
252
|
-
function
|
|
253
|
-
return
|
|
437
|
+
function parseHBuilderXHosts(output) {
|
|
438
|
+
return [...new Set(stripAnsi(output).split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0 && !/^-?\d+:cli:|命令.+(?:不存在|错误)|not detected running/i.test(line)))];
|
|
254
439
|
}
|
|
255
|
-
function
|
|
256
|
-
|
|
257
|
-
return executable?.toLowerCase().endsWith("hbuilderx.exe") ? executable : void 0;
|
|
440
|
+
function parseHBuilderXVersion(output) {
|
|
441
|
+
return stripAnsi(output).match(/\b(\d+\.\d+(?:\.\d+)+(?:-[0-9a-z.-]+)?)\b/i)?.[1];
|
|
258
442
|
}
|
|
259
|
-
function
|
|
260
|
-
|
|
261
|
-
const executable = platform === "win32" ? extractHBuilderXWindowsExecutable(line) : extractHBuilderXMacOSExecutable(line);
|
|
262
|
-
if (executable) return executable;
|
|
263
|
-
}
|
|
443
|
+
function inferHBuilderXChannelFromVersion(version) {
|
|
444
|
+
return /(?:^|-)alpha(?:\.|$)/i.test(version) ? "alpha" : "stable";
|
|
264
445
|
}
|
|
265
|
-
|
|
266
|
-
const
|
|
267
|
-
"
|
|
268
|
-
|
|
269
|
-
"
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
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;
|
|
446
|
+
function createHBuilderXNativeLaunchArgs(options) {
|
|
447
|
+
const args = [
|
|
448
|
+
"launch",
|
|
449
|
+
options.platform,
|
|
450
|
+
"--project",
|
|
451
|
+
path.resolve(options.cwd),
|
|
452
|
+
...options.args ?? []
|
|
453
|
+
];
|
|
454
|
+
if (options.compile != null && !args.includes("--compile")) args.push("--compile", String(options.compile));
|
|
455
|
+
if (options.runtimeLog != null && !args.includes("--runtime-log")) args.push("--runtime-log", String(options.runtimeLog));
|
|
456
|
+
return args;
|
|
286
457
|
}
|
|
287
|
-
function
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
458
|
+
function createRunnerError(cliPath, cwd, args, kind, message, hint, logs) {
|
|
459
|
+
const output = formatRecentLogs(logs);
|
|
460
|
+
return new HBuilderXCommandError([
|
|
461
|
+
`${message}: ${cliPath} ${args.join(" ")}`,
|
|
462
|
+
`cwd=${cwd}`,
|
|
463
|
+
`issue=${kind}: ${message}`,
|
|
464
|
+
`hint=${hint}`,
|
|
465
|
+
output
|
|
466
|
+
].filter(Boolean).join("\n"), {
|
|
467
|
+
command: cliPath,
|
|
468
|
+
args,
|
|
469
|
+
cwd,
|
|
470
|
+
exit: {
|
|
471
|
+
code: null,
|
|
472
|
+
signal: null
|
|
473
|
+
},
|
|
474
|
+
logs,
|
|
475
|
+
output,
|
|
476
|
+
issue: {
|
|
477
|
+
kind,
|
|
478
|
+
message,
|
|
479
|
+
hint
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
function bindHost(args, host) {
|
|
484
|
+
const hostIndex = args.indexOf("--host");
|
|
485
|
+
if (hostIndex < 0) return [
|
|
486
|
+
...args,
|
|
487
|
+
"--host",
|
|
488
|
+
host
|
|
489
|
+
];
|
|
490
|
+
if (args[hostIndex + 1] !== host) throw new Error(`runner 已绑定 HBuilderX host=${host},不能改为 ${args[hostIndex + 1] ?? "空值"}。`);
|
|
491
|
+
return args;
|
|
492
|
+
}
|
|
493
|
+
function appendLogs(target, source) {
|
|
494
|
+
target.push(...source);
|
|
495
|
+
if (target.length > maxSessionLogChunks) target.splice(0, target.length - maxSessionLogChunks);
|
|
291
496
|
}
|
|
292
|
-
async function
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
497
|
+
async function listCompatibleHosts(cliPath, cwd, env, expectedChannel, explicitHost, timeoutMs) {
|
|
498
|
+
const startedAt = Date.now();
|
|
499
|
+
const logs = [];
|
|
500
|
+
do {
|
|
501
|
+
const listed = await runCommand({
|
|
502
|
+
command: cliPath,
|
|
503
|
+
args: ["listhost"],
|
|
504
|
+
cwd,
|
|
505
|
+
env,
|
|
506
|
+
timeoutMs: Math.min(timeoutMs, 1e4),
|
|
507
|
+
allowFailure: true
|
|
508
|
+
});
|
|
509
|
+
appendLogs(logs, listed.logs);
|
|
510
|
+
const hosts = explicitHost ? [explicitHost] : parseHBuilderXHosts(listed.output);
|
|
511
|
+
const compatible = [];
|
|
512
|
+
for (const host of hosts) {
|
|
513
|
+
const versionResult = await runCommand({
|
|
514
|
+
command: cliPath,
|
|
515
|
+
args: [
|
|
516
|
+
"version",
|
|
517
|
+
"--host",
|
|
518
|
+
host
|
|
519
|
+
],
|
|
520
|
+
cwd,
|
|
521
|
+
env,
|
|
522
|
+
timeoutMs: Math.min(timeoutMs, 1e4),
|
|
523
|
+
allowFailure: true
|
|
524
|
+
});
|
|
525
|
+
appendLogs(logs, versionResult.logs);
|
|
526
|
+
if (versionResult.issue.kind === "cli-instance-mismatch") return {
|
|
527
|
+
compatible: [],
|
|
528
|
+
logs
|
|
529
|
+
};
|
|
530
|
+
const version = versionResult.exit.code === 0 ? parseHBuilderXVersion(versionResult.output) : void 0;
|
|
531
|
+
if (!version) continue;
|
|
532
|
+
const channel = inferHBuilderXChannelFromVersion(version);
|
|
533
|
+
if (expectedChannel === "unknown" || channel === expectedChannel) compatible.push({
|
|
534
|
+
host,
|
|
535
|
+
version,
|
|
536
|
+
channel
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
if (compatible.length > 0) return {
|
|
540
|
+
compatible,
|
|
541
|
+
logs
|
|
299
542
|
};
|
|
543
|
+
await wait(hostPollIntervalMs);
|
|
544
|
+
} while (Date.now() - startedAt < timeoutMs);
|
|
545
|
+
return {
|
|
546
|
+
compatible: [],
|
|
547
|
+
logs
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
async function createHBuilderXRunner(options = {}) {
|
|
551
|
+
const env = {
|
|
552
|
+
...process.env,
|
|
553
|
+
...options.env
|
|
554
|
+
};
|
|
555
|
+
const cwd = options.cwd ?? process.cwd();
|
|
556
|
+
const timeoutMs = options.timeoutMs ?? defaultStartupTimeoutMs;
|
|
557
|
+
const resolution = await resolveHBuilderXCliInfoFromOptions({
|
|
558
|
+
candidates: options.hbuilderxCliPath ? [options.hbuilderxCliPath] : options.candidates,
|
|
559
|
+
channel: options.channel,
|
|
560
|
+
env,
|
|
561
|
+
host: options.host
|
|
562
|
+
});
|
|
563
|
+
const cliEnv = {
|
|
564
|
+
...env,
|
|
565
|
+
HBUILDERX_CLI_PATH: resolution.path
|
|
566
|
+
};
|
|
567
|
+
if (!resolution.isRunning) {
|
|
568
|
+
const opened = await runCommand({
|
|
569
|
+
command: resolution.path,
|
|
570
|
+
args: ["open"],
|
|
571
|
+
cwd,
|
|
572
|
+
env: cliEnv,
|
|
573
|
+
timeoutMs,
|
|
574
|
+
allowFailure: true
|
|
575
|
+
});
|
|
576
|
+
const openIssue = classifyHBuilderXOutput(opened.output);
|
|
577
|
+
if (opened.exit.code !== 0 || openIssue.kind === "cli-instance-mismatch" || openIssue.kind === "cli-host-ambiguous") throw createRunnerError(resolution.path, cwd, ["open"], openIssue.kind === "cli-host-ambiguous" ? "cli-host-ambiguous" : "cli-instance-mismatch", openIssue.message, openIssue.hint ?? "请检查目标 HBuilderX 运行实例。", opened.logs);
|
|
300
578
|
}
|
|
301
|
-
const
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
579
|
+
const requestedChannel = resolveHBuilderXChannel(options.channel ?? env.HBUILDERX_CHANNEL);
|
|
580
|
+
const expectedChannel = resolution.channel !== "unknown" ? resolution.channel : requestedChannel === "auto" ? inferHBuilderXChannel(resolution.path) : requestedChannel;
|
|
581
|
+
const explicitHost = options.host ?? env.HBUILDERX_HOST;
|
|
582
|
+
const { compatible, logs } = await listCompatibleHosts(resolution.path, cwd, cliEnv, expectedChannel, explicitHost, timeoutMs);
|
|
583
|
+
if (compatible.length === 0) throw createRunnerError(resolution.path, cwd, ["listhost"], "cli-instance-mismatch", `未找到与 ${expectedChannel} CLI 匹配的 HBuilderX host`, "请启动对应版本,或检查 HBUILDERX_CLI_PATH、HBUILDERX_CHANNEL 和 HBUILDERX_HOST。", logs);
|
|
584
|
+
if (compatible.length > 1) throw createRunnerError(resolution.path, cwd, ["listhost"], "cli-host-ambiguous", `检测到多个匹配的 HBuilderX host:${compatible.map((item) => item.host).join(", ")}`, "请设置 HBUILDERX_HOST,或在 createHBuilderXRunner options 中显式传入 host。", logs);
|
|
585
|
+
const selected = compatible[0];
|
|
586
|
+
const boundResolution = {
|
|
587
|
+
...resolution,
|
|
588
|
+
isRunning: true,
|
|
589
|
+
channel: selected.channel,
|
|
590
|
+
host: selected.host,
|
|
591
|
+
version: selected.version
|
|
306
592
|
};
|
|
307
|
-
|
|
593
|
+
const run = async (commandOptions) => await runCommand({
|
|
594
|
+
...commandOptions,
|
|
595
|
+
command: resolution.path,
|
|
596
|
+
args: bindHost(commandOptions.args, selected.host),
|
|
597
|
+
cwd: commandOptions.cwd ?? cwd,
|
|
598
|
+
env: {
|
|
599
|
+
...cliEnv,
|
|
600
|
+
...commandOptions.env
|
|
601
|
+
}
|
|
602
|
+
});
|
|
603
|
+
const spawn = (commandOptions) => spawnCommand({
|
|
604
|
+
...commandOptions,
|
|
605
|
+
command: resolution.path,
|
|
606
|
+
args: bindHost(commandOptions.args, selected.host),
|
|
607
|
+
cwd: commandOptions.cwd ?? cwd,
|
|
608
|
+
env: {
|
|
609
|
+
...cliEnv,
|
|
610
|
+
...commandOptions.env
|
|
611
|
+
}
|
|
612
|
+
});
|
|
613
|
+
const runner = {
|
|
614
|
+
resolution: boundResolution,
|
|
615
|
+
run,
|
|
616
|
+
spawn,
|
|
617
|
+
async closeProject(projectOptions) {
|
|
618
|
+
return await run({
|
|
619
|
+
...projectOptions,
|
|
620
|
+
args: [
|
|
621
|
+
"project",
|
|
622
|
+
"close",
|
|
623
|
+
"--path",
|
|
624
|
+
path.resolve(projectOptions.cwd)
|
|
625
|
+
],
|
|
626
|
+
allowFailure: projectOptions.allowFailure ?? true
|
|
627
|
+
});
|
|
628
|
+
},
|
|
629
|
+
async openProject(projectOptions) {
|
|
630
|
+
return await run({
|
|
631
|
+
...projectOptions,
|
|
632
|
+
args: [
|
|
633
|
+
"project",
|
|
634
|
+
"open",
|
|
635
|
+
"--path",
|
|
636
|
+
path.resolve(projectOptions.cwd)
|
|
637
|
+
]
|
|
638
|
+
});
|
|
639
|
+
},
|
|
640
|
+
async prepareProject(projectOptions) {
|
|
641
|
+
await runner.closeProject({
|
|
642
|
+
...projectOptions,
|
|
643
|
+
allowFailure: true
|
|
644
|
+
});
|
|
645
|
+
return await runner.openProject(projectOptions);
|
|
646
|
+
},
|
|
647
|
+
async launchProject(launchOptions) {
|
|
648
|
+
return await run({
|
|
649
|
+
...launchOptions,
|
|
650
|
+
args: createHBuilderXNativeLaunchArgs(launchOptions)
|
|
651
|
+
});
|
|
652
|
+
},
|
|
653
|
+
startLaunch(launchOptions) {
|
|
654
|
+
return spawn({
|
|
655
|
+
...launchOptions,
|
|
656
|
+
args: createHBuilderXNativeLaunchArgs(launchOptions)
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
};
|
|
660
|
+
return runner;
|
|
308
661
|
}
|
|
309
|
-
|
|
310
|
-
|
|
662
|
+
//#endregion
|
|
663
|
+
//#region src/hbuilderx.ts
|
|
664
|
+
async function resolveHBuilderXCliInfo(candidatesOrOptions, env = process.env) {
|
|
665
|
+
return await resolveHBuilderXCliInfoFromOptions(Array.isArray(candidatesOrOptions) ? {
|
|
666
|
+
candidates: candidatesOrOptions,
|
|
667
|
+
env
|
|
668
|
+
} : candidatesOrOptions);
|
|
669
|
+
}
|
|
670
|
+
async function resolveHBuilderXCli(candidatesOrOptions) {
|
|
671
|
+
return (await resolveHBuilderXCliInfo(candidatesOrOptions)).path;
|
|
311
672
|
}
|
|
312
673
|
function createHBuilderXEnv(options = {}) {
|
|
313
674
|
return {
|
|
@@ -342,34 +703,13 @@ function spawnPnpmCommand(options) {
|
|
|
342
703
|
});
|
|
343
704
|
}
|
|
344
705
|
async function closeProject(options) {
|
|
345
|
-
return await
|
|
346
|
-
...options,
|
|
347
|
-
args: hbuilderxPnpmArgs([
|
|
348
|
-
"project",
|
|
349
|
-
"close",
|
|
350
|
-
"--path",
|
|
351
|
-
path.resolve(options.cwd)
|
|
352
|
-
]),
|
|
353
|
-
allowFailure: options.allowFailure ?? true
|
|
354
|
-
});
|
|
706
|
+
return await (await createHBuilderXRunner(options)).closeProject(options);
|
|
355
707
|
}
|
|
356
708
|
async function openProject(options) {
|
|
357
|
-
return await
|
|
358
|
-
...options,
|
|
359
|
-
args: hbuilderxPnpmArgs([
|
|
360
|
-
"project",
|
|
361
|
-
"open",
|
|
362
|
-
"--path",
|
|
363
|
-
path.resolve(options.cwd)
|
|
364
|
-
])
|
|
365
|
-
});
|
|
709
|
+
return await (await createHBuilderXRunner(options)).openProject(options);
|
|
366
710
|
}
|
|
367
711
|
async function prepareProject(options) {
|
|
368
|
-
await
|
|
369
|
-
...options,
|
|
370
|
-
allowFailure: true
|
|
371
|
-
});
|
|
372
|
-
return await openProject(options);
|
|
712
|
+
return await (await createHBuilderXRunner(options)).prepareProject(options);
|
|
373
713
|
}
|
|
374
714
|
function createLaunchArgs(options) {
|
|
375
715
|
const args = [
|
|
@@ -384,11 +724,11 @@ function createLaunchArgs(options) {
|
|
|
384
724
|
return hbuilderxPnpmArgs(args);
|
|
385
725
|
}
|
|
386
726
|
async function launchProject(options) {
|
|
387
|
-
return await
|
|
388
|
-
...options,
|
|
389
|
-
args: createLaunchArgs(options)
|
|
390
|
-
});
|
|
727
|
+
return await (await createHBuilderXRunner(options)).launchProject(options);
|
|
391
728
|
}
|
|
729
|
+
/**
|
|
730
|
+
* @deprecated 需要固定 stable/alpha 与 host 时,请使用 `createHBuilderXRunner().startLaunch()`。
|
|
731
|
+
*/
|
|
392
732
|
function startLaunch(options) {
|
|
393
733
|
return spawnPnpmCommand({
|
|
394
734
|
...options,
|
|
@@ -567,4 +907,4 @@ function assertHarmonyToolchain(env = process.env, deviceId) {
|
|
|
567
907
|
return hdc;
|
|
568
908
|
}
|
|
569
909
|
//#endregion
|
|
570
|
-
export { HBuilderXCommandError, assertAndroidToolchain, assertHarmonyToolchain, assertIosSimulatorToolchain, classifyHBuilderXOutput, closeProject, collectProcessOutput, createHBuilderXEnv, createLaunchArgs, createLogBuffer, createProcessExitTracker, createTimeoutIssue, extractHBuilderXExecutableFromProcessOutput, extractHBuilderXMacOSExecutable, extractHBuilderXWindowsExecutable, fileExists, findRunningHBuilderXCli, formatRecentLogs, getDefaultHBuilderXCliCandidates, hbuilderxPnpmArgs, killProcessTree, launchProject, openProject, parseHdcTargets, parseIosSimulatorDevices, prepareProject, readUtf8, resolveAdbCommand, resolveHBuilderXCli, resolveHBuilderXCliInfo, resolveHdcCommand, resolveIosSimulatorDeviceId, runCommand, runPnpmCommand, runTool, selectPreferredIosSimulatorDevice, spawnCommand, spawnPnpmCommand, startLaunch, wait };
|
|
910
|
+
export { HBuilderXCommandError, assertAndroidToolchain, assertHarmonyToolchain, assertIosSimulatorToolchain, classifyHBuilderXOutput, closeProject, collectProcessOutput, createHBuilderXEnv, createHBuilderXNativeLaunchArgs, createHBuilderXRunner, createLaunchArgs, createLogBuffer, createProcessExitTracker, createTimeoutIssue, extractHBuilderXExecutableFromProcessOutput, extractHBuilderXExecutablesFromProcessOutput, extractHBuilderXMacOSExecutable, extractHBuilderXMacOSExecutables, extractHBuilderXWindowsExecutable, extractHBuilderXWindowsExecutables, fileExists, findRunningHBuilderXCli, findRunningHBuilderXCliCandidates, formatRecentLogs, getDefaultHBuilderXCliCandidates, hbuilderxPnpmArgs, inferHBuilderXChannel, inferHBuilderXChannelFromVersion, killProcessTree, launchProject, macOSAlphaCli, macOSStableCli, openProject, parseHBuilderXHosts, parseHBuilderXVersion, parseHdcTargets, parseIosSimulatorDevices, prepareProject, readUtf8, resolveAdbCommand, resolveHBuilderXChannel, resolveHBuilderXCli, resolveHBuilderXCliInfo, resolveHBuilderXCliInfoFromOptions, resolveHdcCommand, resolveIosSimulatorDeviceId, runCommand, runPnpmCommand, runTool, selectHBuilderXCliCandidatesForChannel, selectPreferredIosSimulatorDevice, spawnCommand, spawnPnpmCommand, startLaunch, stripAnsi, wait };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weapp-tailwindcss/hbuilderx-runner",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.1.1",
|
|
5
5
|
"description": "Stable HBuilderX CLI runner utilities for weapp-tailwindcss local e2e workflows.",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|