@weapp-tailwindcss/hbuilderx-runner 0.1.6 → 0.1.8
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.md +8 -0
- package/README.zh-CN.md +8 -0
- package/dist/index.cjs +201 -85
- package/dist/index.d.ts +7 -23
- package/dist/index.mjs +200 -83
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -54,3 +54,11 @@ If one CLI matches multiple hosts, the runner reports an ambiguity instead of gu
|
|
|
54
54
|
Some HBuilderX releases reject concurrent stable and Alpha processes. The runner never closes an existing instance automatically; it reports `cli-instance-mismatch` when the target edition cannot start, so close the conflicting instance before retrying.
|
|
55
55
|
|
|
56
56
|
`runPnpmCommand`, `spawnPnpmCommand`, `hbuilderxPnpmArgs`, and the synchronous `startLaunch` remain for compatibility. Use a bound runner when the stable/Alpha choice must be deterministic; the `@dcloudio/hbuilderx-cli` pnpm wrapper performs its own process discovery and cannot provide that guarantee.
|
|
57
|
+
|
|
58
|
+
## Timeouts and cleanup
|
|
59
|
+
|
|
60
|
+
After `timeoutMs`, `runCommand` cleans up its own process before waiting for the real `close` event and final logs. Cleanup has a separate budget: POSIX waits up to one second after the requested signal, then up to one second after `SIGKILL`; Windows runs `taskkill /T /F` with a five-second timeout, then waits up to one second for closure. Cleanup already in progress also waits for descendants in the default detached process group, including descendants that close their pipes before the root exits.
|
|
61
|
+
|
|
62
|
+
A graceful exit code of zero after the business deadline still has the `timeout` classification. `allowFailure` permits business failures, but never cleanup failures. `HBuilderXCommandError.cleanupError` and `cause` retain cleanup diagnostics alongside the command, cwd, observed exit and recent logs. A Windows partial tree-termination failure remains an error even if the root closes.
|
|
63
|
+
|
|
64
|
+
Call and await `stop()` before a long-running root process closes. Repeated calls share the same cleanup result. Natural closure revokes the numeric PID/group handle to avoid targeting a later process that reuses it; services that deliberately leave the group are outside this scope. With `detached: false`, only the owned root's closure is confirmed. The synchronous `killProcessTree` remains a best-effort compatibility request, not cleanup confirmation. Unreaped POSIX orphan processes or persistent permission errors that prevent confirmation cause conservative cleanup failure.
|
package/README.zh-CN.md
CHANGED
|
@@ -55,3 +55,11 @@ macOS 默认路径为 `/Applications/HBuilderX.app/Contents/MacOS/cli` 和 `/App
|
|
|
55
55
|
部分 HBuilderX 版本自身会拒绝 stable 与 Alpha 跨版本并行运行。runner 不会擅自关闭当前实例;目标版本无法启动时会抛出 `cli-instance-mismatch`,请关闭冲突实例后重试。
|
|
56
56
|
|
|
57
57
|
`runPnpmCommand`、`spawnPnpmCommand`、`hbuilderxPnpmArgs` 和同步 `startLaunch` 为兼容旧调用保留。需要固定 stable/Alpha 时应使用绑定 runner;`@dcloudio/hbuilderx-cli` 的 pnpm 包装器会重新按运行进程探测版本,无法提供同等保证。
|
|
58
|
+
|
|
59
|
+
## 超时与清理
|
|
60
|
+
|
|
61
|
+
`runCommand` 达到 `timeoutMs` 后先清理自己启动的进程,再等待真实 `close` 和最终日志。清理使用独立预算:POSIX 先发送请求信号,最多等待 1 秒,再发送 `SIGKILL` 并最多等待 1 秒;Windows 使用最多 5 秒的 `taskkill /T /F`,随后最多等待 1 秒确认关闭。默认独立进程组中的后代即使关闭管道、根进程先退出,也纳入已开始的清理。
|
|
62
|
+
|
|
63
|
+
业务超时后即使进程优雅地返回 0,结果仍是 `timeout`。`allowFailure` 只允许返回业务失败,不放行清理失败;清理失败通过 `HBuilderXCommandError.cleanupError` 和 `cause` 保留,主错误继续记录命令、cwd、退出状态和最近日志。Windows 部分终止失败不会因根进程关闭而被抹掉。
|
|
64
|
+
|
|
65
|
+
长驻命令必须在根进程 `close` 前调用并等待 `stop()`,重复调用复用同一次清理结果。自然关闭后注销数字 PID/进程组句柄,避免误终止后来复用该编号的进程;不会追踪主动脱离独立组的服务。`detached: false` 只确认所持有的根进程关闭。同步 `killProcessTree` 仅保留兼容的终止请求,不等同于清理成功。POSIX 进程组若仍含未回收的孤儿进程,或权限错误持续导致无法确认组已消失,清理会保守失败。
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
|
|
2
|
+
// \0rolldown/runtime.js
|
|
3
3
|
var __create = Object.create;
|
|
4
4
|
var __defProp = Object.defineProperty;
|
|
5
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -20,7 +20,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
20
20
|
value: mod,
|
|
21
21
|
enumerable: true
|
|
22
22
|
}) : target, mod));
|
|
23
|
-
//#endregion
|
|
24
23
|
let node_fs_promises = require("node:fs/promises");
|
|
25
24
|
node_fs_promises = __toESM(node_fs_promises, 1);
|
|
26
25
|
let node_path = require("node:path");
|
|
@@ -31,7 +30,7 @@ let node_buffer = require("node:buffer");
|
|
|
31
30
|
let node_child_process = require("node:child_process");
|
|
32
31
|
let cross_spawn = require("cross-spawn");
|
|
33
32
|
cross_spawn = __toESM(cross_spawn, 1);
|
|
34
|
-
|
|
33
|
+
// src/fs.ts
|
|
35
34
|
async function fileExists(file) {
|
|
36
35
|
try {
|
|
37
36
|
await node_fs_promises.default.access(file);
|
|
@@ -46,12 +45,11 @@ async function readUtf8(file) {
|
|
|
46
45
|
function wait(ms) {
|
|
47
46
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
48
47
|
}
|
|
49
|
-
|
|
50
|
-
//#region src/hbuilderx/configured-cli.ts
|
|
48
|
+
// src/hbuilderx/configured-cli.ts
|
|
51
49
|
const macOSStableCli = "/Applications/HBuilderX.app/Contents/MacOS/cli";
|
|
52
50
|
const macOSAlphaCli = "/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli";
|
|
53
51
|
function resolveCandidateSource(candidate, env) {
|
|
54
|
-
if (candidate === env
|
|
52
|
+
if (candidate === env["HBUILDERX_CLI_PATH"]) return "env";
|
|
55
53
|
if (candidate === "/Applications/HBuilderX.app/Contents/MacOS/cli" || candidate === "/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli") return "default-path";
|
|
56
54
|
return "candidate";
|
|
57
55
|
}
|
|
@@ -68,13 +66,12 @@ async function resolveConfiguredCli(options) {
|
|
|
68
66
|
source: resolveCandidateSource(candidate, env)
|
|
69
67
|
};
|
|
70
68
|
}
|
|
71
|
-
if (env
|
|
72
|
-
path: env
|
|
69
|
+
if (env["HBUILDERX_CLI_PATH"] && await fileExists(env["HBUILDERX_CLI_PATH"])) return {
|
|
70
|
+
path: env["HBUILDERX_CLI_PATH"],
|
|
73
71
|
source: "env"
|
|
74
72
|
};
|
|
75
73
|
}
|
|
76
|
-
|
|
77
|
-
//#region src/hbuilderx/discovery.ts
|
|
74
|
+
// src/hbuilderx/discovery.ts
|
|
78
75
|
function unique(items) {
|
|
79
76
|
return [...new Set(items)];
|
|
80
77
|
}
|
|
@@ -92,9 +89,9 @@ function inferHBuilderXChannel(file) {
|
|
|
92
89
|
if (/HBuilderX(?:\.app)?[\\/]/i.test(file) || /HBuilderX\.exe$/i.test(file)) return "stable";
|
|
93
90
|
return "unknown";
|
|
94
91
|
}
|
|
95
|
-
function getDefaultHBuilderXCliCandidates(env = node_process.default.env, channel = resolveHBuilderXChannel(env
|
|
92
|
+
function getDefaultHBuilderXCliCandidates(env = node_process.default.env, channel = resolveHBuilderXChannel(env["HBUILDERX_CHANNEL"]), platform = node_process.default.platform) {
|
|
96
93
|
const defaults = platform === "darwin" ? channel === "stable" ? [macOSStableCli] : channel === "alpha" ? [macOSAlphaCli] : [macOSStableCli, macOSAlphaCli] : [];
|
|
97
|
-
return unique([env
|
|
94
|
+
return unique([env["HBUILDERX_CLI_PATH"], ...defaults].filter((item) => Boolean(item)));
|
|
98
95
|
}
|
|
99
96
|
function extractHBuilderXMacOSExecutables(output) {
|
|
100
97
|
return unique(Array.from(output.matchAll(/(\/[^\r\n]*?HBuilderX(?:-Alpha|\s+Alpha)?\.app\/Contents\/MacOS\/HBuilderX)(?=\s|$)/gi), (match) => match[1]));
|
|
@@ -202,7 +199,7 @@ async function firstExisting(items) {
|
|
|
202
199
|
}
|
|
203
200
|
async function resolveHBuilderXCliInfoFromOptions(options = {}) {
|
|
204
201
|
const env = options.env ?? node_process.default.env;
|
|
205
|
-
const channel = resolveHBuilderXChannel(options.channel ?? env
|
|
202
|
+
const channel = resolveHBuilderXChannel(options.channel ?? env["HBUILDERX_CHANNEL"]);
|
|
206
203
|
const running = await findRunningHBuilderXCliCandidates();
|
|
207
204
|
const configured = await resolveConfiguredCli(options);
|
|
208
205
|
if (configured) return {
|
|
@@ -226,8 +223,7 @@ async function resolveHBuilderXCliInfoFromOptions(options = {}) {
|
|
|
226
223
|
};
|
|
227
224
|
throw new Error([`未找到 HBuilderX ${channel === "auto" ? "" : `${channel} `}CLI。`, "请安装 HBuilderX,或设置 HBUILDERX_CLI_PATH=/path/to/cli。"].join(""));
|
|
228
225
|
}
|
|
229
|
-
|
|
230
|
-
//#region src/logs.ts
|
|
226
|
+
// src/logs.ts
|
|
231
227
|
const PROJECT_NOT_UNI_APP_RE = /不是\s*uni-app\s*项目|not\s+a\s+uni-app\s+project/i;
|
|
232
228
|
const PROJECT_TYPE_UNSUPPORTED_RE = /项目类型为([^,]+),暂不支持|是\s+([^,\s][^,]*)项目,暂不支持|project\s+type.+unsupported/i;
|
|
233
229
|
const PROJECT_TARGET_UNSUPPORTED_RE = /不支持运行到\s*(鸿蒙|Harmony)|(?:Harmony|OpenHarmony).+(?:target|platform).+unsupported/i;
|
|
@@ -356,38 +352,140 @@ function formatRecentLogs(logs, maxLength = 6e3) {
|
|
|
356
352
|
const important = (importantLogsByBuffer.get(logs) ?? []).filter((line) => !recent.includes(line));
|
|
357
353
|
return important.length > 0 ? `关键错误:\n${important.join("\n")}\n\n最近日志:\n${recent}` : recent;
|
|
358
354
|
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
355
|
+
// src/process/termination.ts
|
|
356
|
+
const ownedGroups = /* @__PURE__ */ new WeakMap();
|
|
357
|
+
const closedChildren = /* @__PURE__ */ new WeakSet();
|
|
358
|
+
const shutdownWaitMs = 1e3;
|
|
359
|
+
/** 只记录本次 spawn 创建的独立进程组,不从外部 PID 猜测组归属。 */
|
|
360
|
+
function registerProcessGroup(child, detached) {
|
|
361
|
+
if (node_process.default.platform !== "win32" && detached && child.pid) ownedGroups.set(child, child.pid);
|
|
362
|
+
child.once("close", () => {
|
|
363
|
+
closedChildren.add(child);
|
|
364
|
+
ownedGroups.delete(child);
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
function signalOwnedTree(child, signal, group) {
|
|
368
|
+
if (!child.pid) return;
|
|
372
369
|
if (node_process.default.platform === "win32") {
|
|
373
|
-
(
|
|
370
|
+
if (child.exitCode !== null || child.signalCode !== null || closedChildren.has(child)) return;
|
|
371
|
+
const result = (0, node_child_process.spawnSync)("taskkill", [
|
|
374
372
|
"/pid",
|
|
375
|
-
String(pid),
|
|
373
|
+
String(child.pid),
|
|
376
374
|
"/t",
|
|
377
375
|
"/f"
|
|
378
376
|
], {
|
|
379
|
-
|
|
377
|
+
timeout: 5e3,
|
|
378
|
+
killSignal: "SIGKILL",
|
|
379
|
+
maxBuffer: 65536,
|
|
380
|
+
encoding: "utf8",
|
|
381
|
+
stdio: [
|
|
382
|
+
"ignore",
|
|
383
|
+
"pipe",
|
|
384
|
+
"pipe"
|
|
385
|
+
],
|
|
380
386
|
windowsHide: true
|
|
381
387
|
});
|
|
388
|
+
if (result.error || result.status !== 0) throw new Error(`taskkill pid=${child.pid} 失败,exit=${result.status} signal=${result.signal}\n${result.error?.message ?? ""}\n${result.stderr ?? ""}\n${result.stdout ?? ""}`, { cause: result.error });
|
|
382
389
|
return;
|
|
383
390
|
}
|
|
384
|
-
try {
|
|
385
|
-
node_process.default.kill(-
|
|
386
|
-
|
|
391
|
+
if (group) try {
|
|
392
|
+
node_process.default.kill(-group, signal);
|
|
393
|
+
return;
|
|
394
|
+
} catch (error) {
|
|
395
|
+
if (error.code !== "ESRCH") throw error;
|
|
396
|
+
}
|
|
397
|
+
if (!closedChildren.has(child) && child.exitCode === null && child.signalCode === null && !child.kill(signal)) throw new Error(`未能向子进程 pid=${child.pid} 发送 ${signal}`);
|
|
398
|
+
}
|
|
399
|
+
/** 同步入口在 close 后失效,避免稍后向复用的 PID 或进程组发送信号。 */
|
|
400
|
+
function signalProcessTree(child, signal) {
|
|
401
|
+
if (!closedChildren.has(child)) signalOwnedTree(child, signal, ownedGroups.get(child));
|
|
402
|
+
}
|
|
403
|
+
function waitForCleanup(closed, isGroupGone) {
|
|
404
|
+
return new Promise((resolve, reject) => {
|
|
405
|
+
let didClose = false;
|
|
406
|
+
let finished = false;
|
|
407
|
+
let lastProbeError;
|
|
408
|
+
const finish = (done, error) => {
|
|
409
|
+
finished = true;
|
|
410
|
+
clearTimeout(deadline);
|
|
411
|
+
clearInterval(poll);
|
|
412
|
+
if (error) reject(error);
|
|
413
|
+
else resolve(done);
|
|
414
|
+
};
|
|
415
|
+
const check = () => {
|
|
416
|
+
if (finished) return;
|
|
417
|
+
try {
|
|
418
|
+
const groupGone = isGroupGone();
|
|
419
|
+
lastProbeError = void 0;
|
|
420
|
+
if (didClose && groupGone) finish(true);
|
|
421
|
+
} catch (error) {
|
|
422
|
+
lastProbeError = error;
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
const expire = () => finish(false, lastProbeError);
|
|
426
|
+
const deadline = setTimeout(expire, shutdownWaitMs);
|
|
427
|
+
const poll = setInterval(check, 25);
|
|
428
|
+
closed.then(() => {
|
|
429
|
+
didClose = true;
|
|
430
|
+
check();
|
|
431
|
+
});
|
|
432
|
+
check();
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
/** 清理预算独立于业务超时;只持有本轮开始时仍有效的组归属。 */
|
|
436
|
+
async function stopProcessTree(child, closed, signal) {
|
|
437
|
+
let group = ownedGroups.get(child);
|
|
438
|
+
const errors = [];
|
|
439
|
+
const exitRaces = [];
|
|
440
|
+
const isGroupGone = () => {
|
|
441
|
+
if (!group) return true;
|
|
442
|
+
try {
|
|
443
|
+
node_process.default.kill(-group, 0);
|
|
444
|
+
return false;
|
|
445
|
+
} catch (error) {
|
|
446
|
+
if (error.code !== "ESRCH") throw error;
|
|
447
|
+
group = void 0;
|
|
448
|
+
ownedGroups.delete(child);
|
|
449
|
+
return true;
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
const signals = node_process.default.platform === "win32" || signal === "SIGKILL" ? [signal] : [signal, "SIGKILL"];
|
|
453
|
+
for (const nextSignal of signals) {
|
|
387
454
|
try {
|
|
388
|
-
child
|
|
389
|
-
} catch {
|
|
455
|
+
signalOwnedTree(child, nextSignal, group);
|
|
456
|
+
} catch (error) {
|
|
457
|
+
if (node_process.default.platform === "darwin" && group && (child.exitCode !== null || child.signalCode !== null) && error.code === "EPERM") exitRaces.push(error);
|
|
458
|
+
else errors.push(error);
|
|
459
|
+
}
|
|
460
|
+
try {
|
|
461
|
+
if (await waitForCleanup(closed, isGroupGone)) {
|
|
462
|
+
if (!errors.length) return;
|
|
463
|
+
break;
|
|
464
|
+
}
|
|
465
|
+
} catch (error) {
|
|
466
|
+
errors.push(error);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
const failures = [...errors, ...exitRaces];
|
|
470
|
+
const details = failures.map((error) => error instanceof Error ? error.message : String(error)).join("\n");
|
|
471
|
+
throw new AggregateError(failures, `子进程 pid=${child.pid ?? "unknown"} 的有界清理未成功:需确认 close 和本轮进程组结束\n${details}`.trim());
|
|
472
|
+
}
|
|
473
|
+
// src/process.ts
|
|
474
|
+
var HBuilderXCommandError = class extends Error {
|
|
475
|
+
result;
|
|
476
|
+
cleanupError;
|
|
477
|
+
constructor(message, result, cleanupError) {
|
|
478
|
+
super(message, cleanupError ? { cause: cleanupError } : void 0);
|
|
479
|
+
this.name = "HBuilderXCommandError";
|
|
480
|
+
this.result = result;
|
|
481
|
+
if (cleanupError) this.cleanupError = cleanupError;
|
|
390
482
|
}
|
|
483
|
+
};
|
|
484
|
+
/** 保留同步请求终止的兼容入口;需要确认关闭时必须等待 spawned.stop()。 */
|
|
485
|
+
function killProcessTree(child, signal = "SIGTERM") {
|
|
486
|
+
try {
|
|
487
|
+
signalProcessTree(child, signal);
|
|
488
|
+
} catch {}
|
|
391
489
|
}
|
|
392
490
|
function createCommandResult(options, exit, logs, timedOut = false) {
|
|
393
491
|
const output = formatRecentLogs(logs);
|
|
@@ -401,21 +499,27 @@ function createCommandResult(options, exit, logs, timedOut = false) {
|
|
|
401
499
|
issue: timedOut ? createTimeoutIssue() : classifyHBuilderXOutput(output)
|
|
402
500
|
};
|
|
403
501
|
}
|
|
404
|
-
function buildCommandError(prefix, options, exit, logs, timedOut = false) {
|
|
502
|
+
function buildCommandError(prefix, options, exit, logs, timedOut = false, cleanupError) {
|
|
405
503
|
const result = createCommandResult(options, exit, logs, timedOut);
|
|
504
|
+
if (cleanupError && !timedOut) result.issue = {
|
|
505
|
+
kind: "process-exit",
|
|
506
|
+
message: "子进程清理失败,未确认关闭或进程树终止。"
|
|
507
|
+
};
|
|
406
508
|
return new HBuilderXCommandError([
|
|
407
509
|
`${prefix}: ${options.command} ${options.args.join(" ")}`,
|
|
408
510
|
`cwd=${options.cwd}`,
|
|
409
511
|
`exit=${exit.signal ?? exit.code}`,
|
|
410
512
|
`issue=${result.issue.kind}: ${result.issue.message}`,
|
|
411
513
|
result.issue.hint ? `hint=${result.issue.hint}` : "",
|
|
412
|
-
result.output
|
|
413
|
-
|
|
514
|
+
result.output,
|
|
515
|
+
cleanupError ? `cleanup=${cleanupError.message}` : ""
|
|
516
|
+
].filter(Boolean).join("\n"), result, cleanupError);
|
|
414
517
|
}
|
|
415
518
|
function spawnCommand(options) {
|
|
519
|
+
const detached = options.detached ?? node_process.default.platform !== "win32";
|
|
416
520
|
const child = (0, cross_spawn.default)(options.command, options.args, {
|
|
417
521
|
cwd: options.cwd,
|
|
418
|
-
detached
|
|
522
|
+
detached,
|
|
419
523
|
stdio: options.stdio === "inherit" ? "inherit" : [
|
|
420
524
|
"ignore",
|
|
421
525
|
"pipe",
|
|
@@ -424,14 +528,16 @@ function spawnCommand(options) {
|
|
|
424
528
|
env: {
|
|
425
529
|
...node_process.default.env,
|
|
426
530
|
...options.env,
|
|
427
|
-
NODE_OPTIONS: options.env?.NODE_OPTIONS ?? node_process.default.env
|
|
531
|
+
NODE_OPTIONS: options.env?.["NODE_OPTIONS"] ?? node_process.default.env["NODE_OPTIONS"] ?? "--max-old-space-size=8192"
|
|
428
532
|
}
|
|
429
533
|
});
|
|
534
|
+
registerProcessGroup(child, detached);
|
|
430
535
|
const logs = options.stdio === "inherit" ? [] : collectProcessOutput(child);
|
|
431
536
|
child.on("error", (error) => logs.push(`${error.message}\n`));
|
|
432
537
|
let exit;
|
|
538
|
+
let stopping;
|
|
433
539
|
const closed = new Promise((resolve) => {
|
|
434
|
-
child.
|
|
540
|
+
child.once("close", (code, signal) => {
|
|
435
541
|
exit = {
|
|
436
542
|
code,
|
|
437
543
|
signal
|
|
@@ -449,9 +555,16 @@ function spawnCommand(options) {
|
|
|
449
555
|
ensureRunning() {
|
|
450
556
|
if (exit && exit.code !== 0) throw buildCommandError("命令提前退出", options, exit, logs);
|
|
451
557
|
},
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
558
|
+
stop(signal = "SIGTERM") {
|
|
559
|
+
if (stopping) return stopping;
|
|
560
|
+
if (exit) return Promise.resolve();
|
|
561
|
+
stopping = stopProcessTree(child, closed, signal).catch((error) => {
|
|
562
|
+
throw buildCommandError("命令清理失败", options, {
|
|
563
|
+
code: child.exitCode,
|
|
564
|
+
signal: child.signalCode
|
|
565
|
+
}, logs, false, error);
|
|
566
|
+
});
|
|
567
|
+
return stopping;
|
|
455
568
|
}
|
|
456
569
|
};
|
|
457
570
|
}
|
|
@@ -460,19 +573,26 @@ async function runCommand(options) {
|
|
|
460
573
|
let timer;
|
|
461
574
|
let timedOut = false;
|
|
462
575
|
try {
|
|
463
|
-
const
|
|
576
|
+
const deadline = Symbol("deadline");
|
|
577
|
+
const outcome = await (options.timeoutMs == null ? spawned.closed : Promise.race([spawned.closed, new Promise((resolve) => {
|
|
464
578
|
timer = setTimeout(() => {
|
|
465
579
|
timedOut = true;
|
|
466
|
-
|
|
467
|
-
resolve({
|
|
468
|
-
code: null,
|
|
469
|
-
signal: "SIGTERM"
|
|
470
|
-
});
|
|
580
|
+
resolve(deadline);
|
|
471
581
|
}, options.timeoutMs);
|
|
472
582
|
})]));
|
|
583
|
+
if (outcome === deadline) try {
|
|
584
|
+
await spawned.stop();
|
|
585
|
+
} catch (error) {
|
|
586
|
+
const cleanupError = error instanceof HBuilderXCommandError ? error.cleanupError ?? error : error;
|
|
587
|
+
throw buildCommandError("命令超时且清理失败", options, {
|
|
588
|
+
code: spawned.child.exitCode,
|
|
589
|
+
signal: spawned.child.signalCode
|
|
590
|
+
}, spawned.logs, true, cleanupError);
|
|
591
|
+
}
|
|
592
|
+
const exit = outcome === deadline ? await spawned.closed : outcome;
|
|
473
593
|
const result = createCommandResult(options, exit, spawned.logs, timedOut);
|
|
474
|
-
if (exit.code === 0 || options.allowFailure) return result;
|
|
475
|
-
throw buildCommandError(
|
|
594
|
+
if (!timedOut && exit.code === 0 || options.allowFailure) return result;
|
|
595
|
+
throw buildCommandError(timedOut ? "命令超时" : "命令失败", options, exit, spawned.logs, timedOut);
|
|
476
596
|
} finally {
|
|
477
597
|
if (timer) clearTimeout(timer);
|
|
478
598
|
}
|
|
@@ -501,8 +621,7 @@ function createProcessExitTracker(child) {
|
|
|
501
621
|
}
|
|
502
622
|
};
|
|
503
623
|
}
|
|
504
|
-
|
|
505
|
-
//#region src/hbuilderx/hosts.ts
|
|
624
|
+
// src/hbuilderx/hosts.ts
|
|
506
625
|
const ansiRE = new RegExp(`${String.fromCharCode(27)}\\[[\\d;]*m`, "g");
|
|
507
626
|
function stripAnsi(value) {
|
|
508
627
|
return value.replace(ansiRE, "");
|
|
@@ -578,7 +697,7 @@ async function connectHBuilderXHost(options) {
|
|
|
578
697
|
};
|
|
579
698
|
const assertCommand = (result) => {
|
|
580
699
|
if (result.issue.kind === "cli-instance-mismatch" || result.issue.kind === "cli-host-ambiguous") throw createRunnerError(cliPath, cwd, result.args, result.issue.kind, result.issue.message, result.issue.hint ?? "请检查目标 HBuilderX host。", logs);
|
|
581
|
-
if (result.exit.code !== 0) throw new HBuilderXCommandError(`HBuilderX host 命令失败:${cliPath} ${result.args.join(" ")}\ncwd=${cwd}\nexit=${result.exit.signal ?? result.exit.code}\nissue=${result.issue.kind}\n${formatRecentLogs(logs)}`, result);
|
|
700
|
+
if (result.issue.kind === "timeout" || result.exit.code !== 0) throw new HBuilderXCommandError(`HBuilderX host 命令失败:${cliPath} ${result.args.join(" ")}\ncwd=${cwd}\nexit=${result.exit.signal ?? result.exit.code}\nissue=${result.issue.kind}\n${formatRecentLogs(logs)}`, result);
|
|
582
701
|
};
|
|
583
702
|
const probe = async () => {
|
|
584
703
|
const listed = await command(["listhost"]);
|
|
@@ -615,8 +734,7 @@ async function connectHBuilderXHost(options) {
|
|
|
615
734
|
} while (Date.now() < deadline);
|
|
616
735
|
throw timeoutError(["listhost"]);
|
|
617
736
|
}
|
|
618
|
-
|
|
619
|
-
//#region src/hbuilderx/runner.ts
|
|
737
|
+
// src/hbuilderx/runner.ts
|
|
620
738
|
const defaultStartupTimeoutMs = 6e4;
|
|
621
739
|
function createHBuilderXNativeLaunchArgs(options) {
|
|
622
740
|
const args = [
|
|
@@ -647,11 +765,12 @@ async function createHBuilderXRunner(options = {}) {
|
|
|
647
765
|
};
|
|
648
766
|
const cwd = options.cwd ?? node_process.default.cwd();
|
|
649
767
|
const timeoutMs = options.timeoutMs ?? defaultStartupTimeoutMs;
|
|
768
|
+
const candidates = options.hbuilderxCliPath ? [options.hbuilderxCliPath] : options.candidates;
|
|
650
769
|
const resolveOptions = {
|
|
651
|
-
candidates
|
|
652
|
-
channel: options.channel,
|
|
770
|
+
...candidates !== void 0 ? { candidates } : {},
|
|
771
|
+
...options.channel !== void 0 ? { channel: options.channel } : {},
|
|
653
772
|
env,
|
|
654
|
-
host: options.host
|
|
773
|
+
...options.host !== void 0 ? { host: options.host } : {}
|
|
655
774
|
};
|
|
656
775
|
const configured = await resolveConfiguredCli(resolveOptions);
|
|
657
776
|
const resolution = configured ? {
|
|
@@ -662,15 +781,15 @@ async function createHBuilderXRunner(options = {}) {
|
|
|
662
781
|
...env,
|
|
663
782
|
HBUILDERX_CLI_PATH: resolution.path
|
|
664
783
|
};
|
|
665
|
-
const requestedChannel = resolveHBuilderXChannel(options.channel ?? env
|
|
784
|
+
const requestedChannel = resolveHBuilderXChannel(options.channel ?? env["HBUILDERX_CHANNEL"]);
|
|
666
785
|
const expectedChannel = resolution.channel !== "unknown" ? resolution.channel : requestedChannel === "auto" ? inferHBuilderXChannel(resolution.path) : requestedChannel;
|
|
667
|
-
const explicitHost = options.host ?? env
|
|
786
|
+
const explicitHost = options.host ?? env["HBUILDERX_HOST"];
|
|
668
787
|
const selected = await connectHBuilderXHost({
|
|
669
788
|
cliPath: resolution.path,
|
|
670
789
|
cwd,
|
|
671
790
|
env: cliEnv,
|
|
672
791
|
expectedChannel,
|
|
673
|
-
explicitHost,
|
|
792
|
+
...explicitHost !== void 0 ? { explicitHost } : {},
|
|
674
793
|
timeoutMs
|
|
675
794
|
});
|
|
676
795
|
const boundResolution = {
|
|
@@ -749,8 +868,7 @@ async function createHBuilderXRunner(options = {}) {
|
|
|
749
868
|
};
|
|
750
869
|
return runner;
|
|
751
870
|
}
|
|
752
|
-
|
|
753
|
-
//#region src/hbuilderx.ts
|
|
871
|
+
// src/hbuilderx.ts
|
|
754
872
|
async function resolveHBuilderXCliInfo(candidatesOrOptions, env = node_process.default.env) {
|
|
755
873
|
return await resolveHBuilderXCliInfoFromOptions(Array.isArray(candidatesOrOptions) ? {
|
|
756
874
|
candidates: candidatesOrOptions,
|
|
@@ -759,14 +877,14 @@ async function resolveHBuilderXCliInfo(candidatesOrOptions, env = node_process.d
|
|
|
759
877
|
}
|
|
760
878
|
async function resolveHBuilderXCli(candidatesOrOptions) {
|
|
761
879
|
const options = Array.isArray(candidatesOrOptions) ? { candidates: candidatesOrOptions } : candidatesOrOptions ?? {};
|
|
762
|
-
resolveHBuilderXChannel(options.channel ?? (options.env ?? node_process.default.env)
|
|
880
|
+
resolveHBuilderXChannel(options.channel ?? (options.env ?? node_process.default.env)["HBUILDERX_CHANNEL"]);
|
|
763
881
|
return (await resolveConfiguredCli(options))?.path ?? (await resolveHBuilderXCliInfoFromOptions(options)).path;
|
|
764
882
|
}
|
|
765
883
|
function createHBuilderXEnv(options = {}) {
|
|
766
884
|
return {
|
|
767
885
|
...options.env,
|
|
768
|
-
HBUILDERX_CLI_PATH: options.hbuilderxCliPath ?? options.env?.HBUILDERX_CLI_PATH ?? node_process.default.env
|
|
769
|
-
NODE_OPTIONS: options.env?.NODE_OPTIONS ?? node_process.default.env
|
|
886
|
+
HBUILDERX_CLI_PATH: options.hbuilderxCliPath ?? options.env?.["HBUILDERX_CLI_PATH"] ?? node_process.default.env["HBUILDERX_CLI_PATH"] ?? getDefaultHBuilderXCliCandidates()[0],
|
|
887
|
+
NODE_OPTIONS: options.env?.["NODE_OPTIONS"] ?? node_process.default.env["NODE_OPTIONS"] ?? "--max-old-space-size=8192"
|
|
770
888
|
};
|
|
771
889
|
}
|
|
772
890
|
function hbuilderxPnpmArgs(args) {
|
|
@@ -781,8 +899,8 @@ async function runPnpmCommand(options) {
|
|
|
781
899
|
command: "pnpm",
|
|
782
900
|
args: options.args,
|
|
783
901
|
cwd: options.cwd,
|
|
784
|
-
timeoutMs: options.timeoutMs,
|
|
785
|
-
allowFailure: options.allowFailure,
|
|
902
|
+
...options.timeoutMs !== void 0 ? { timeoutMs: options.timeoutMs } : {},
|
|
903
|
+
...options.allowFailure !== void 0 ? { allowFailure: options.allowFailure } : {},
|
|
786
904
|
env: createHBuilderXEnv(options)
|
|
787
905
|
});
|
|
788
906
|
}
|
|
@@ -827,8 +945,7 @@ function startLaunch(options) {
|
|
|
827
945
|
args: createLaunchArgs(options)
|
|
828
946
|
});
|
|
829
947
|
}
|
|
830
|
-
|
|
831
|
-
//#region src/toolchains.ts
|
|
948
|
+
// src/toolchains.ts
|
|
832
949
|
function runTool(command, args) {
|
|
833
950
|
const result = (0, node_child_process.spawnSync)(command, args, { encoding: "utf8" });
|
|
834
951
|
return {
|
|
@@ -846,9 +963,9 @@ function resolveAdbCommand(env = node_process.default.env) {
|
|
|
846
963
|
env: {}
|
|
847
964
|
};
|
|
848
965
|
const candidates = [
|
|
849
|
-
env
|
|
850
|
-
env
|
|
851
|
-
env
|
|
966
|
+
env["ANDROID_HOME"],
|
|
967
|
+
env["ANDROID_SDK_ROOT"],
|
|
968
|
+
env["HOME"] ? node_path.default.join(env["HOME"], "Library/Android/sdk") : void 0,
|
|
852
969
|
"/opt/android-sdk",
|
|
853
970
|
"/usr/local/share/android-sdk"
|
|
854
971
|
].filter((item) => Boolean(item));
|
|
@@ -859,9 +976,9 @@ function resolveAdbCommand(env = node_process.default.env) {
|
|
|
859
976
|
command: adbPath,
|
|
860
977
|
output: candidate.output,
|
|
861
978
|
env: {
|
|
862
|
-
ANDROID_HOME: env
|
|
863
|
-
ANDROID_SDK_ROOT: env
|
|
864
|
-
PATH: `${node_path.default.dirname(adbPath)}${node_path.default.delimiter}${env
|
|
979
|
+
ANDROID_HOME: env["ANDROID_HOME"] ?? sdkRoot,
|
|
980
|
+
ANDROID_SDK_ROOT: env["ANDROID_SDK_ROOT"] ?? sdkRoot,
|
|
981
|
+
PATH: `${node_path.default.dirname(adbPath)}${node_path.default.delimiter}${env["PATH"] ?? ""}`
|
|
865
982
|
}
|
|
866
983
|
};
|
|
867
984
|
}
|
|
@@ -890,7 +1007,7 @@ function assertIosSimulatorToolchain(env = node_process.default.env) {
|
|
|
890
1007
|
if (!simctl.ok || !xcodebuild.ok || firstLaunchStatus?.ok === false) throw new Error([
|
|
891
1008
|
"当前机器缺少 iOS 模拟器所需的完整 Xcode 工具链,无法运行 HBuilderX app-ios E2E。",
|
|
892
1009
|
`xcode-select: ${xcodeSelect.output || "unknown"}`,
|
|
893
|
-
`DEVELOPER_DIR: ${env
|
|
1010
|
+
`DEVELOPER_DIR: ${env["DEVELOPER_DIR"] || "未设置"}`,
|
|
894
1011
|
`simctl: ${simctl.output || "not found"}`,
|
|
895
1012
|
`xcodebuild: ${xcodebuild.output || "not found"}`,
|
|
896
1013
|
`firstLaunchStatus: ${firstLaunchStatus?.output || (firstLaunchStatus?.ok === false ? "not ready" : "unknown")}`,
|
|
@@ -936,7 +1053,7 @@ function selectPreferredIosSimulatorDevice(devices) {
|
|
|
936
1053
|
})[0];
|
|
937
1054
|
}
|
|
938
1055
|
function resolveIosSimulatorDeviceId(env = node_process.default.env) {
|
|
939
|
-
const explicitDeviceId = env
|
|
1056
|
+
const explicitDeviceId = env["E2E_HBUILDERX_IOS_DEVICE_ID"];
|
|
940
1057
|
if (explicitDeviceId) return explicitDeviceId;
|
|
941
1058
|
const booted = runTool("xcrun", [
|
|
942
1059
|
"simctl",
|
|
@@ -965,9 +1082,9 @@ function resolveIosSimulatorDeviceId(env = node_process.default.env) {
|
|
|
965
1082
|
}
|
|
966
1083
|
function resolveHdcCommand(env = node_process.default.env) {
|
|
967
1084
|
const candidates = [
|
|
968
|
-
env
|
|
969
|
-
env
|
|
970
|
-
env
|
|
1085
|
+
env["DEMO_VISUAL_HARMONY_HDC_PATH"],
|
|
1086
|
+
env["E2E_HBUILDERX_HARMONY_HDC_PATH"],
|
|
1087
|
+
env["HDC_PATH"],
|
|
971
1088
|
"hdc",
|
|
972
1089
|
node_process.default.platform === "darwin" ? "/Applications/DevEco-Studio.app/Contents/sdk/default/openharmony/toolchains/hdc" : void 0
|
|
973
1090
|
].filter((item) => Boolean(item));
|
|
@@ -998,7 +1115,6 @@ function assertHarmonyToolchain(env = node_process.default.env, deviceId) {
|
|
|
998
1115
|
}
|
|
999
1116
|
return hdc;
|
|
1000
1117
|
}
|
|
1001
|
-
//#endregion
|
|
1002
1118
|
exports.HBuilderXCommandError = HBuilderXCommandError;
|
|
1003
1119
|
exports.assertAndroidToolchain = assertAndroidToolchain;
|
|
1004
1120
|
exports.assertHarmonyToolchain = assertHarmonyToolchain;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import { Buffer } from "node:buffer";
|
|
2
1
|
import { ChildProcess } from "node:child_process";
|
|
3
|
-
|
|
2
|
+
import { Buffer } from "node:buffer";
|
|
4
3
|
export declare function fileExists(file: string): Promise<boolean>;
|
|
5
4
|
export declare function readUtf8(file: string): Promise<string>;
|
|
6
5
|
export declare function wait(ms: number): Promise<unknown>;
|
|
7
|
-
//#endregion
|
|
8
|
-
//#region src/types.d.ts
|
|
9
6
|
export type HBuilderXMiniProgramPlatform = 'mp-weixin' | 'mp-alipay' | 'mp-baidu' | 'mp-toutiao';
|
|
10
7
|
export type HBuilderXAppPlatform = 'app-android' | 'app-ios' | 'app-harmony';
|
|
11
8
|
export type HBuilderXPlatform = HBuilderXMiniProgramPlatform | HBuilderXAppPlatform;
|
|
@@ -25,6 +22,7 @@ export interface HBuilderXCommandOptions {
|
|
|
25
22
|
command: string;
|
|
26
23
|
args: string[];
|
|
27
24
|
cwd: string;
|
|
25
|
+
/** 业务截止时间;超时后另有有界进程清理预算。 */
|
|
28
26
|
timeoutMs?: number;
|
|
29
27
|
env?: Record<string, string | undefined>;
|
|
30
28
|
allowFailure?: boolean;
|
|
@@ -46,6 +44,7 @@ export interface SpawnedHBuilderXCommand {
|
|
|
46
44
|
command: string;
|
|
47
45
|
args: string[];
|
|
48
46
|
cwd: string;
|
|
47
|
+
/** 在 close 前启动有界清理并确认本轮进程树结束;失败抛出诊断,多次调用复用同一结果。 */
|
|
49
48
|
stop: (signal?: NodeJS.Signals) => Promise<void>;
|
|
50
49
|
closed: Promise<CommandExit>;
|
|
51
50
|
ensureRunning: () => void;
|
|
@@ -115,12 +114,8 @@ export interface HBuilderXRunner {
|
|
|
115
114
|
launchProject: (options: HBuilderXLaunchOptions) => Promise<HBuilderXCommandResult>;
|
|
116
115
|
startLaunch: (options: HBuilderXLaunchOptions) => SpawnedHBuilderXCommand;
|
|
117
116
|
}
|
|
118
|
-
//#endregion
|
|
119
|
-
//#region src/hbuilderx/configured-cli.d.ts
|
|
120
117
|
export declare const macOSStableCli = "/Applications/HBuilderX.app/Contents/MacOS/cli";
|
|
121
118
|
export declare const macOSAlphaCli = "/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli";
|
|
122
|
-
//#endregion
|
|
123
|
-
//#region src/hbuilderx/discovery.d.ts
|
|
124
119
|
export declare function resolveHBuilderXChannel(channel?: string): HBuilderXChannel;
|
|
125
120
|
export declare function inferHBuilderXChannel(file: string): HBuilderXResolvedChannel;
|
|
126
121
|
export declare function getDefaultHBuilderXCliCandidates(env?: NodeJS.ProcessEnv, channel?: HBuilderXChannel, platform?: NodeJS.Platform): string[];
|
|
@@ -134,12 +129,12 @@ export declare function findRunningHBuilderXCliCandidates(platform?: NodeJS.Plat
|
|
|
134
129
|
export declare function findRunningHBuilderXCli(platform?: NodeJS.Platform): Promise<string | undefined>;
|
|
135
130
|
export declare function selectHBuilderXCliCandidatesForChannel(items: string[], channel: HBuilderXChannel): string[];
|
|
136
131
|
export declare function resolveHBuilderXCliInfoFromOptions(options?: HBuilderXCliResolveOptions): Promise<HBuilderXCliResolution>;
|
|
137
|
-
//#endregion
|
|
138
|
-
//#region src/process.d.ts
|
|
139
132
|
export declare class HBuilderXCommandError extends Error {
|
|
140
133
|
readonly result: HBuilderXCommandResult;
|
|
141
|
-
|
|
134
|
+
readonly cleanupError?: Error;
|
|
135
|
+
constructor(message: string, result: HBuilderXCommandResult, cleanupError?: Error);
|
|
142
136
|
}
|
|
137
|
+
/** 保留同步请求终止的兼容入口;需要确认关闭时必须等待 spawned.stop()。 */
|
|
143
138
|
export declare function killProcessTree(child: ChildProcess, signal?: NodeJS.Signals): void;
|
|
144
139
|
export declare function spawnCommand(options: HBuilderXCommandOptions): SpawnedHBuilderXCommand;
|
|
145
140
|
export declare function runCommand(options: HBuilderXCommandOptions): Promise<HBuilderXCommandResult>;
|
|
@@ -147,18 +142,12 @@ export declare function createProcessExitTracker(child: ChildProcess): {
|
|
|
147
142
|
closed: Promise<CommandExit>;
|
|
148
143
|
ensureRunning(logs: string[]): void;
|
|
149
144
|
};
|
|
150
|
-
//#endregion
|
|
151
|
-
//#region src/hbuilderx/hosts.d.ts
|
|
152
145
|
export declare function stripAnsi(value: string): string;
|
|
153
146
|
export declare function parseHBuilderXHosts(output: string): string[];
|
|
154
147
|
export declare function parseHBuilderXVersion(output: string): string | undefined;
|
|
155
148
|
export declare function inferHBuilderXChannelFromVersion(version: string): HBuilderXResolvedChannel;
|
|
156
|
-
//#endregion
|
|
157
|
-
//#region src/hbuilderx/runner.d.ts
|
|
158
149
|
export declare function createHBuilderXNativeLaunchArgs(options: HBuilderXLaunchOptions): string[];
|
|
159
150
|
export declare function createHBuilderXRunner(options?: HBuilderXRunnerOptions): Promise<HBuilderXRunner>;
|
|
160
|
-
//#endregion
|
|
161
|
-
//#region src/hbuilderx.d.ts
|
|
162
151
|
export declare function resolveHBuilderXCliInfo(options?: HBuilderXCliResolveOptions): ReturnType<typeof resolveHBuilderXCliInfoFromOptions>;
|
|
163
152
|
export declare function resolveHBuilderXCliInfo(candidates?: string[], env?: NodeJS.ProcessEnv): ReturnType<typeof resolveHBuilderXCliInfoFromOptions>;
|
|
164
153
|
export declare function resolveHBuilderXCli(options?: HBuilderXCliResolveOptions): Promise<string>;
|
|
@@ -186,8 +175,6 @@ export declare function launchProject(options: HBuilderXLaunchOptions): Promise<
|
|
|
186
175
|
* @deprecated 需要固定 stable/alpha 与 host 时,请使用 `createHBuilderXRunner().startLaunch()`。
|
|
187
176
|
*/
|
|
188
177
|
export declare function startLaunch(options: HBuilderXLaunchOptions): SpawnedHBuilderXCommand;
|
|
189
|
-
//#endregion
|
|
190
|
-
//#region src/logs.d.ts
|
|
191
178
|
export declare function createLogBuffer(maxChunks?: number): {
|
|
192
179
|
logs: string[];
|
|
193
180
|
push(chunk: Buffer | string): void;
|
|
@@ -197,8 +184,6 @@ export declare function collectProcessOutput(child: ChildProcess, maxChunks?: nu
|
|
|
197
184
|
export declare function classifyHBuilderXOutput(output: string): HBuilderXIssue;
|
|
198
185
|
export declare function createTimeoutIssue(): HBuilderXIssue;
|
|
199
186
|
export declare function formatRecentLogs(logs: string[], maxLength?: number): string;
|
|
200
|
-
//#endregion
|
|
201
|
-
//#region src/toolchains.d.ts
|
|
202
187
|
export declare function runTool(command: string, args: string[]): ToolResult;
|
|
203
188
|
export declare function resolveAdbCommand(env?: NodeJS.ProcessEnv): AndroidToolchain;
|
|
204
189
|
export declare function assertAndroidToolchain(env?: NodeJS.ProcessEnv): Record<string, string> | undefined;
|
|
@@ -214,5 +199,4 @@ export declare function selectPreferredIosSimulatorDevice(devices: IosSimulatorD
|
|
|
214
199
|
export declare function resolveIosSimulatorDeviceId(env?: NodeJS.ProcessEnv): string;
|
|
215
200
|
export declare function resolveHdcCommand(env?: NodeJS.ProcessEnv): string;
|
|
216
201
|
export declare function parseHdcTargets(output: string): string[];
|
|
217
|
-
export declare function assertHarmonyToolchain(env?: NodeJS.ProcessEnv, deviceId?: string): string;
|
|
218
|
-
//#endregion
|
|
202
|
+
export declare function assertHarmonyToolchain(env?: NodeJS.ProcessEnv, deviceId?: string): string;
|
package/dist/index.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import process from "node:process";
|
|
|
4
4
|
import { Buffer } from "node:buffer";
|
|
5
5
|
import { spawnSync } from "node:child_process";
|
|
6
6
|
import spawn from "cross-spawn";
|
|
7
|
-
|
|
7
|
+
// src/fs.ts
|
|
8
8
|
async function fileExists(file) {
|
|
9
9
|
try {
|
|
10
10
|
await fs.access(file);
|
|
@@ -19,12 +19,11 @@ async function readUtf8(file) {
|
|
|
19
19
|
function wait(ms) {
|
|
20
20
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
21
21
|
}
|
|
22
|
-
|
|
23
|
-
//#region src/hbuilderx/configured-cli.ts
|
|
22
|
+
// src/hbuilderx/configured-cli.ts
|
|
24
23
|
const macOSStableCli = "/Applications/HBuilderX.app/Contents/MacOS/cli";
|
|
25
24
|
const macOSAlphaCli = "/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli";
|
|
26
25
|
function resolveCandidateSource(candidate, env) {
|
|
27
|
-
if (candidate === env
|
|
26
|
+
if (candidate === env["HBUILDERX_CLI_PATH"]) return "env";
|
|
28
27
|
if (candidate === "/Applications/HBuilderX.app/Contents/MacOS/cli" || candidate === "/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli") return "default-path";
|
|
29
28
|
return "candidate";
|
|
30
29
|
}
|
|
@@ -41,13 +40,12 @@ async function resolveConfiguredCli(options) {
|
|
|
41
40
|
source: resolveCandidateSource(candidate, env)
|
|
42
41
|
};
|
|
43
42
|
}
|
|
44
|
-
if (env
|
|
45
|
-
path: env
|
|
43
|
+
if (env["HBUILDERX_CLI_PATH"] && await fileExists(env["HBUILDERX_CLI_PATH"])) return {
|
|
44
|
+
path: env["HBUILDERX_CLI_PATH"],
|
|
46
45
|
source: "env"
|
|
47
46
|
};
|
|
48
47
|
}
|
|
49
|
-
|
|
50
|
-
//#region src/hbuilderx/discovery.ts
|
|
48
|
+
// src/hbuilderx/discovery.ts
|
|
51
49
|
function unique(items) {
|
|
52
50
|
return [...new Set(items)];
|
|
53
51
|
}
|
|
@@ -65,9 +63,9 @@ function inferHBuilderXChannel(file) {
|
|
|
65
63
|
if (/HBuilderX(?:\.app)?[\\/]/i.test(file) || /HBuilderX\.exe$/i.test(file)) return "stable";
|
|
66
64
|
return "unknown";
|
|
67
65
|
}
|
|
68
|
-
function getDefaultHBuilderXCliCandidates(env = process.env, channel = resolveHBuilderXChannel(env
|
|
66
|
+
function getDefaultHBuilderXCliCandidates(env = process.env, channel = resolveHBuilderXChannel(env["HBUILDERX_CHANNEL"]), platform = process.platform) {
|
|
69
67
|
const defaults = platform === "darwin" ? channel === "stable" ? [macOSStableCli] : channel === "alpha" ? [macOSAlphaCli] : [macOSStableCli, macOSAlphaCli] : [];
|
|
70
|
-
return unique([env
|
|
68
|
+
return unique([env["HBUILDERX_CLI_PATH"], ...defaults].filter((item) => Boolean(item)));
|
|
71
69
|
}
|
|
72
70
|
function extractHBuilderXMacOSExecutables(output) {
|
|
73
71
|
return unique(Array.from(output.matchAll(/(\/[^\r\n]*?HBuilderX(?:-Alpha|\s+Alpha)?\.app\/Contents\/MacOS\/HBuilderX)(?=\s|$)/gi), (match) => match[1]));
|
|
@@ -175,7 +173,7 @@ async function firstExisting(items) {
|
|
|
175
173
|
}
|
|
176
174
|
async function resolveHBuilderXCliInfoFromOptions(options = {}) {
|
|
177
175
|
const env = options.env ?? process.env;
|
|
178
|
-
const channel = resolveHBuilderXChannel(options.channel ?? env
|
|
176
|
+
const channel = resolveHBuilderXChannel(options.channel ?? env["HBUILDERX_CHANNEL"]);
|
|
179
177
|
const running = await findRunningHBuilderXCliCandidates();
|
|
180
178
|
const configured = await resolveConfiguredCli(options);
|
|
181
179
|
if (configured) return {
|
|
@@ -199,8 +197,7 @@ async function resolveHBuilderXCliInfoFromOptions(options = {}) {
|
|
|
199
197
|
};
|
|
200
198
|
throw new Error([`未找到 HBuilderX ${channel === "auto" ? "" : `${channel} `}CLI。`, "请安装 HBuilderX,或设置 HBUILDERX_CLI_PATH=/path/to/cli。"].join(""));
|
|
201
199
|
}
|
|
202
|
-
|
|
203
|
-
//#region src/logs.ts
|
|
200
|
+
// src/logs.ts
|
|
204
201
|
const PROJECT_NOT_UNI_APP_RE = /不是\s*uni-app\s*项目|not\s+a\s+uni-app\s+project/i;
|
|
205
202
|
const PROJECT_TYPE_UNSUPPORTED_RE = /项目类型为([^,]+),暂不支持|是\s+([^,\s][^,]*)项目,暂不支持|project\s+type.+unsupported/i;
|
|
206
203
|
const PROJECT_TARGET_UNSUPPORTED_RE = /不支持运行到\s*(鸿蒙|Harmony)|(?:Harmony|OpenHarmony).+(?:target|platform).+unsupported/i;
|
|
@@ -329,38 +326,140 @@ function formatRecentLogs(logs, maxLength = 6e3) {
|
|
|
329
326
|
const important = (importantLogsByBuffer.get(logs) ?? []).filter((line) => !recent.includes(line));
|
|
330
327
|
return important.length > 0 ? `关键错误:\n${important.join("\n")}\n\n最近日志:\n${recent}` : recent;
|
|
331
328
|
}
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
329
|
+
// src/process/termination.ts
|
|
330
|
+
const ownedGroups = /* @__PURE__ */ new WeakMap();
|
|
331
|
+
const closedChildren = /* @__PURE__ */ new WeakSet();
|
|
332
|
+
const shutdownWaitMs = 1e3;
|
|
333
|
+
/** 只记录本次 spawn 创建的独立进程组,不从外部 PID 猜测组归属。 */
|
|
334
|
+
function registerProcessGroup(child, detached) {
|
|
335
|
+
if (process.platform !== "win32" && detached && child.pid) ownedGroups.set(child, child.pid);
|
|
336
|
+
child.once("close", () => {
|
|
337
|
+
closedChildren.add(child);
|
|
338
|
+
ownedGroups.delete(child);
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
function signalOwnedTree(child, signal, group) {
|
|
342
|
+
if (!child.pid) return;
|
|
345
343
|
if (process.platform === "win32") {
|
|
346
|
-
|
|
344
|
+
if (child.exitCode !== null || child.signalCode !== null || closedChildren.has(child)) return;
|
|
345
|
+
const result = spawnSync("taskkill", [
|
|
347
346
|
"/pid",
|
|
348
|
-
String(pid),
|
|
347
|
+
String(child.pid),
|
|
349
348
|
"/t",
|
|
350
349
|
"/f"
|
|
351
350
|
], {
|
|
352
|
-
|
|
351
|
+
timeout: 5e3,
|
|
352
|
+
killSignal: "SIGKILL",
|
|
353
|
+
maxBuffer: 65536,
|
|
354
|
+
encoding: "utf8",
|
|
355
|
+
stdio: [
|
|
356
|
+
"ignore",
|
|
357
|
+
"pipe",
|
|
358
|
+
"pipe"
|
|
359
|
+
],
|
|
353
360
|
windowsHide: true
|
|
354
361
|
});
|
|
362
|
+
if (result.error || result.status !== 0) throw new Error(`taskkill pid=${child.pid} 失败,exit=${result.status} signal=${result.signal}\n${result.error?.message ?? ""}\n${result.stderr ?? ""}\n${result.stdout ?? ""}`, { cause: result.error });
|
|
355
363
|
return;
|
|
356
364
|
}
|
|
357
|
-
try {
|
|
358
|
-
process.kill(-
|
|
359
|
-
|
|
365
|
+
if (group) try {
|
|
366
|
+
process.kill(-group, signal);
|
|
367
|
+
return;
|
|
368
|
+
} catch (error) {
|
|
369
|
+
if (error.code !== "ESRCH") throw error;
|
|
370
|
+
}
|
|
371
|
+
if (!closedChildren.has(child) && child.exitCode === null && child.signalCode === null && !child.kill(signal)) throw new Error(`未能向子进程 pid=${child.pid} 发送 ${signal}`);
|
|
372
|
+
}
|
|
373
|
+
/** 同步入口在 close 后失效,避免稍后向复用的 PID 或进程组发送信号。 */
|
|
374
|
+
function signalProcessTree(child, signal) {
|
|
375
|
+
if (!closedChildren.has(child)) signalOwnedTree(child, signal, ownedGroups.get(child));
|
|
376
|
+
}
|
|
377
|
+
function waitForCleanup(closed, isGroupGone) {
|
|
378
|
+
return new Promise((resolve, reject) => {
|
|
379
|
+
let didClose = false;
|
|
380
|
+
let finished = false;
|
|
381
|
+
let lastProbeError;
|
|
382
|
+
const finish = (done, error) => {
|
|
383
|
+
finished = true;
|
|
384
|
+
clearTimeout(deadline);
|
|
385
|
+
clearInterval(poll);
|
|
386
|
+
if (error) reject(error);
|
|
387
|
+
else resolve(done);
|
|
388
|
+
};
|
|
389
|
+
const check = () => {
|
|
390
|
+
if (finished) return;
|
|
391
|
+
try {
|
|
392
|
+
const groupGone = isGroupGone();
|
|
393
|
+
lastProbeError = void 0;
|
|
394
|
+
if (didClose && groupGone) finish(true);
|
|
395
|
+
} catch (error) {
|
|
396
|
+
lastProbeError = error;
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
const expire = () => finish(false, lastProbeError);
|
|
400
|
+
const deadline = setTimeout(expire, shutdownWaitMs);
|
|
401
|
+
const poll = setInterval(check, 25);
|
|
402
|
+
closed.then(() => {
|
|
403
|
+
didClose = true;
|
|
404
|
+
check();
|
|
405
|
+
});
|
|
406
|
+
check();
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
/** 清理预算独立于业务超时;只持有本轮开始时仍有效的组归属。 */
|
|
410
|
+
async function stopProcessTree(child, closed, signal) {
|
|
411
|
+
let group = ownedGroups.get(child);
|
|
412
|
+
const errors = [];
|
|
413
|
+
const exitRaces = [];
|
|
414
|
+
const isGroupGone = () => {
|
|
415
|
+
if (!group) return true;
|
|
416
|
+
try {
|
|
417
|
+
process.kill(-group, 0);
|
|
418
|
+
return false;
|
|
419
|
+
} catch (error) {
|
|
420
|
+
if (error.code !== "ESRCH") throw error;
|
|
421
|
+
group = void 0;
|
|
422
|
+
ownedGroups.delete(child);
|
|
423
|
+
return true;
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
const signals = process.platform === "win32" || signal === "SIGKILL" ? [signal] : [signal, "SIGKILL"];
|
|
427
|
+
for (const nextSignal of signals) {
|
|
360
428
|
try {
|
|
361
|
-
child
|
|
362
|
-
} catch {
|
|
429
|
+
signalOwnedTree(child, nextSignal, group);
|
|
430
|
+
} catch (error) {
|
|
431
|
+
if (process.platform === "darwin" && group && (child.exitCode !== null || child.signalCode !== null) && error.code === "EPERM") exitRaces.push(error);
|
|
432
|
+
else errors.push(error);
|
|
433
|
+
}
|
|
434
|
+
try {
|
|
435
|
+
if (await waitForCleanup(closed, isGroupGone)) {
|
|
436
|
+
if (!errors.length) return;
|
|
437
|
+
break;
|
|
438
|
+
}
|
|
439
|
+
} catch (error) {
|
|
440
|
+
errors.push(error);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
const failures = [...errors, ...exitRaces];
|
|
444
|
+
const details = failures.map((error) => error instanceof Error ? error.message : String(error)).join("\n");
|
|
445
|
+
throw new AggregateError(failures, `子进程 pid=${child.pid ?? "unknown"} 的有界清理未成功:需确认 close 和本轮进程组结束\n${details}`.trim());
|
|
446
|
+
}
|
|
447
|
+
// src/process.ts
|
|
448
|
+
var HBuilderXCommandError = class extends Error {
|
|
449
|
+
result;
|
|
450
|
+
cleanupError;
|
|
451
|
+
constructor(message, result, cleanupError) {
|
|
452
|
+
super(message, cleanupError ? { cause: cleanupError } : void 0);
|
|
453
|
+
this.name = "HBuilderXCommandError";
|
|
454
|
+
this.result = result;
|
|
455
|
+
if (cleanupError) this.cleanupError = cleanupError;
|
|
363
456
|
}
|
|
457
|
+
};
|
|
458
|
+
/** 保留同步请求终止的兼容入口;需要确认关闭时必须等待 spawned.stop()。 */
|
|
459
|
+
function killProcessTree(child, signal = "SIGTERM") {
|
|
460
|
+
try {
|
|
461
|
+
signalProcessTree(child, signal);
|
|
462
|
+
} catch {}
|
|
364
463
|
}
|
|
365
464
|
function createCommandResult(options, exit, logs, timedOut = false) {
|
|
366
465
|
const output = formatRecentLogs(logs);
|
|
@@ -374,21 +473,27 @@ function createCommandResult(options, exit, logs, timedOut = false) {
|
|
|
374
473
|
issue: timedOut ? createTimeoutIssue() : classifyHBuilderXOutput(output)
|
|
375
474
|
};
|
|
376
475
|
}
|
|
377
|
-
function buildCommandError(prefix, options, exit, logs, timedOut = false) {
|
|
476
|
+
function buildCommandError(prefix, options, exit, logs, timedOut = false, cleanupError) {
|
|
378
477
|
const result = createCommandResult(options, exit, logs, timedOut);
|
|
478
|
+
if (cleanupError && !timedOut) result.issue = {
|
|
479
|
+
kind: "process-exit",
|
|
480
|
+
message: "子进程清理失败,未确认关闭或进程树终止。"
|
|
481
|
+
};
|
|
379
482
|
return new HBuilderXCommandError([
|
|
380
483
|
`${prefix}: ${options.command} ${options.args.join(" ")}`,
|
|
381
484
|
`cwd=${options.cwd}`,
|
|
382
485
|
`exit=${exit.signal ?? exit.code}`,
|
|
383
486
|
`issue=${result.issue.kind}: ${result.issue.message}`,
|
|
384
487
|
result.issue.hint ? `hint=${result.issue.hint}` : "",
|
|
385
|
-
result.output
|
|
386
|
-
|
|
488
|
+
result.output,
|
|
489
|
+
cleanupError ? `cleanup=${cleanupError.message}` : ""
|
|
490
|
+
].filter(Boolean).join("\n"), result, cleanupError);
|
|
387
491
|
}
|
|
388
492
|
function spawnCommand(options) {
|
|
493
|
+
const detached = options.detached ?? process.platform !== "win32";
|
|
389
494
|
const child = spawn(options.command, options.args, {
|
|
390
495
|
cwd: options.cwd,
|
|
391
|
-
detached
|
|
496
|
+
detached,
|
|
392
497
|
stdio: options.stdio === "inherit" ? "inherit" : [
|
|
393
498
|
"ignore",
|
|
394
499
|
"pipe",
|
|
@@ -397,14 +502,16 @@ function spawnCommand(options) {
|
|
|
397
502
|
env: {
|
|
398
503
|
...process.env,
|
|
399
504
|
...options.env,
|
|
400
|
-
NODE_OPTIONS: options.env?.NODE_OPTIONS ?? process.env
|
|
505
|
+
NODE_OPTIONS: options.env?.["NODE_OPTIONS"] ?? process.env["NODE_OPTIONS"] ?? "--max-old-space-size=8192"
|
|
401
506
|
}
|
|
402
507
|
});
|
|
508
|
+
registerProcessGroup(child, detached);
|
|
403
509
|
const logs = options.stdio === "inherit" ? [] : collectProcessOutput(child);
|
|
404
510
|
child.on("error", (error) => logs.push(`${error.message}\n`));
|
|
405
511
|
let exit;
|
|
512
|
+
let stopping;
|
|
406
513
|
const closed = new Promise((resolve) => {
|
|
407
|
-
child.
|
|
514
|
+
child.once("close", (code, signal) => {
|
|
408
515
|
exit = {
|
|
409
516
|
code,
|
|
410
517
|
signal
|
|
@@ -422,9 +529,16 @@ function spawnCommand(options) {
|
|
|
422
529
|
ensureRunning() {
|
|
423
530
|
if (exit && exit.code !== 0) throw buildCommandError("命令提前退出", options, exit, logs);
|
|
424
531
|
},
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
532
|
+
stop(signal = "SIGTERM") {
|
|
533
|
+
if (stopping) return stopping;
|
|
534
|
+
if (exit) return Promise.resolve();
|
|
535
|
+
stopping = stopProcessTree(child, closed, signal).catch((error) => {
|
|
536
|
+
throw buildCommandError("命令清理失败", options, {
|
|
537
|
+
code: child.exitCode,
|
|
538
|
+
signal: child.signalCode
|
|
539
|
+
}, logs, false, error);
|
|
540
|
+
});
|
|
541
|
+
return stopping;
|
|
428
542
|
}
|
|
429
543
|
};
|
|
430
544
|
}
|
|
@@ -433,19 +547,26 @@ async function runCommand(options) {
|
|
|
433
547
|
let timer;
|
|
434
548
|
let timedOut = false;
|
|
435
549
|
try {
|
|
436
|
-
const
|
|
550
|
+
const deadline = Symbol("deadline");
|
|
551
|
+
const outcome = await (options.timeoutMs == null ? spawned.closed : Promise.race([spawned.closed, new Promise((resolve) => {
|
|
437
552
|
timer = setTimeout(() => {
|
|
438
553
|
timedOut = true;
|
|
439
|
-
|
|
440
|
-
resolve({
|
|
441
|
-
code: null,
|
|
442
|
-
signal: "SIGTERM"
|
|
443
|
-
});
|
|
554
|
+
resolve(deadline);
|
|
444
555
|
}, options.timeoutMs);
|
|
445
556
|
})]));
|
|
557
|
+
if (outcome === deadline) try {
|
|
558
|
+
await spawned.stop();
|
|
559
|
+
} catch (error) {
|
|
560
|
+
const cleanupError = error instanceof HBuilderXCommandError ? error.cleanupError ?? error : error;
|
|
561
|
+
throw buildCommandError("命令超时且清理失败", options, {
|
|
562
|
+
code: spawned.child.exitCode,
|
|
563
|
+
signal: spawned.child.signalCode
|
|
564
|
+
}, spawned.logs, true, cleanupError);
|
|
565
|
+
}
|
|
566
|
+
const exit = outcome === deadline ? await spawned.closed : outcome;
|
|
446
567
|
const result = createCommandResult(options, exit, spawned.logs, timedOut);
|
|
447
|
-
if (exit.code === 0 || options.allowFailure) return result;
|
|
448
|
-
throw buildCommandError(
|
|
568
|
+
if (!timedOut && exit.code === 0 || options.allowFailure) return result;
|
|
569
|
+
throw buildCommandError(timedOut ? "命令超时" : "命令失败", options, exit, spawned.logs, timedOut);
|
|
449
570
|
} finally {
|
|
450
571
|
if (timer) clearTimeout(timer);
|
|
451
572
|
}
|
|
@@ -474,8 +595,7 @@ function createProcessExitTracker(child) {
|
|
|
474
595
|
}
|
|
475
596
|
};
|
|
476
597
|
}
|
|
477
|
-
|
|
478
|
-
//#region src/hbuilderx/hosts.ts
|
|
598
|
+
// src/hbuilderx/hosts.ts
|
|
479
599
|
const ansiRE = new RegExp(`${String.fromCharCode(27)}\\[[\\d;]*m`, "g");
|
|
480
600
|
function stripAnsi(value) {
|
|
481
601
|
return value.replace(ansiRE, "");
|
|
@@ -551,7 +671,7 @@ async function connectHBuilderXHost(options) {
|
|
|
551
671
|
};
|
|
552
672
|
const assertCommand = (result) => {
|
|
553
673
|
if (result.issue.kind === "cli-instance-mismatch" || result.issue.kind === "cli-host-ambiguous") throw createRunnerError(cliPath, cwd, result.args, result.issue.kind, result.issue.message, result.issue.hint ?? "请检查目标 HBuilderX host。", logs);
|
|
554
|
-
if (result.exit.code !== 0) throw new HBuilderXCommandError(`HBuilderX host 命令失败:${cliPath} ${result.args.join(" ")}\ncwd=${cwd}\nexit=${result.exit.signal ?? result.exit.code}\nissue=${result.issue.kind}\n${formatRecentLogs(logs)}`, result);
|
|
674
|
+
if (result.issue.kind === "timeout" || result.exit.code !== 0) throw new HBuilderXCommandError(`HBuilderX host 命令失败:${cliPath} ${result.args.join(" ")}\ncwd=${cwd}\nexit=${result.exit.signal ?? result.exit.code}\nissue=${result.issue.kind}\n${formatRecentLogs(logs)}`, result);
|
|
555
675
|
};
|
|
556
676
|
const probe = async () => {
|
|
557
677
|
const listed = await command(["listhost"]);
|
|
@@ -588,8 +708,7 @@ async function connectHBuilderXHost(options) {
|
|
|
588
708
|
} while (Date.now() < deadline);
|
|
589
709
|
throw timeoutError(["listhost"]);
|
|
590
710
|
}
|
|
591
|
-
|
|
592
|
-
//#region src/hbuilderx/runner.ts
|
|
711
|
+
// src/hbuilderx/runner.ts
|
|
593
712
|
const defaultStartupTimeoutMs = 6e4;
|
|
594
713
|
function createHBuilderXNativeLaunchArgs(options) {
|
|
595
714
|
const args = [
|
|
@@ -620,11 +739,12 @@ async function createHBuilderXRunner(options = {}) {
|
|
|
620
739
|
};
|
|
621
740
|
const cwd = options.cwd ?? process.cwd();
|
|
622
741
|
const timeoutMs = options.timeoutMs ?? defaultStartupTimeoutMs;
|
|
742
|
+
const candidates = options.hbuilderxCliPath ? [options.hbuilderxCliPath] : options.candidates;
|
|
623
743
|
const resolveOptions = {
|
|
624
|
-
candidates
|
|
625
|
-
channel: options.channel,
|
|
744
|
+
...candidates !== void 0 ? { candidates } : {},
|
|
745
|
+
...options.channel !== void 0 ? { channel: options.channel } : {},
|
|
626
746
|
env,
|
|
627
|
-
host: options.host
|
|
747
|
+
...options.host !== void 0 ? { host: options.host } : {}
|
|
628
748
|
};
|
|
629
749
|
const configured = await resolveConfiguredCli(resolveOptions);
|
|
630
750
|
const resolution = configured ? {
|
|
@@ -635,15 +755,15 @@ async function createHBuilderXRunner(options = {}) {
|
|
|
635
755
|
...env,
|
|
636
756
|
HBUILDERX_CLI_PATH: resolution.path
|
|
637
757
|
};
|
|
638
|
-
const requestedChannel = resolveHBuilderXChannel(options.channel ?? env
|
|
758
|
+
const requestedChannel = resolveHBuilderXChannel(options.channel ?? env["HBUILDERX_CHANNEL"]);
|
|
639
759
|
const expectedChannel = resolution.channel !== "unknown" ? resolution.channel : requestedChannel === "auto" ? inferHBuilderXChannel(resolution.path) : requestedChannel;
|
|
640
|
-
const explicitHost = options.host ?? env
|
|
760
|
+
const explicitHost = options.host ?? env["HBUILDERX_HOST"];
|
|
641
761
|
const selected = await connectHBuilderXHost({
|
|
642
762
|
cliPath: resolution.path,
|
|
643
763
|
cwd,
|
|
644
764
|
env: cliEnv,
|
|
645
765
|
expectedChannel,
|
|
646
|
-
explicitHost,
|
|
766
|
+
...explicitHost !== void 0 ? { explicitHost } : {},
|
|
647
767
|
timeoutMs
|
|
648
768
|
});
|
|
649
769
|
const boundResolution = {
|
|
@@ -722,8 +842,7 @@ async function createHBuilderXRunner(options = {}) {
|
|
|
722
842
|
};
|
|
723
843
|
return runner;
|
|
724
844
|
}
|
|
725
|
-
|
|
726
|
-
//#region src/hbuilderx.ts
|
|
845
|
+
// src/hbuilderx.ts
|
|
727
846
|
async function resolveHBuilderXCliInfo(candidatesOrOptions, env = process.env) {
|
|
728
847
|
return await resolveHBuilderXCliInfoFromOptions(Array.isArray(candidatesOrOptions) ? {
|
|
729
848
|
candidates: candidatesOrOptions,
|
|
@@ -732,14 +851,14 @@ async function resolveHBuilderXCliInfo(candidatesOrOptions, env = process.env) {
|
|
|
732
851
|
}
|
|
733
852
|
async function resolveHBuilderXCli(candidatesOrOptions) {
|
|
734
853
|
const options = Array.isArray(candidatesOrOptions) ? { candidates: candidatesOrOptions } : candidatesOrOptions ?? {};
|
|
735
|
-
resolveHBuilderXChannel(options.channel ?? (options.env ?? process.env)
|
|
854
|
+
resolveHBuilderXChannel(options.channel ?? (options.env ?? process.env)["HBUILDERX_CHANNEL"]);
|
|
736
855
|
return (await resolveConfiguredCli(options))?.path ?? (await resolveHBuilderXCliInfoFromOptions(options)).path;
|
|
737
856
|
}
|
|
738
857
|
function createHBuilderXEnv(options = {}) {
|
|
739
858
|
return {
|
|
740
859
|
...options.env,
|
|
741
|
-
HBUILDERX_CLI_PATH: options.hbuilderxCliPath ?? options.env?.HBUILDERX_CLI_PATH ?? process.env
|
|
742
|
-
NODE_OPTIONS: options.env?.NODE_OPTIONS ?? process.env
|
|
860
|
+
HBUILDERX_CLI_PATH: options.hbuilderxCliPath ?? options.env?.["HBUILDERX_CLI_PATH"] ?? process.env["HBUILDERX_CLI_PATH"] ?? getDefaultHBuilderXCliCandidates()[0],
|
|
861
|
+
NODE_OPTIONS: options.env?.["NODE_OPTIONS"] ?? process.env["NODE_OPTIONS"] ?? "--max-old-space-size=8192"
|
|
743
862
|
};
|
|
744
863
|
}
|
|
745
864
|
function hbuilderxPnpmArgs(args) {
|
|
@@ -754,8 +873,8 @@ async function runPnpmCommand(options) {
|
|
|
754
873
|
command: "pnpm",
|
|
755
874
|
args: options.args,
|
|
756
875
|
cwd: options.cwd,
|
|
757
|
-
timeoutMs: options.timeoutMs,
|
|
758
|
-
allowFailure: options.allowFailure,
|
|
876
|
+
...options.timeoutMs !== void 0 ? { timeoutMs: options.timeoutMs } : {},
|
|
877
|
+
...options.allowFailure !== void 0 ? { allowFailure: options.allowFailure } : {},
|
|
759
878
|
env: createHBuilderXEnv(options)
|
|
760
879
|
});
|
|
761
880
|
}
|
|
@@ -800,8 +919,7 @@ function startLaunch(options) {
|
|
|
800
919
|
args: createLaunchArgs(options)
|
|
801
920
|
});
|
|
802
921
|
}
|
|
803
|
-
|
|
804
|
-
//#region src/toolchains.ts
|
|
922
|
+
// src/toolchains.ts
|
|
805
923
|
function runTool(command, args) {
|
|
806
924
|
const result = spawnSync(command, args, { encoding: "utf8" });
|
|
807
925
|
return {
|
|
@@ -819,9 +937,9 @@ function resolveAdbCommand(env = process.env) {
|
|
|
819
937
|
env: {}
|
|
820
938
|
};
|
|
821
939
|
const candidates = [
|
|
822
|
-
env
|
|
823
|
-
env
|
|
824
|
-
env
|
|
940
|
+
env["ANDROID_HOME"],
|
|
941
|
+
env["ANDROID_SDK_ROOT"],
|
|
942
|
+
env["HOME"] ? path.join(env["HOME"], "Library/Android/sdk") : void 0,
|
|
825
943
|
"/opt/android-sdk",
|
|
826
944
|
"/usr/local/share/android-sdk"
|
|
827
945
|
].filter((item) => Boolean(item));
|
|
@@ -832,9 +950,9 @@ function resolveAdbCommand(env = process.env) {
|
|
|
832
950
|
command: adbPath,
|
|
833
951
|
output: candidate.output,
|
|
834
952
|
env: {
|
|
835
|
-
ANDROID_HOME: env
|
|
836
|
-
ANDROID_SDK_ROOT: env
|
|
837
|
-
PATH: `${path.dirname(adbPath)}${path.delimiter}${env
|
|
953
|
+
ANDROID_HOME: env["ANDROID_HOME"] ?? sdkRoot,
|
|
954
|
+
ANDROID_SDK_ROOT: env["ANDROID_SDK_ROOT"] ?? sdkRoot,
|
|
955
|
+
PATH: `${path.dirname(adbPath)}${path.delimiter}${env["PATH"] ?? ""}`
|
|
838
956
|
}
|
|
839
957
|
};
|
|
840
958
|
}
|
|
@@ -863,7 +981,7 @@ function assertIosSimulatorToolchain(env = process.env) {
|
|
|
863
981
|
if (!simctl.ok || !xcodebuild.ok || firstLaunchStatus?.ok === false) throw new Error([
|
|
864
982
|
"当前机器缺少 iOS 模拟器所需的完整 Xcode 工具链,无法运行 HBuilderX app-ios E2E。",
|
|
865
983
|
`xcode-select: ${xcodeSelect.output || "unknown"}`,
|
|
866
|
-
`DEVELOPER_DIR: ${env
|
|
984
|
+
`DEVELOPER_DIR: ${env["DEVELOPER_DIR"] || "未设置"}`,
|
|
867
985
|
`simctl: ${simctl.output || "not found"}`,
|
|
868
986
|
`xcodebuild: ${xcodebuild.output || "not found"}`,
|
|
869
987
|
`firstLaunchStatus: ${firstLaunchStatus?.output || (firstLaunchStatus?.ok === false ? "not ready" : "unknown")}`,
|
|
@@ -909,7 +1027,7 @@ function selectPreferredIosSimulatorDevice(devices) {
|
|
|
909
1027
|
})[0];
|
|
910
1028
|
}
|
|
911
1029
|
function resolveIosSimulatorDeviceId(env = process.env) {
|
|
912
|
-
const explicitDeviceId = env
|
|
1030
|
+
const explicitDeviceId = env["E2E_HBUILDERX_IOS_DEVICE_ID"];
|
|
913
1031
|
if (explicitDeviceId) return explicitDeviceId;
|
|
914
1032
|
const booted = runTool("xcrun", [
|
|
915
1033
|
"simctl",
|
|
@@ -938,9 +1056,9 @@ function resolveIosSimulatorDeviceId(env = process.env) {
|
|
|
938
1056
|
}
|
|
939
1057
|
function resolveHdcCommand(env = process.env) {
|
|
940
1058
|
const candidates = [
|
|
941
|
-
env
|
|
942
|
-
env
|
|
943
|
-
env
|
|
1059
|
+
env["DEMO_VISUAL_HARMONY_HDC_PATH"],
|
|
1060
|
+
env["E2E_HBUILDERX_HARMONY_HDC_PATH"],
|
|
1061
|
+
env["HDC_PATH"],
|
|
944
1062
|
"hdc",
|
|
945
1063
|
process.platform === "darwin" ? "/Applications/DevEco-Studio.app/Contents/sdk/default/openharmony/toolchains/hdc" : void 0
|
|
946
1064
|
].filter((item) => Boolean(item));
|
|
@@ -971,5 +1089,4 @@ function assertHarmonyToolchain(env = process.env, deviceId) {
|
|
|
971
1089
|
}
|
|
972
1090
|
return hdc;
|
|
973
1091
|
}
|
|
974
|
-
//#endregion
|
|
975
1092
|
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,18 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weapp-tailwindcss/hbuilderx-runner",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.8",
|
|
5
5
|
"description": "HBuilderX runner utilities for cross-platform uni-app Tailwind CSS workflows. 跨端 uni-app Tailwind CSS 工作流的 HBuilderX 运行器工具。",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"homepage": "https://tw.weapp.dev/docs/quick-start/frameworks/hbuilderx",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "git+https://github.com/
|
|
11
|
+
"url": "git+https://github.com/weapp-tailwindcss/weapp-tailwindcss.git",
|
|
12
12
|
"directory": "packages/hbuilderx-runner"
|
|
13
13
|
},
|
|
14
14
|
"bugs": {
|
|
15
|
-
"url": "https://github.com/
|
|
15
|
+
"url": "https://github.com/weapp-tailwindcss/weapp-tailwindcss/issues"
|
|
16
16
|
},
|
|
17
17
|
"keywords": [
|
|
18
18
|
"hbuilderx",
|
|
@@ -52,6 +52,7 @@
|
|
|
52
52
|
"dev": "tsdown --watch --sourcemap",
|
|
53
53
|
"build": "tsdown",
|
|
54
54
|
"test": "vitest run",
|
|
55
|
+
"typecheck": "tsc -p tsconfig.json --pretty false",
|
|
55
56
|
"test:dev": "vitest",
|
|
56
57
|
"release": "pnpm publish",
|
|
57
58
|
"lint": "eslint .",
|