@weapp-tailwindcss/hbuilderx-runner 0.1.3 → 0.1.5
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 +2 -0
- package/README.zh-CN.md +2 -0
- package/dist/index.cjs +188 -126
- package/dist/index.d.ts +93 -90
- package/dist/index.mjs +187 -126
- package/package.json +7 -1
package/README.md
CHANGED
|
@@ -47,6 +47,8 @@ The default channel is `auto`. Resolution uses this precedence:
|
|
|
47
47
|
|
|
48
48
|
The macOS defaults are `/Applications/HBuilderX.app/Contents/MacOS/cli` and `/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli`. Set `HBUILDERX_CLI_PATH` for non-standard Windows/Linux installations.
|
|
49
49
|
|
|
50
|
+
`resolveHBuilderXCli` returns an explicitly configured existing path without querying running processes. `resolveHBuilderXCliInfo` also reports `isRunning`, so it still queries the operating system. A configured runner connects through the native CLI host handshake.
|
|
51
|
+
|
|
50
52
|
If one CLI matches multiple hosts, the runner reports an ambiguity instead of guessing. Set `HBUILDERX_HOST` or pass `host` to `createHBuilderXRunner`.
|
|
51
53
|
|
|
52
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.
|
package/README.zh-CN.md
CHANGED
|
@@ -48,6 +48,8 @@ await launch.stop()
|
|
|
48
48
|
|
|
49
49
|
macOS 默认路径为 `/Applications/HBuilderX.app/Contents/MacOS/cli` 和 `/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli`。Windows/Linux 的非标准安装请设置 `HBUILDERX_CLI_PATH`。
|
|
50
50
|
|
|
51
|
+
`resolveHBuilderXCli` 只返回已配置且存在的路径,不查询运行进程;`resolveHBuilderXCliInfo` 还需要返回 `isRunning`,因此保留操作系统查询。已配置路径的 runner 通过原生 CLI 的 host 握手连接实例。
|
|
52
|
+
|
|
51
53
|
当同一 CLI 匹配到多个 host 时,runner 会拒绝猜测。请设置 `HBUILDERX_HOST`,或向 `createHBuilderXRunner` 传入 `host`。
|
|
52
54
|
|
|
53
55
|
部分 HBuilderX 版本自身会拒绝 stable 与 Alpha 跨版本并行运行。runner 不会擅自关闭当前实例;目标版本无法启动时会抛出 `cli-instance-mismatch`,请关闭冲突实例后重试。
|
package/dist/index.cjs
CHANGED
|
@@ -27,7 +27,10 @@ let node_path = require("node:path");
|
|
|
27
27
|
node_path = __toESM(node_path, 1);
|
|
28
28
|
let node_process = require("node:process");
|
|
29
29
|
node_process = __toESM(node_process, 1);
|
|
30
|
+
let node_buffer = require("node:buffer");
|
|
30
31
|
let node_child_process = require("node:child_process");
|
|
32
|
+
let cross_spawn = require("cross-spawn");
|
|
33
|
+
cross_spawn = __toESM(cross_spawn, 1);
|
|
31
34
|
//#region src/fs.ts
|
|
32
35
|
async function fileExists(file) {
|
|
33
36
|
try {
|
|
@@ -44,9 +47,34 @@ function wait(ms) {
|
|
|
44
47
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
45
48
|
}
|
|
46
49
|
//#endregion
|
|
47
|
-
//#region src/hbuilderx/
|
|
50
|
+
//#region src/hbuilderx/configured-cli.ts
|
|
48
51
|
const macOSStableCli = "/Applications/HBuilderX.app/Contents/MacOS/cli";
|
|
49
52
|
const macOSAlphaCli = "/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli";
|
|
53
|
+
function resolveCandidateSource(candidate, env) {
|
|
54
|
+
if (candidate === env.HBUILDERX_CLI_PATH) return "env";
|
|
55
|
+
if (candidate === "/Applications/HBuilderX.app/Contents/MacOS/cli" || candidate === "/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli") return "default-path";
|
|
56
|
+
return "candidate";
|
|
57
|
+
}
|
|
58
|
+
async function firstExisting$1(items) {
|
|
59
|
+
for (const item of items) if (await fileExists(item)) return item;
|
|
60
|
+
}
|
|
61
|
+
async function resolveConfiguredCli(options) {
|
|
62
|
+
const env = options.env ?? node_process.default.env;
|
|
63
|
+
if (options.candidates) {
|
|
64
|
+
const candidate = await firstExisting$1(options.candidates);
|
|
65
|
+
if (!candidate) throw new Error("未找到显式指定的 HBuilderX CLI candidate。");
|
|
66
|
+
return {
|
|
67
|
+
path: candidate,
|
|
68
|
+
source: resolveCandidateSource(candidate, env)
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
if (env.HBUILDERX_CLI_PATH && await fileExists(env.HBUILDERX_CLI_PATH)) return {
|
|
72
|
+
path: env.HBUILDERX_CLI_PATH,
|
|
73
|
+
source: "env"
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region src/hbuilderx/discovery.ts
|
|
50
78
|
function unique(items) {
|
|
51
79
|
return [...new Set(items)];
|
|
52
80
|
}
|
|
@@ -92,27 +120,64 @@ function extractHBuilderXExecutableFromProcessOutput(output, platform = node_pro
|
|
|
92
120
|
return extractHBuilderXExecutablesFromProcessOutput(output, platform)[0];
|
|
93
121
|
}
|
|
94
122
|
async function findRunningHBuilderXCliCandidates(platform = node_process.default.platform) {
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
"
|
|
98
|
-
"
|
|
99
|
-
"
|
|
100
|
-
"
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
123
|
+
const command = platform === "win32" ? "powershell.exe" : "ps";
|
|
124
|
+
const args = platform === "win32" ? [
|
|
125
|
+
"-NoLogo",
|
|
126
|
+
"-NoProfile",
|
|
127
|
+
"-NonInteractive",
|
|
128
|
+
"-Command",
|
|
129
|
+
[
|
|
130
|
+
"$ErrorActionPreference = 'Stop'",
|
|
131
|
+
"[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false)",
|
|
132
|
+
`$PSModuleAutoLoadingPreference = 'None'`,
|
|
133
|
+
`[Console]::Error.WriteLine('process-discovery: querying')`,
|
|
134
|
+
`$paths = [System.Collections.Generic.List[string]]::new()`,
|
|
135
|
+
`foreach ($p in [System.Diagnostics.Process]::GetProcessesByName('HBuilderX')) { try { $paths.Add([Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($p.MainModule.FileName))) } catch {} finally { $p.Dispose() } }`,
|
|
136
|
+
`[Console]::Error.WriteLine('process-discovery: complete')`,
|
|
137
|
+
`[Console]::WriteLine('WT-HBUILDERX-PROCESSES/1')`,
|
|
138
|
+
`[Console]::WriteLine([string]::Join([Environment]::NewLine, $paths))`,
|
|
139
|
+
`[Console]::WriteLine('END')`
|
|
140
|
+
].join("; ")
|
|
141
|
+
] : [
|
|
106
142
|
"-ax",
|
|
107
143
|
"-o",
|
|
108
144
|
"command="
|
|
109
|
-
]
|
|
110
|
-
|
|
111
|
-
const
|
|
145
|
+
];
|
|
146
|
+
const timeout = 15e3;
|
|
147
|
+
const maxAttempts = platform === "win32" ? 3 : 1;
|
|
148
|
+
let result;
|
|
149
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
150
|
+
result = (0, node_child_process.spawnSync)(command, args, {
|
|
151
|
+
encoding: "utf8",
|
|
152
|
+
stdio: [
|
|
153
|
+
"ignore",
|
|
154
|
+
"pipe",
|
|
155
|
+
"pipe"
|
|
156
|
+
],
|
|
157
|
+
windowsHide: true,
|
|
158
|
+
timeout,
|
|
159
|
+
maxBuffer: 1048576
|
|
160
|
+
});
|
|
161
|
+
if (!result.error && result.status === 0) break;
|
|
162
|
+
if (!(result.signal === "SIGTERM" || result.error?.code === "ETIMEDOUT") || attempt === maxAttempts - 1) break;
|
|
163
|
+
}
|
|
164
|
+
if (!result || result.error || result.status !== 0) throw new Error(`HBuilderX 进程探测失败:${command}, exit=${result?.status}, signal=${result?.signal}, cwd=${node_process.default.cwd()}\n${result?.error?.message ?? ""}\n${result?.stderr ?? ""}\n${result?.stdout ?? ""}`);
|
|
165
|
+
let executables;
|
|
166
|
+
if (platform === "win32") {
|
|
167
|
+
const lines = result.stdout.replace(/^\uFEFF/, "").trim().split(/\r?\n/);
|
|
168
|
+
if (lines.shift() !== "WT-HBUILDERX-PROCESSES/1" || lines.pop() !== "END") throw new Error("HBuilderX Windows 进程探测返回了不完整的路径列表");
|
|
169
|
+
executables = lines.filter(Boolean).map((line) => {
|
|
170
|
+
const bytes = node_buffer.Buffer.from(line, "base64");
|
|
171
|
+
const executable = bytes.toString("utf8");
|
|
172
|
+
if (bytes.toString("base64") !== line || !node_buffer.Buffer.from(executable).equals(bytes) || !node_path.default.win32.isAbsolute(executable) || node_path.default.win32.parse(executable).root.length < 3 || node_path.default.win32.basename(executable).toLowerCase() !== "hbuilderx.exe") throw new Error("HBuilderX Windows 进程探测返回了无效路径列表");
|
|
173
|
+
return executable;
|
|
174
|
+
});
|
|
175
|
+
} else executables = extractHBuilderXExecutablesFromProcessOutput(result.stdout, platform);
|
|
176
|
+
const paths = platform === "win32" ? node_path.default.win32 : node_path.default.posix;
|
|
112
177
|
const candidates = [];
|
|
113
178
|
for (const executable of executables) {
|
|
114
179
|
if (!await fileExists(executable)) continue;
|
|
115
|
-
const cli =
|
|
180
|
+
const cli = paths.join(paths.dirname(executable), platform === "win32" ? "cli.exe" : "cli");
|
|
116
181
|
if (await fileExists(cli)) candidates.push(cli);
|
|
117
182
|
}
|
|
118
183
|
return unique(candidates);
|
|
@@ -132,11 +197,6 @@ function selectHBuilderXCliCandidatesForChannel(items, channel) {
|
|
|
132
197
|
if (channel === "auto") return sortByChannel(items);
|
|
133
198
|
return items.filter((item) => inferHBuilderXChannel(item) === channel);
|
|
134
199
|
}
|
|
135
|
-
function resolveCandidateSource(candidate, env) {
|
|
136
|
-
if (candidate === env.HBUILDERX_CLI_PATH) return "env";
|
|
137
|
-
if (candidate === "/Applications/HBuilderX.app/Contents/MacOS/cli" || candidate === "/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli") return "default-path";
|
|
138
|
-
return "candidate";
|
|
139
|
-
}
|
|
140
200
|
async function firstExisting(items) {
|
|
141
201
|
for (const item of items) if (await fileExists(item)) return item;
|
|
142
202
|
}
|
|
@@ -144,21 +204,11 @@ async function resolveHBuilderXCliInfoFromOptions(options = {}) {
|
|
|
144
204
|
const env = options.env ?? node_process.default.env;
|
|
145
205
|
const channel = resolveHBuilderXChannel(options.channel ?? env.HBUILDERX_CHANNEL);
|
|
146
206
|
const running = await findRunningHBuilderXCliCandidates();
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
source: resolveCandidateSource(candidate, env),
|
|
153
|
-
channel: inferHBuilderXChannel(candidate)
|
|
154
|
-
};
|
|
155
|
-
throw new Error("未找到显式指定的 HBuilderX CLI candidate。");
|
|
156
|
-
}
|
|
157
|
-
if (env.HBUILDERX_CLI_PATH && await fileExists(env.HBUILDERX_CLI_PATH)) return {
|
|
158
|
-
path: env.HBUILDERX_CLI_PATH,
|
|
159
|
-
isRunning: running.some((item) => normalizeFile(item) === normalizeFile(env.HBUILDERX_CLI_PATH)),
|
|
160
|
-
source: "env",
|
|
161
|
-
channel: inferHBuilderXChannel(env.HBUILDERX_CLI_PATH)
|
|
207
|
+
const configured = await resolveConfiguredCli(options);
|
|
208
|
+
if (configured) return {
|
|
209
|
+
...configured,
|
|
210
|
+
isRunning: running.some((item) => normalizeFile(item) === normalizeFile(configured.path)),
|
|
211
|
+
channel: inferHBuilderXChannel(configured.path)
|
|
162
212
|
};
|
|
163
213
|
const runningCandidate = selectHBuilderXCliCandidatesForChannel(running, channel)[0];
|
|
164
214
|
if (runningCandidate) return {
|
|
@@ -223,8 +273,8 @@ function createLogBuffer(maxChunks = 160) {
|
|
|
223
273
|
}
|
|
224
274
|
function collectProcessOutput(child, maxChunks = 160) {
|
|
225
275
|
const buffer = createLogBuffer(maxChunks);
|
|
226
|
-
child.stdout?.on("data", (chunk) => buffer.push(chunk));
|
|
227
|
-
child.stderr?.on("data", (chunk) => buffer.push(chunk));
|
|
276
|
+
child.stdout?.setEncoding("utf8").on("data", (chunk) => buffer.push(chunk));
|
|
277
|
+
child.stderr?.setEncoding("utf8").on("data", (chunk) => buffer.push(chunk));
|
|
228
278
|
return buffer.logs;
|
|
229
279
|
}
|
|
230
280
|
function classifyHBuilderXOutput(output) {
|
|
@@ -363,10 +413,9 @@ function buildCommandError(prefix, options, exit, logs, timedOut = false) {
|
|
|
363
413
|
].filter(Boolean).join("\n"), result);
|
|
364
414
|
}
|
|
365
415
|
function spawnCommand(options) {
|
|
366
|
-
const child = (0,
|
|
416
|
+
const child = (0, cross_spawn.default)(options.command, options.args, {
|
|
367
417
|
cwd: options.cwd,
|
|
368
418
|
detached: options.detached ?? node_process.default.platform !== "win32",
|
|
369
|
-
shell: node_process.default.platform === "win32",
|
|
370
419
|
stdio: options.stdio === "inherit" ? "inherit" : [
|
|
371
420
|
"ignore",
|
|
372
421
|
"pipe",
|
|
@@ -379,6 +428,7 @@ function spawnCommand(options) {
|
|
|
379
428
|
}
|
|
380
429
|
});
|
|
381
430
|
const logs = options.stdio === "inherit" ? [] : collectProcessOutput(child);
|
|
431
|
+
child.on("error", (error) => logs.push(`${error.message}\n`));
|
|
382
432
|
let exit;
|
|
383
433
|
const closed = new Promise((resolve) => {
|
|
384
434
|
child.on("close", (code, signal) => {
|
|
@@ -452,11 +502,8 @@ function createProcessExitTracker(child) {
|
|
|
452
502
|
};
|
|
453
503
|
}
|
|
454
504
|
//#endregion
|
|
455
|
-
//#region src/hbuilderx/
|
|
505
|
+
//#region src/hbuilderx/hosts.ts
|
|
456
506
|
const ansiRE = new RegExp(`${String.fromCharCode(27)}\\[[\\d;]*m`, "g");
|
|
457
|
-
const defaultStartupTimeoutMs = 6e4;
|
|
458
|
-
const hostPollIntervalMs = 500;
|
|
459
|
-
const maxSessionLogChunks = 160;
|
|
460
507
|
function stripAnsi(value) {
|
|
461
508
|
return value.replace(ansiRE, "");
|
|
462
509
|
}
|
|
@@ -469,18 +516,6 @@ function parseHBuilderXVersion(output) {
|
|
|
469
516
|
function inferHBuilderXChannelFromVersion(version) {
|
|
470
517
|
return /(?:^|-)alpha(?:\.|$)/i.test(version) ? "alpha" : "stable";
|
|
471
518
|
}
|
|
472
|
-
function createHBuilderXNativeLaunchArgs(options) {
|
|
473
|
-
const args = [
|
|
474
|
-
"launch",
|
|
475
|
-
options.platform,
|
|
476
|
-
"--project",
|
|
477
|
-
node_path.default.resolve(options.cwd),
|
|
478
|
-
...options.args ?? []
|
|
479
|
-
];
|
|
480
|
-
if (options.compile != null && !args.includes("--compile")) args.push("--compile", String(options.compile));
|
|
481
|
-
if (options.runtimeLog != null && !args.includes("--runtime-log")) args.push("--runtime-log", String(options.runtimeLog));
|
|
482
|
-
return args;
|
|
483
|
-
}
|
|
484
519
|
function createRunnerError(cliPath, cwd, args, kind, message, hint, logs) {
|
|
485
520
|
const output = formatRecentLogs(logs);
|
|
486
521
|
return new HBuilderXCommandError([
|
|
@@ -506,55 +541,55 @@ function createRunnerError(cliPath, cwd, args, kind, message, hint, logs) {
|
|
|
506
541
|
}
|
|
507
542
|
});
|
|
508
543
|
}
|
|
509
|
-
function
|
|
510
|
-
const
|
|
511
|
-
|
|
512
|
-
...args,
|
|
513
|
-
"--host",
|
|
514
|
-
host
|
|
515
|
-
];
|
|
516
|
-
if (args[hostIndex + 1] !== host) throw new Error(`runner 已绑定 HBuilderX host=${host},不能改为 ${args[hostIndex + 1] ?? "空值"}。`);
|
|
517
|
-
return args;
|
|
518
|
-
}
|
|
519
|
-
function appendLogs(target, source) {
|
|
520
|
-
target.push(...source);
|
|
521
|
-
if (target.length > maxSessionLogChunks) target.splice(0, target.length - maxSessionLogChunks);
|
|
522
|
-
}
|
|
523
|
-
async function listCompatibleHosts(cliPath, cwd, env, expectedChannel, explicitHost, timeoutMs) {
|
|
524
|
-
const startedAt = Date.now();
|
|
544
|
+
async function connectHBuilderXHost(options) {
|
|
545
|
+
const { cliPath, cwd, env, expectedChannel, explicitHost } = options;
|
|
546
|
+
const deadline = Date.now() + options.timeoutMs;
|
|
525
547
|
const logs = [];
|
|
526
|
-
|
|
527
|
-
|
|
548
|
+
const timeoutError = (args) => new HBuilderXCommandError(`HBuilderX host 连接超时:${cliPath} ${args.join(" ")}\ncwd=${cwd}\nexit=null\nissue=timeout\n${formatRecentLogs(logs)}`, {
|
|
549
|
+
command: cliPath,
|
|
550
|
+
args,
|
|
551
|
+
cwd,
|
|
552
|
+
exit: {
|
|
553
|
+
code: null,
|
|
554
|
+
signal: null
|
|
555
|
+
},
|
|
556
|
+
logs,
|
|
557
|
+
output: formatRecentLogs(logs),
|
|
558
|
+
issue: createTimeoutIssue()
|
|
559
|
+
});
|
|
560
|
+
const command = async (args, maxTimeoutMs = 1e4) => {
|
|
561
|
+
const remaining = deadline - Date.now();
|
|
562
|
+
if (remaining <= 0) throw timeoutError(args);
|
|
563
|
+
const result = await runCommand({
|
|
528
564
|
command: cliPath,
|
|
529
|
-
args
|
|
565
|
+
args,
|
|
530
566
|
cwd,
|
|
531
567
|
env,
|
|
532
|
-
timeoutMs: Math.min(
|
|
568
|
+
timeoutMs: Math.min(remaining, maxTimeoutMs),
|
|
533
569
|
allowFailure: true
|
|
534
570
|
});
|
|
535
|
-
|
|
536
|
-
|
|
571
|
+
logs.push(...result.logs);
|
|
572
|
+
if (logs.length > 160) logs.splice(0, logs.length - 160);
|
|
573
|
+
return result;
|
|
574
|
+
};
|
|
575
|
+
const assertCommand = (result) => {
|
|
576
|
+
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);
|
|
577
|
+
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);
|
|
578
|
+
};
|
|
579
|
+
const probe = async () => {
|
|
580
|
+
const listed = await command(["listhost"]);
|
|
581
|
+
if (!(listed.issue.kind !== "timeout" && /HBuilderX is not detected running\. Please execute cli open/i.test(stripAnsi(listed.output)))) assertCommand(listed);
|
|
582
|
+
const hosts = parseHBuilderXHosts(listed.output);
|
|
537
583
|
const compatible = [];
|
|
538
|
-
for (const host of hosts) {
|
|
539
|
-
const
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
env,
|
|
548
|
-
timeoutMs: Math.min(timeoutMs, 1e4),
|
|
549
|
-
allowFailure: true
|
|
550
|
-
});
|
|
551
|
-
appendLogs(logs, versionResult.logs);
|
|
552
|
-
if (versionResult.issue.kind === "cli-instance-mismatch") return {
|
|
553
|
-
compatible: [],
|
|
554
|
-
logs
|
|
555
|
-
};
|
|
556
|
-
const version = versionResult.exit.code === 0 ? parseHBuilderXVersion(versionResult.output) : void 0;
|
|
557
|
-
if (!version) continue;
|
|
584
|
+
for (const host of explicitHost ? [explicitHost] : hosts) {
|
|
585
|
+
const result = await command([
|
|
586
|
+
"version",
|
|
587
|
+
"--host",
|
|
588
|
+
host
|
|
589
|
+
]);
|
|
590
|
+
assertCommand(result);
|
|
591
|
+
const version = parseHBuilderXVersion(result.output);
|
|
592
|
+
if (!version) throw createRunnerError(cliPath, cwd, result.args, "cli-instance-mismatch", `无法验证 HBuilderX host=${host} 的版本`, "请检查目标实例是否就绪。", logs);
|
|
558
593
|
const channel = inferHBuilderXChannelFromVersion(version);
|
|
559
594
|
if (expectedChannel === "unknown" || channel === expectedChannel) compatible.push({
|
|
560
595
|
host,
|
|
@@ -562,16 +597,44 @@ async function listCompatibleHosts(cliPath, cwd, env, expectedChannel, explicitH
|
|
|
562
597
|
channel
|
|
563
598
|
});
|
|
564
599
|
}
|
|
565
|
-
if (compatible.length >
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
};
|
|
569
|
-
await wait(hostPollIntervalMs);
|
|
570
|
-
} while (Date.now() - startedAt < timeoutMs);
|
|
571
|
-
return {
|
|
572
|
-
compatible: [],
|
|
573
|
-
logs
|
|
600
|
+
if (compatible.length > 1) throw createRunnerError(cliPath, cwd, ["listhost"], "cli-host-ambiguous", `检测到多个匹配的 HBuilderX host:${compatible.map((item) => item.host).join(", ")}`, "请设置 HBUILDERX_HOST,或显式传入 host。", logs);
|
|
601
|
+
if (!compatible.length && (hosts.length || explicitHost)) throw createRunnerError(cliPath, cwd, ["listhost"], "cli-instance-mismatch", `未找到与 ${expectedChannel} CLI 匹配的 HBuilderX host`, "请检查 HBUILDERX_CLI_PATH、HBUILDERX_CHANNEL 和 HBUILDERX_HOST。", logs);
|
|
602
|
+
return compatible[0];
|
|
574
603
|
};
|
|
604
|
+
const existing = await probe();
|
|
605
|
+
if (existing) return existing;
|
|
606
|
+
assertCommand(await command(["open"], options.timeoutMs));
|
|
607
|
+
do {
|
|
608
|
+
const selected = await probe();
|
|
609
|
+
if (selected) return selected;
|
|
610
|
+
await wait(Math.min(500, Math.max(0, deadline - Date.now())));
|
|
611
|
+
} while (Date.now() < deadline);
|
|
612
|
+
throw timeoutError(["listhost"]);
|
|
613
|
+
}
|
|
614
|
+
//#endregion
|
|
615
|
+
//#region src/hbuilderx/runner.ts
|
|
616
|
+
const defaultStartupTimeoutMs = 6e4;
|
|
617
|
+
function createHBuilderXNativeLaunchArgs(options) {
|
|
618
|
+
const args = [
|
|
619
|
+
"launch",
|
|
620
|
+
options.platform,
|
|
621
|
+
"--project",
|
|
622
|
+
node_path.default.resolve(options.cwd),
|
|
623
|
+
...options.args ?? []
|
|
624
|
+
];
|
|
625
|
+
if (options.compile != null && !args.includes("--compile")) args.push("--compile", String(options.compile));
|
|
626
|
+
if (options.runtimeLog != null && !args.includes("--runtime-log")) args.push("--runtime-log", String(options.runtimeLog));
|
|
627
|
+
return args;
|
|
628
|
+
}
|
|
629
|
+
function bindHost(args, host) {
|
|
630
|
+
const hostIndex = args.indexOf("--host");
|
|
631
|
+
if (hostIndex < 0) return [
|
|
632
|
+
...args,
|
|
633
|
+
"--host",
|
|
634
|
+
host
|
|
635
|
+
];
|
|
636
|
+
if (args[hostIndex + 1] !== host) throw new Error(`runner 已绑定 HBuilderX host=${host},不能改为 ${args[hostIndex + 1] ?? "空值"}。`);
|
|
637
|
+
return args;
|
|
575
638
|
}
|
|
576
639
|
async function createHBuilderXRunner(options = {}) {
|
|
577
640
|
const env = {
|
|
@@ -580,35 +643,32 @@ async function createHBuilderXRunner(options = {}) {
|
|
|
580
643
|
};
|
|
581
644
|
const cwd = options.cwd ?? node_process.default.cwd();
|
|
582
645
|
const timeoutMs = options.timeoutMs ?? defaultStartupTimeoutMs;
|
|
583
|
-
const
|
|
646
|
+
const resolveOptions = {
|
|
584
647
|
candidates: options.hbuilderxCliPath ? [options.hbuilderxCliPath] : options.candidates,
|
|
585
648
|
channel: options.channel,
|
|
586
649
|
env,
|
|
587
650
|
host: options.host
|
|
588
|
-
}
|
|
651
|
+
};
|
|
652
|
+
const configured = await resolveConfiguredCli(resolveOptions);
|
|
653
|
+
const resolution = configured ? {
|
|
654
|
+
...configured,
|
|
655
|
+
channel: inferHBuilderXChannel(configured.path)
|
|
656
|
+
} : await resolveHBuilderXCliInfoFromOptions(resolveOptions);
|
|
589
657
|
const cliEnv = {
|
|
590
658
|
...env,
|
|
591
659
|
HBUILDERX_CLI_PATH: resolution.path
|
|
592
660
|
};
|
|
593
|
-
if (!resolution.isRunning) {
|
|
594
|
-
const opened = await runCommand({
|
|
595
|
-
command: resolution.path,
|
|
596
|
-
args: ["open"],
|
|
597
|
-
cwd,
|
|
598
|
-
env: cliEnv,
|
|
599
|
-
timeoutMs,
|
|
600
|
-
allowFailure: true
|
|
601
|
-
});
|
|
602
|
-
const openIssue = classifyHBuilderXOutput(opened.output);
|
|
603
|
-
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);
|
|
604
|
-
}
|
|
605
661
|
const requestedChannel = resolveHBuilderXChannel(options.channel ?? env.HBUILDERX_CHANNEL);
|
|
606
662
|
const expectedChannel = resolution.channel !== "unknown" ? resolution.channel : requestedChannel === "auto" ? inferHBuilderXChannel(resolution.path) : requestedChannel;
|
|
607
663
|
const explicitHost = options.host ?? env.HBUILDERX_HOST;
|
|
608
|
-
const
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
664
|
+
const selected = await connectHBuilderXHost({
|
|
665
|
+
cliPath: resolution.path,
|
|
666
|
+
cwd,
|
|
667
|
+
env: cliEnv,
|
|
668
|
+
expectedChannel,
|
|
669
|
+
explicitHost,
|
|
670
|
+
timeoutMs
|
|
671
|
+
});
|
|
612
672
|
const boundResolution = {
|
|
613
673
|
...resolution,
|
|
614
674
|
isRunning: true,
|
|
@@ -694,7 +754,9 @@ async function resolveHBuilderXCliInfo(candidatesOrOptions, env = node_process.d
|
|
|
694
754
|
} : candidatesOrOptions);
|
|
695
755
|
}
|
|
696
756
|
async function resolveHBuilderXCli(candidatesOrOptions) {
|
|
697
|
-
|
|
757
|
+
const options = Array.isArray(candidatesOrOptions) ? { candidates: candidatesOrOptions } : candidatesOrOptions ?? {};
|
|
758
|
+
resolveHBuilderXChannel(options.channel ?? (options.env ?? node_process.default.env).HBUILDERX_CHANNEL);
|
|
759
|
+
return (await resolveConfiguredCli(options))?.path ?? (await resolveHBuilderXCliInfoFromOptions(options)).path;
|
|
698
760
|
}
|
|
699
761
|
function createHBuilderXEnv(options = {}) {
|
|
700
762
|
return {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
import { ChildProcess } from "node:child_process";
|
|
2
2
|
import { Buffer } from "node:buffer";
|
|
3
3
|
//#region src/fs.d.ts
|
|
4
|
-
declare function fileExists(file: string): Promise<boolean>;
|
|
5
|
-
declare function readUtf8(file: string): Promise<string>;
|
|
6
|
-
declare function wait(ms: number): Promise<unknown>;
|
|
4
|
+
export declare function fileExists(file: string): Promise<boolean>;
|
|
5
|
+
export declare function readUtf8(file: string): Promise<string>;
|
|
6
|
+
export declare function wait(ms: number): Promise<unknown>;
|
|
7
7
|
//#endregion
|
|
8
8
|
//#region src/types.d.ts
|
|
9
|
-
type HBuilderXMiniProgramPlatform = 'mp-weixin' | 'mp-alipay' | 'mp-baidu' | 'mp-toutiao';
|
|
10
|
-
type HBuilderXAppPlatform = 'app-android' | 'app-ios' | 'app-harmony';
|
|
11
|
-
type HBuilderXPlatform = HBuilderXMiniProgramPlatform | HBuilderXAppPlatform;
|
|
12
|
-
type HBuilderXChannel = 'auto' | 'stable' | 'alpha';
|
|
13
|
-
type HBuilderXResolvedChannel = Exclude<HBuilderXChannel, 'auto'> | 'unknown';
|
|
14
|
-
type HBuilderXIssueKind = 'cli-not-found' | 'cli-instance-mismatch' | 'cli-host-ambiguous' | 'project-not-uni-app' | 'project-type-unsupported' | 'config-load-failed' | 'android-toolchain-missing' | 'ios-toolchain-missing' | 'harmony-toolchain-missing' | 'harmony-build-failed' | 'timeout' | 'process-exit' | 'unknown';
|
|
15
|
-
interface HBuilderXIssue {
|
|
9
|
+
export type HBuilderXMiniProgramPlatform = 'mp-weixin' | 'mp-alipay' | 'mp-baidu' | 'mp-toutiao';
|
|
10
|
+
export type HBuilderXAppPlatform = 'app-android' | 'app-ios' | 'app-harmony';
|
|
11
|
+
export type HBuilderXPlatform = HBuilderXMiniProgramPlatform | HBuilderXAppPlatform;
|
|
12
|
+
export type HBuilderXChannel = 'auto' | 'stable' | 'alpha';
|
|
13
|
+
export type HBuilderXResolvedChannel = Exclude<HBuilderXChannel, 'auto'> | 'unknown';
|
|
14
|
+
export type HBuilderXIssueKind = 'cli-not-found' | 'cli-instance-mismatch' | 'cli-host-ambiguous' | 'project-not-uni-app' | 'project-type-unsupported' | 'config-load-failed' | 'android-toolchain-missing' | 'ios-toolchain-missing' | 'harmony-toolchain-missing' | 'harmony-build-failed' | 'timeout' | 'process-exit' | 'unknown';
|
|
15
|
+
export interface HBuilderXIssue {
|
|
16
16
|
kind: HBuilderXIssueKind;
|
|
17
17
|
message: string;
|
|
18
18
|
hint?: string;
|
|
19
19
|
}
|
|
20
|
-
interface CommandExit {
|
|
20
|
+
export interface CommandExit {
|
|
21
21
|
code: number | null;
|
|
22
22
|
signal: NodeJS.Signals | null;
|
|
23
23
|
}
|
|
24
|
-
interface HBuilderXCommandOptions {
|
|
24
|
+
export interface HBuilderXCommandOptions {
|
|
25
25
|
command: string;
|
|
26
26
|
args: string[];
|
|
27
27
|
cwd: string;
|
|
@@ -31,7 +31,7 @@ interface HBuilderXCommandOptions {
|
|
|
31
31
|
detached?: boolean;
|
|
32
32
|
stdio?: 'pipe' | 'inherit';
|
|
33
33
|
}
|
|
34
|
-
interface HBuilderXCommandResult {
|
|
34
|
+
export interface HBuilderXCommandResult {
|
|
35
35
|
command: string;
|
|
36
36
|
args: string[];
|
|
37
37
|
cwd: string;
|
|
@@ -40,7 +40,7 @@ interface HBuilderXCommandResult {
|
|
|
40
40
|
output: string;
|
|
41
41
|
issue: HBuilderXIssue;
|
|
42
42
|
}
|
|
43
|
-
interface SpawnedHBuilderXCommand {
|
|
43
|
+
export interface SpawnedHBuilderXCommand {
|
|
44
44
|
child: ChildProcess;
|
|
45
45
|
logs: string[];
|
|
46
46
|
command: string;
|
|
@@ -50,7 +50,7 @@ interface SpawnedHBuilderXCommand {
|
|
|
50
50
|
closed: Promise<CommandExit>;
|
|
51
51
|
ensureRunning: () => void;
|
|
52
52
|
}
|
|
53
|
-
interface HBuilderXProjectOptions {
|
|
53
|
+
export interface HBuilderXProjectOptions {
|
|
54
54
|
cwd: string;
|
|
55
55
|
hbuilderxCliPath?: string;
|
|
56
56
|
channel?: HBuilderXChannel;
|
|
@@ -59,31 +59,31 @@ interface HBuilderXProjectOptions {
|
|
|
59
59
|
timeoutMs?: number;
|
|
60
60
|
allowFailure?: boolean;
|
|
61
61
|
}
|
|
62
|
-
interface HBuilderXLaunchOptions extends HBuilderXProjectOptions {
|
|
62
|
+
export interface HBuilderXLaunchOptions extends HBuilderXProjectOptions {
|
|
63
63
|
platform: HBuilderXPlatform;
|
|
64
64
|
args?: string[];
|
|
65
65
|
compile?: boolean;
|
|
66
66
|
runtimeLog?: boolean;
|
|
67
67
|
}
|
|
68
|
-
interface ToolResult {
|
|
68
|
+
export interface ToolResult {
|
|
69
69
|
ok: boolean;
|
|
70
70
|
output: string;
|
|
71
71
|
status: number | null;
|
|
72
72
|
signal: NodeJS.Signals | null;
|
|
73
73
|
}
|
|
74
|
-
interface AndroidToolchain {
|
|
74
|
+
export interface AndroidToolchain {
|
|
75
75
|
command: string;
|
|
76
76
|
output: string;
|
|
77
77
|
env: Record<string, string> | undefined;
|
|
78
78
|
}
|
|
79
|
-
type HBuilderXCliResolutionSource = 'running-process' | 'env' | 'default-path' | 'candidate';
|
|
80
|
-
interface HBuilderXCliResolveOptions {
|
|
79
|
+
export type HBuilderXCliResolutionSource = 'running-process' | 'env' | 'default-path' | 'candidate';
|
|
80
|
+
export interface HBuilderXCliResolveOptions {
|
|
81
81
|
candidates?: string[];
|
|
82
82
|
channel?: HBuilderXChannel;
|
|
83
83
|
env?: NodeJS.ProcessEnv;
|
|
84
84
|
host?: string;
|
|
85
85
|
}
|
|
86
|
-
interface HBuilderXCliResolution {
|
|
86
|
+
export interface HBuilderXCliResolution {
|
|
87
87
|
path: string;
|
|
88
88
|
isRunning: boolean;
|
|
89
89
|
source: HBuilderXCliResolutionSource;
|
|
@@ -91,7 +91,7 @@ interface HBuilderXCliResolution {
|
|
|
91
91
|
host?: string;
|
|
92
92
|
version?: string;
|
|
93
93
|
}
|
|
94
|
-
interface HBuilderXNativeCommandOptions {
|
|
94
|
+
export interface HBuilderXNativeCommandOptions {
|
|
95
95
|
args: string[];
|
|
96
96
|
cwd?: string;
|
|
97
97
|
timeoutMs?: number;
|
|
@@ -100,12 +100,12 @@ interface HBuilderXNativeCommandOptions {
|
|
|
100
100
|
detached?: boolean;
|
|
101
101
|
stdio?: 'pipe' | 'inherit';
|
|
102
102
|
}
|
|
103
|
-
interface HBuilderXRunnerOptions extends HBuilderXCliResolveOptions {
|
|
103
|
+
export interface HBuilderXRunnerOptions extends HBuilderXCliResolveOptions {
|
|
104
104
|
cwd?: string;
|
|
105
105
|
hbuilderxCliPath?: string;
|
|
106
106
|
timeoutMs?: number;
|
|
107
107
|
}
|
|
108
|
-
interface HBuilderXRunner {
|
|
108
|
+
export interface HBuilderXRunner {
|
|
109
109
|
resolution: HBuilderXCliResolution;
|
|
110
110
|
run: (options: HBuilderXNativeCommandOptions) => Promise<HBuilderXCommandResult>;
|
|
111
111
|
spawn: (options: HBuilderXNativeCommandOptions) => SpawnedHBuilderXCommand;
|
|
@@ -116,100 +116,103 @@ interface HBuilderXRunner {
|
|
|
116
116
|
startLaunch: (options: HBuilderXLaunchOptions) => SpawnedHBuilderXCommand;
|
|
117
117
|
}
|
|
118
118
|
//#endregion
|
|
119
|
+
//#region src/hbuilderx/configured-cli.d.ts
|
|
120
|
+
export declare const macOSStableCli = "/Applications/HBuilderX.app/Contents/MacOS/cli";
|
|
121
|
+
export declare const macOSAlphaCli = "/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli";
|
|
122
|
+
//#endregion
|
|
119
123
|
//#region src/hbuilderx/discovery.d.ts
|
|
120
|
-
declare
|
|
121
|
-
declare
|
|
122
|
-
declare function
|
|
123
|
-
declare function
|
|
124
|
-
declare function
|
|
125
|
-
declare function
|
|
126
|
-
declare function
|
|
127
|
-
declare function
|
|
128
|
-
declare function
|
|
129
|
-
declare function
|
|
130
|
-
declare function
|
|
131
|
-
declare function
|
|
132
|
-
declare function
|
|
133
|
-
|
|
134
|
-
|
|
124
|
+
export declare function resolveHBuilderXChannel(channel?: string): HBuilderXChannel;
|
|
125
|
+
export declare function inferHBuilderXChannel(file: string): HBuilderXResolvedChannel;
|
|
126
|
+
export declare function getDefaultHBuilderXCliCandidates(env?: NodeJS.ProcessEnv, channel?: HBuilderXChannel, platform?: NodeJS.Platform): string[];
|
|
127
|
+
export declare function extractHBuilderXMacOSExecutables(output: string): string[];
|
|
128
|
+
export declare function extractHBuilderXWindowsExecutables(output: string): string[];
|
|
129
|
+
export declare function extractHBuilderXExecutablesFromProcessOutput(output: string, platform?: NodeJS.Platform): string[];
|
|
130
|
+
export declare function extractHBuilderXMacOSExecutable(line: string): string | undefined;
|
|
131
|
+
export declare function extractHBuilderXWindowsExecutable(line: string): string | undefined;
|
|
132
|
+
export declare function extractHBuilderXExecutableFromProcessOutput(output: string, platform?: NodeJS.Platform): string | undefined;
|
|
133
|
+
export declare function findRunningHBuilderXCliCandidates(platform?: NodeJS.Platform): Promise<string[]>;
|
|
134
|
+
export declare function findRunningHBuilderXCli(platform?: NodeJS.Platform): Promise<string | undefined>;
|
|
135
|
+
export declare function selectHBuilderXCliCandidatesForChannel(items: string[], channel: HBuilderXChannel): string[];
|
|
136
|
+
export declare function resolveHBuilderXCliInfoFromOptions(options?: HBuilderXCliResolveOptions): Promise<HBuilderXCliResolution>;
|
|
137
|
+
//#endregion
|
|
138
|
+
//#region src/process.d.ts
|
|
139
|
+
export declare class HBuilderXCommandError extends Error {
|
|
140
|
+
readonly result: HBuilderXCommandResult;
|
|
141
|
+
constructor(message: string, result: HBuilderXCommandResult);
|
|
142
|
+
}
|
|
143
|
+
export declare function killProcessTree(child: ChildProcess, signal?: NodeJS.Signals): void;
|
|
144
|
+
export declare function spawnCommand(options: HBuilderXCommandOptions): SpawnedHBuilderXCommand;
|
|
145
|
+
export declare function runCommand(options: HBuilderXCommandOptions): Promise<HBuilderXCommandResult>;
|
|
146
|
+
export declare function createProcessExitTracker(child: ChildProcess): {
|
|
147
|
+
closed: Promise<CommandExit>;
|
|
148
|
+
ensureRunning(logs: string[]): void;
|
|
149
|
+
};
|
|
150
|
+
//#endregion
|
|
151
|
+
//#region src/hbuilderx/hosts.d.ts
|
|
152
|
+
export declare function stripAnsi(value: string): string;
|
|
153
|
+
export declare function parseHBuilderXHosts(output: string): string[];
|
|
154
|
+
export declare function parseHBuilderXVersion(output: string): string | undefined;
|
|
155
|
+
export declare function inferHBuilderXChannelFromVersion(version: string): HBuilderXResolvedChannel;
|
|
135
156
|
//#endregion
|
|
136
157
|
//#region src/hbuilderx/runner.d.ts
|
|
137
|
-
declare function
|
|
138
|
-
declare function
|
|
139
|
-
declare function parseHBuilderXVersion(output: string): string | undefined;
|
|
140
|
-
declare function inferHBuilderXChannelFromVersion(version: string): HBuilderXResolvedChannel;
|
|
141
|
-
declare function createHBuilderXNativeLaunchArgs(options: HBuilderXLaunchOptions): string[];
|
|
142
|
-
declare function createHBuilderXRunner(options?: HBuilderXRunnerOptions): Promise<HBuilderXRunner>;
|
|
158
|
+
export declare function createHBuilderXNativeLaunchArgs(options: HBuilderXLaunchOptions): string[];
|
|
159
|
+
export declare function createHBuilderXRunner(options?: HBuilderXRunnerOptions): Promise<HBuilderXRunner>;
|
|
143
160
|
//#endregion
|
|
144
161
|
//#region src/hbuilderx.d.ts
|
|
145
|
-
declare function resolveHBuilderXCliInfo(options?: HBuilderXCliResolveOptions): ReturnType<typeof resolveHBuilderXCliInfoFromOptions>;
|
|
146
|
-
declare function resolveHBuilderXCliInfo(candidates?: string[], env?: NodeJS.ProcessEnv): ReturnType<typeof resolveHBuilderXCliInfoFromOptions>;
|
|
147
|
-
declare function resolveHBuilderXCli(options?: HBuilderXCliResolveOptions): Promise<string>;
|
|
148
|
-
declare function resolveHBuilderXCli(candidates?: string[]): Promise<string>;
|
|
149
|
-
declare function createHBuilderXEnv(options?: {
|
|
162
|
+
export declare function resolveHBuilderXCliInfo(options?: HBuilderXCliResolveOptions): ReturnType<typeof resolveHBuilderXCliInfoFromOptions>;
|
|
163
|
+
export declare function resolveHBuilderXCliInfo(candidates?: string[], env?: NodeJS.ProcessEnv): ReturnType<typeof resolveHBuilderXCliInfoFromOptions>;
|
|
164
|
+
export declare function resolveHBuilderXCli(options?: HBuilderXCliResolveOptions): Promise<string>;
|
|
165
|
+
export declare function resolveHBuilderXCli(candidates?: string[]): Promise<string>;
|
|
166
|
+
export declare function createHBuilderXEnv(options?: {
|
|
150
167
|
hbuilderxCliPath?: string;
|
|
151
168
|
env?: Record<string, string | undefined>;
|
|
152
169
|
}): {
|
|
153
170
|
HBUILDERX_CLI_PATH: string | undefined;
|
|
154
171
|
NODE_OPTIONS: string;
|
|
155
172
|
};
|
|
156
|
-
declare function hbuilderxPnpmArgs(args: string[]): string[];
|
|
157
|
-
declare function runPnpmCommand(options: HBuilderXProjectOptions & {
|
|
173
|
+
export declare function hbuilderxPnpmArgs(args: string[]): string[];
|
|
174
|
+
export declare function runPnpmCommand(options: HBuilderXProjectOptions & {
|
|
158
175
|
args: string[];
|
|
159
176
|
}): Promise<HBuilderXCommandResult>;
|
|
160
|
-
declare function spawnPnpmCommand(options: HBuilderXProjectOptions & {
|
|
177
|
+
export declare function spawnPnpmCommand(options: HBuilderXProjectOptions & {
|
|
161
178
|
args: string[];
|
|
162
179
|
}): SpawnedHBuilderXCommand;
|
|
163
|
-
declare function closeProject(options: HBuilderXProjectOptions): Promise<HBuilderXCommandResult>;
|
|
164
|
-
declare function openProject(options: HBuilderXProjectOptions): Promise<HBuilderXCommandResult>;
|
|
165
|
-
declare function prepareProject(options: HBuilderXProjectOptions): Promise<HBuilderXCommandResult>;
|
|
166
|
-
declare function createLaunchArgs(options: HBuilderXLaunchOptions): string[];
|
|
167
|
-
declare function launchProject(options: HBuilderXLaunchOptions): Promise<HBuilderXCommandResult>;
|
|
180
|
+
export declare function closeProject(options: HBuilderXProjectOptions): Promise<HBuilderXCommandResult>;
|
|
181
|
+
export declare function openProject(options: HBuilderXProjectOptions): Promise<HBuilderXCommandResult>;
|
|
182
|
+
export declare function prepareProject(options: HBuilderXProjectOptions): Promise<HBuilderXCommandResult>;
|
|
183
|
+
export declare function createLaunchArgs(options: HBuilderXLaunchOptions): string[];
|
|
184
|
+
export declare function launchProject(options: HBuilderXLaunchOptions): Promise<HBuilderXCommandResult>;
|
|
168
185
|
/**
|
|
169
186
|
* @deprecated 需要固定 stable/alpha 与 host 时,请使用 `createHBuilderXRunner().startLaunch()`。
|
|
170
187
|
*/
|
|
171
|
-
declare function startLaunch(options: HBuilderXLaunchOptions): SpawnedHBuilderXCommand;
|
|
188
|
+
export declare function startLaunch(options: HBuilderXLaunchOptions): SpawnedHBuilderXCommand;
|
|
172
189
|
//#endregion
|
|
173
190
|
//#region src/logs.d.ts
|
|
174
|
-
declare function createLogBuffer(maxChunks?: number): {
|
|
191
|
+
export declare function createLogBuffer(maxChunks?: number): {
|
|
175
192
|
logs: string[];
|
|
176
193
|
push(chunk: Buffer | string): void;
|
|
177
194
|
text(): string;
|
|
178
195
|
};
|
|
179
|
-
declare function collectProcessOutput(child: ChildProcess, maxChunks?: number): string[];
|
|
180
|
-
declare function classifyHBuilderXOutput(output: string): HBuilderXIssue;
|
|
181
|
-
declare function createTimeoutIssue(): HBuilderXIssue;
|
|
182
|
-
declare function formatRecentLogs(logs: string[], maxLength?: number): string;
|
|
183
|
-
//#endregion
|
|
184
|
-
//#region src/process.d.ts
|
|
185
|
-
declare class HBuilderXCommandError extends Error {
|
|
186
|
-
readonly result: HBuilderXCommandResult;
|
|
187
|
-
constructor(message: string, result: HBuilderXCommandResult);
|
|
188
|
-
}
|
|
189
|
-
declare function killProcessTree(child: ChildProcess, signal?: NodeJS.Signals): void;
|
|
190
|
-
declare function spawnCommand(options: HBuilderXCommandOptions): SpawnedHBuilderXCommand;
|
|
191
|
-
declare function runCommand(options: HBuilderXCommandOptions): Promise<HBuilderXCommandResult>;
|
|
192
|
-
declare function createProcessExitTracker(child: ChildProcess): {
|
|
193
|
-
closed: Promise<CommandExit>;
|
|
194
|
-
ensureRunning(logs: string[]): void;
|
|
195
|
-
};
|
|
196
|
+
export declare function collectProcessOutput(child: ChildProcess, maxChunks?: number): string[];
|
|
197
|
+
export declare function classifyHBuilderXOutput(output: string): HBuilderXIssue;
|
|
198
|
+
export declare function createTimeoutIssue(): HBuilderXIssue;
|
|
199
|
+
export declare function formatRecentLogs(logs: string[], maxLength?: number): string;
|
|
196
200
|
//#endregion
|
|
197
201
|
//#region src/toolchains.d.ts
|
|
198
|
-
declare function runTool(command: string, args: string[]): ToolResult;
|
|
199
|
-
declare function resolveAdbCommand(env?: NodeJS.ProcessEnv): AndroidToolchain;
|
|
200
|
-
declare function assertAndroidToolchain(env?: NodeJS.ProcessEnv): Record<string, string> | undefined;
|
|
201
|
-
declare function assertIosSimulatorToolchain(env?: NodeJS.ProcessEnv): void;
|
|
202
|
-
interface IosSimulatorDevice {
|
|
202
|
+
export declare function runTool(command: string, args: string[]): ToolResult;
|
|
203
|
+
export declare function resolveAdbCommand(env?: NodeJS.ProcessEnv): AndroidToolchain;
|
|
204
|
+
export declare function assertAndroidToolchain(env?: NodeJS.ProcessEnv): Record<string, string> | undefined;
|
|
205
|
+
export declare function assertIosSimulatorToolchain(env?: NodeJS.ProcessEnv): void;
|
|
206
|
+
export interface IosSimulatorDevice {
|
|
203
207
|
lastBootedAt?: string;
|
|
204
208
|
name: string;
|
|
205
209
|
state: string;
|
|
206
210
|
udid: string;
|
|
207
211
|
}
|
|
208
|
-
declare function parseIosSimulatorDevices(output: string): IosSimulatorDevice[];
|
|
209
|
-
declare function selectPreferredIosSimulatorDevice(devices: IosSimulatorDevice[]): IosSimulatorDevice | undefined;
|
|
210
|
-
declare function resolveIosSimulatorDeviceId(env?: NodeJS.ProcessEnv): string;
|
|
211
|
-
declare function resolveHdcCommand(env?: NodeJS.ProcessEnv): string;
|
|
212
|
-
declare function parseHdcTargets(output: string): string[];
|
|
213
|
-
declare function assertHarmonyToolchain(env?: NodeJS.ProcessEnv, deviceId?: string): string;
|
|
214
|
-
//#endregion
|
|
215
|
-
export { AndroidToolchain, CommandExit, HBuilderXAppPlatform, HBuilderXChannel, HBuilderXCliResolution, HBuilderXCliResolutionSource, HBuilderXCliResolveOptions, HBuilderXCommandError, HBuilderXCommandOptions, HBuilderXCommandResult, HBuilderXIssue, HBuilderXIssueKind, HBuilderXLaunchOptions, HBuilderXMiniProgramPlatform, HBuilderXNativeCommandOptions, HBuilderXPlatform, HBuilderXProjectOptions, HBuilderXResolvedChannel, HBuilderXRunner, HBuilderXRunnerOptions, IosSimulatorDevice, SpawnedHBuilderXCommand, ToolResult, 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 };
|
|
212
|
+
export declare function parseIosSimulatorDevices(output: string): IosSimulatorDevice[];
|
|
213
|
+
export declare function selectPreferredIosSimulatorDevice(devices: IosSimulatorDevice[]): IosSimulatorDevice | undefined;
|
|
214
|
+
export declare function resolveIosSimulatorDeviceId(env?: NodeJS.ProcessEnv): string;
|
|
215
|
+
export declare function resolveHdcCommand(env?: NodeJS.ProcessEnv): string;
|
|
216
|
+
export declare function parseHdcTargets(output: string): string[];
|
|
217
|
+
export declare function assertHarmonyToolchain(env?: NodeJS.ProcessEnv, deviceId?: string): string;
|
|
218
|
+
//#endregion
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import fs from "node:fs/promises";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import process from "node:process";
|
|
4
|
-
import {
|
|
4
|
+
import { Buffer } from "node:buffer";
|
|
5
|
+
import { spawnSync } from "node:child_process";
|
|
6
|
+
import spawn from "cross-spawn";
|
|
5
7
|
//#region src/fs.ts
|
|
6
8
|
async function fileExists(file) {
|
|
7
9
|
try {
|
|
@@ -18,9 +20,34 @@ function wait(ms) {
|
|
|
18
20
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
19
21
|
}
|
|
20
22
|
//#endregion
|
|
21
|
-
//#region src/hbuilderx/
|
|
23
|
+
//#region src/hbuilderx/configured-cli.ts
|
|
22
24
|
const macOSStableCli = "/Applications/HBuilderX.app/Contents/MacOS/cli";
|
|
23
25
|
const macOSAlphaCli = "/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli";
|
|
26
|
+
function resolveCandidateSource(candidate, env) {
|
|
27
|
+
if (candidate === env.HBUILDERX_CLI_PATH) return "env";
|
|
28
|
+
if (candidate === "/Applications/HBuilderX.app/Contents/MacOS/cli" || candidate === "/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli") return "default-path";
|
|
29
|
+
return "candidate";
|
|
30
|
+
}
|
|
31
|
+
async function firstExisting$1(items) {
|
|
32
|
+
for (const item of items) if (await fileExists(item)) return item;
|
|
33
|
+
}
|
|
34
|
+
async function resolveConfiguredCli(options) {
|
|
35
|
+
const env = options.env ?? process.env;
|
|
36
|
+
if (options.candidates) {
|
|
37
|
+
const candidate = await firstExisting$1(options.candidates);
|
|
38
|
+
if (!candidate) throw new Error("未找到显式指定的 HBuilderX CLI candidate。");
|
|
39
|
+
return {
|
|
40
|
+
path: candidate,
|
|
41
|
+
source: resolveCandidateSource(candidate, env)
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
if (env.HBUILDERX_CLI_PATH && await fileExists(env.HBUILDERX_CLI_PATH)) return {
|
|
45
|
+
path: env.HBUILDERX_CLI_PATH,
|
|
46
|
+
source: "env"
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/hbuilderx/discovery.ts
|
|
24
51
|
function unique(items) {
|
|
25
52
|
return [...new Set(items)];
|
|
26
53
|
}
|
|
@@ -66,27 +93,64 @@ function extractHBuilderXExecutableFromProcessOutput(output, platform = process.
|
|
|
66
93
|
return extractHBuilderXExecutablesFromProcessOutput(output, platform)[0];
|
|
67
94
|
}
|
|
68
95
|
async function findRunningHBuilderXCliCandidates(platform = process.platform) {
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
96
|
+
const command = platform === "win32" ? "powershell.exe" : "ps";
|
|
97
|
+
const args = platform === "win32" ? [
|
|
98
|
+
"-NoLogo",
|
|
99
|
+
"-NoProfile",
|
|
100
|
+
"-NonInteractive",
|
|
101
|
+
"-Command",
|
|
102
|
+
[
|
|
103
|
+
"$ErrorActionPreference = 'Stop'",
|
|
104
|
+
"[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false)",
|
|
105
|
+
`$PSModuleAutoLoadingPreference = 'None'`,
|
|
106
|
+
`[Console]::Error.WriteLine('process-discovery: querying')`,
|
|
107
|
+
`$paths = [System.Collections.Generic.List[string]]::new()`,
|
|
108
|
+
`foreach ($p in [System.Diagnostics.Process]::GetProcessesByName('HBuilderX')) { try { $paths.Add([Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($p.MainModule.FileName))) } catch {} finally { $p.Dispose() } }`,
|
|
109
|
+
`[Console]::Error.WriteLine('process-discovery: complete')`,
|
|
110
|
+
`[Console]::WriteLine('WT-HBUILDERX-PROCESSES/1')`,
|
|
111
|
+
`[Console]::WriteLine([string]::Join([Environment]::NewLine, $paths))`,
|
|
112
|
+
`[Console]::WriteLine('END')`
|
|
113
|
+
].join("; ")
|
|
114
|
+
] : [
|
|
80
115
|
"-ax",
|
|
81
116
|
"-o",
|
|
82
117
|
"command="
|
|
83
|
-
]
|
|
84
|
-
|
|
85
|
-
const
|
|
118
|
+
];
|
|
119
|
+
const timeout = 15e3;
|
|
120
|
+
const maxAttempts = platform === "win32" ? 3 : 1;
|
|
121
|
+
let result;
|
|
122
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
123
|
+
result = spawnSync(command, args, {
|
|
124
|
+
encoding: "utf8",
|
|
125
|
+
stdio: [
|
|
126
|
+
"ignore",
|
|
127
|
+
"pipe",
|
|
128
|
+
"pipe"
|
|
129
|
+
],
|
|
130
|
+
windowsHide: true,
|
|
131
|
+
timeout,
|
|
132
|
+
maxBuffer: 1048576
|
|
133
|
+
});
|
|
134
|
+
if (!result.error && result.status === 0) break;
|
|
135
|
+
if (!(result.signal === "SIGTERM" || result.error?.code === "ETIMEDOUT") || attempt === maxAttempts - 1) break;
|
|
136
|
+
}
|
|
137
|
+
if (!result || result.error || result.status !== 0) throw new Error(`HBuilderX 进程探测失败:${command}, exit=${result?.status}, signal=${result?.signal}, cwd=${process.cwd()}\n${result?.error?.message ?? ""}\n${result?.stderr ?? ""}\n${result?.stdout ?? ""}`);
|
|
138
|
+
let executables;
|
|
139
|
+
if (platform === "win32") {
|
|
140
|
+
const lines = result.stdout.replace(/^\uFEFF/, "").trim().split(/\r?\n/);
|
|
141
|
+
if (lines.shift() !== "WT-HBUILDERX-PROCESSES/1" || lines.pop() !== "END") throw new Error("HBuilderX Windows 进程探测返回了不完整的路径列表");
|
|
142
|
+
executables = lines.filter(Boolean).map((line) => {
|
|
143
|
+
const bytes = Buffer.from(line, "base64");
|
|
144
|
+
const executable = bytes.toString("utf8");
|
|
145
|
+
if (bytes.toString("base64") !== line || !Buffer.from(executable).equals(bytes) || !path.win32.isAbsolute(executable) || path.win32.parse(executable).root.length < 3 || path.win32.basename(executable).toLowerCase() !== "hbuilderx.exe") throw new Error("HBuilderX Windows 进程探测返回了无效路径列表");
|
|
146
|
+
return executable;
|
|
147
|
+
});
|
|
148
|
+
} else executables = extractHBuilderXExecutablesFromProcessOutput(result.stdout, platform);
|
|
149
|
+
const paths = platform === "win32" ? path.win32 : path.posix;
|
|
86
150
|
const candidates = [];
|
|
87
151
|
for (const executable of executables) {
|
|
88
152
|
if (!await fileExists(executable)) continue;
|
|
89
|
-
const cli =
|
|
153
|
+
const cli = paths.join(paths.dirname(executable), platform === "win32" ? "cli.exe" : "cli");
|
|
90
154
|
if (await fileExists(cli)) candidates.push(cli);
|
|
91
155
|
}
|
|
92
156
|
return unique(candidates);
|
|
@@ -106,11 +170,6 @@ function selectHBuilderXCliCandidatesForChannel(items, channel) {
|
|
|
106
170
|
if (channel === "auto") return sortByChannel(items);
|
|
107
171
|
return items.filter((item) => inferHBuilderXChannel(item) === channel);
|
|
108
172
|
}
|
|
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
173
|
async function firstExisting(items) {
|
|
115
174
|
for (const item of items) if (await fileExists(item)) return item;
|
|
116
175
|
}
|
|
@@ -118,21 +177,11 @@ async function resolveHBuilderXCliInfoFromOptions(options = {}) {
|
|
|
118
177
|
const env = options.env ?? process.env;
|
|
119
178
|
const channel = resolveHBuilderXChannel(options.channel ?? env.HBUILDERX_CHANNEL);
|
|
120
179
|
const running = await findRunningHBuilderXCliCandidates();
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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)
|
|
180
|
+
const configured = await resolveConfiguredCli(options);
|
|
181
|
+
if (configured) return {
|
|
182
|
+
...configured,
|
|
183
|
+
isRunning: running.some((item) => normalizeFile(item) === normalizeFile(configured.path)),
|
|
184
|
+
channel: inferHBuilderXChannel(configured.path)
|
|
136
185
|
};
|
|
137
186
|
const runningCandidate = selectHBuilderXCliCandidatesForChannel(running, channel)[0];
|
|
138
187
|
if (runningCandidate) return {
|
|
@@ -197,8 +246,8 @@ function createLogBuffer(maxChunks = 160) {
|
|
|
197
246
|
}
|
|
198
247
|
function collectProcessOutput(child, maxChunks = 160) {
|
|
199
248
|
const buffer = createLogBuffer(maxChunks);
|
|
200
|
-
child.stdout?.on("data", (chunk) => buffer.push(chunk));
|
|
201
|
-
child.stderr?.on("data", (chunk) => buffer.push(chunk));
|
|
249
|
+
child.stdout?.setEncoding("utf8").on("data", (chunk) => buffer.push(chunk));
|
|
250
|
+
child.stderr?.setEncoding("utf8").on("data", (chunk) => buffer.push(chunk));
|
|
202
251
|
return buffer.logs;
|
|
203
252
|
}
|
|
204
253
|
function classifyHBuilderXOutput(output) {
|
|
@@ -340,7 +389,6 @@ function spawnCommand(options) {
|
|
|
340
389
|
const child = spawn(options.command, options.args, {
|
|
341
390
|
cwd: options.cwd,
|
|
342
391
|
detached: options.detached ?? process.platform !== "win32",
|
|
343
|
-
shell: process.platform === "win32",
|
|
344
392
|
stdio: options.stdio === "inherit" ? "inherit" : [
|
|
345
393
|
"ignore",
|
|
346
394
|
"pipe",
|
|
@@ -353,6 +401,7 @@ function spawnCommand(options) {
|
|
|
353
401
|
}
|
|
354
402
|
});
|
|
355
403
|
const logs = options.stdio === "inherit" ? [] : collectProcessOutput(child);
|
|
404
|
+
child.on("error", (error) => logs.push(`${error.message}\n`));
|
|
356
405
|
let exit;
|
|
357
406
|
const closed = new Promise((resolve) => {
|
|
358
407
|
child.on("close", (code, signal) => {
|
|
@@ -426,11 +475,8 @@ function createProcessExitTracker(child) {
|
|
|
426
475
|
};
|
|
427
476
|
}
|
|
428
477
|
//#endregion
|
|
429
|
-
//#region src/hbuilderx/
|
|
478
|
+
//#region src/hbuilderx/hosts.ts
|
|
430
479
|
const ansiRE = new RegExp(`${String.fromCharCode(27)}\\[[\\d;]*m`, "g");
|
|
431
|
-
const defaultStartupTimeoutMs = 6e4;
|
|
432
|
-
const hostPollIntervalMs = 500;
|
|
433
|
-
const maxSessionLogChunks = 160;
|
|
434
480
|
function stripAnsi(value) {
|
|
435
481
|
return value.replace(ansiRE, "");
|
|
436
482
|
}
|
|
@@ -443,18 +489,6 @@ function parseHBuilderXVersion(output) {
|
|
|
443
489
|
function inferHBuilderXChannelFromVersion(version) {
|
|
444
490
|
return /(?:^|-)alpha(?:\.|$)/i.test(version) ? "alpha" : "stable";
|
|
445
491
|
}
|
|
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;
|
|
457
|
-
}
|
|
458
492
|
function createRunnerError(cliPath, cwd, args, kind, message, hint, logs) {
|
|
459
493
|
const output = formatRecentLogs(logs);
|
|
460
494
|
return new HBuilderXCommandError([
|
|
@@ -480,55 +514,55 @@ function createRunnerError(cliPath, cwd, args, kind, message, hint, logs) {
|
|
|
480
514
|
}
|
|
481
515
|
});
|
|
482
516
|
}
|
|
483
|
-
function
|
|
484
|
-
const
|
|
485
|
-
|
|
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);
|
|
496
|
-
}
|
|
497
|
-
async function listCompatibleHosts(cliPath, cwd, env, expectedChannel, explicitHost, timeoutMs) {
|
|
498
|
-
const startedAt = Date.now();
|
|
517
|
+
async function connectHBuilderXHost(options) {
|
|
518
|
+
const { cliPath, cwd, env, expectedChannel, explicitHost } = options;
|
|
519
|
+
const deadline = Date.now() + options.timeoutMs;
|
|
499
520
|
const logs = [];
|
|
500
|
-
|
|
501
|
-
|
|
521
|
+
const timeoutError = (args) => new HBuilderXCommandError(`HBuilderX host 连接超时:${cliPath} ${args.join(" ")}\ncwd=${cwd}\nexit=null\nissue=timeout\n${formatRecentLogs(logs)}`, {
|
|
522
|
+
command: cliPath,
|
|
523
|
+
args,
|
|
524
|
+
cwd,
|
|
525
|
+
exit: {
|
|
526
|
+
code: null,
|
|
527
|
+
signal: null
|
|
528
|
+
},
|
|
529
|
+
logs,
|
|
530
|
+
output: formatRecentLogs(logs),
|
|
531
|
+
issue: createTimeoutIssue()
|
|
532
|
+
});
|
|
533
|
+
const command = async (args, maxTimeoutMs = 1e4) => {
|
|
534
|
+
const remaining = deadline - Date.now();
|
|
535
|
+
if (remaining <= 0) throw timeoutError(args);
|
|
536
|
+
const result = await runCommand({
|
|
502
537
|
command: cliPath,
|
|
503
|
-
args
|
|
538
|
+
args,
|
|
504
539
|
cwd,
|
|
505
540
|
env,
|
|
506
|
-
timeoutMs: Math.min(
|
|
541
|
+
timeoutMs: Math.min(remaining, maxTimeoutMs),
|
|
507
542
|
allowFailure: true
|
|
508
543
|
});
|
|
509
|
-
|
|
510
|
-
|
|
544
|
+
logs.push(...result.logs);
|
|
545
|
+
if (logs.length > 160) logs.splice(0, logs.length - 160);
|
|
546
|
+
return result;
|
|
547
|
+
};
|
|
548
|
+
const assertCommand = (result) => {
|
|
549
|
+
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);
|
|
550
|
+
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);
|
|
551
|
+
};
|
|
552
|
+
const probe = async () => {
|
|
553
|
+
const listed = await command(["listhost"]);
|
|
554
|
+
if (!(listed.issue.kind !== "timeout" && /HBuilderX is not detected running\. Please execute cli open/i.test(stripAnsi(listed.output)))) assertCommand(listed);
|
|
555
|
+
const hosts = parseHBuilderXHosts(listed.output);
|
|
511
556
|
const compatible = [];
|
|
512
|
-
for (const host of hosts) {
|
|
513
|
-
const
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
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;
|
|
557
|
+
for (const host of explicitHost ? [explicitHost] : hosts) {
|
|
558
|
+
const result = await command([
|
|
559
|
+
"version",
|
|
560
|
+
"--host",
|
|
561
|
+
host
|
|
562
|
+
]);
|
|
563
|
+
assertCommand(result);
|
|
564
|
+
const version = parseHBuilderXVersion(result.output);
|
|
565
|
+
if (!version) throw createRunnerError(cliPath, cwd, result.args, "cli-instance-mismatch", `无法验证 HBuilderX host=${host} 的版本`, "请检查目标实例是否就绪。", logs);
|
|
532
566
|
const channel = inferHBuilderXChannelFromVersion(version);
|
|
533
567
|
if (expectedChannel === "unknown" || channel === expectedChannel) compatible.push({
|
|
534
568
|
host,
|
|
@@ -536,16 +570,44 @@ async function listCompatibleHosts(cliPath, cwd, env, expectedChannel, explicitH
|
|
|
536
570
|
channel
|
|
537
571
|
});
|
|
538
572
|
}
|
|
539
|
-
if (compatible.length >
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
};
|
|
543
|
-
await wait(hostPollIntervalMs);
|
|
544
|
-
} while (Date.now() - startedAt < timeoutMs);
|
|
545
|
-
return {
|
|
546
|
-
compatible: [],
|
|
547
|
-
logs
|
|
573
|
+
if (compatible.length > 1) throw createRunnerError(cliPath, cwd, ["listhost"], "cli-host-ambiguous", `检测到多个匹配的 HBuilderX host:${compatible.map((item) => item.host).join(", ")}`, "请设置 HBUILDERX_HOST,或显式传入 host。", logs);
|
|
574
|
+
if (!compatible.length && (hosts.length || explicitHost)) throw createRunnerError(cliPath, cwd, ["listhost"], "cli-instance-mismatch", `未找到与 ${expectedChannel} CLI 匹配的 HBuilderX host`, "请检查 HBUILDERX_CLI_PATH、HBUILDERX_CHANNEL 和 HBUILDERX_HOST。", logs);
|
|
575
|
+
return compatible[0];
|
|
548
576
|
};
|
|
577
|
+
const existing = await probe();
|
|
578
|
+
if (existing) return existing;
|
|
579
|
+
assertCommand(await command(["open"], options.timeoutMs));
|
|
580
|
+
do {
|
|
581
|
+
const selected = await probe();
|
|
582
|
+
if (selected) return selected;
|
|
583
|
+
await wait(Math.min(500, Math.max(0, deadline - Date.now())));
|
|
584
|
+
} while (Date.now() < deadline);
|
|
585
|
+
throw timeoutError(["listhost"]);
|
|
586
|
+
}
|
|
587
|
+
//#endregion
|
|
588
|
+
//#region src/hbuilderx/runner.ts
|
|
589
|
+
const defaultStartupTimeoutMs = 6e4;
|
|
590
|
+
function createHBuilderXNativeLaunchArgs(options) {
|
|
591
|
+
const args = [
|
|
592
|
+
"launch",
|
|
593
|
+
options.platform,
|
|
594
|
+
"--project",
|
|
595
|
+
path.resolve(options.cwd),
|
|
596
|
+
...options.args ?? []
|
|
597
|
+
];
|
|
598
|
+
if (options.compile != null && !args.includes("--compile")) args.push("--compile", String(options.compile));
|
|
599
|
+
if (options.runtimeLog != null && !args.includes("--runtime-log")) args.push("--runtime-log", String(options.runtimeLog));
|
|
600
|
+
return args;
|
|
601
|
+
}
|
|
602
|
+
function bindHost(args, host) {
|
|
603
|
+
const hostIndex = args.indexOf("--host");
|
|
604
|
+
if (hostIndex < 0) return [
|
|
605
|
+
...args,
|
|
606
|
+
"--host",
|
|
607
|
+
host
|
|
608
|
+
];
|
|
609
|
+
if (args[hostIndex + 1] !== host) throw new Error(`runner 已绑定 HBuilderX host=${host},不能改为 ${args[hostIndex + 1] ?? "空值"}。`);
|
|
610
|
+
return args;
|
|
549
611
|
}
|
|
550
612
|
async function createHBuilderXRunner(options = {}) {
|
|
551
613
|
const env = {
|
|
@@ -554,35 +616,32 @@ async function createHBuilderXRunner(options = {}) {
|
|
|
554
616
|
};
|
|
555
617
|
const cwd = options.cwd ?? process.cwd();
|
|
556
618
|
const timeoutMs = options.timeoutMs ?? defaultStartupTimeoutMs;
|
|
557
|
-
const
|
|
619
|
+
const resolveOptions = {
|
|
558
620
|
candidates: options.hbuilderxCliPath ? [options.hbuilderxCliPath] : options.candidates,
|
|
559
621
|
channel: options.channel,
|
|
560
622
|
env,
|
|
561
623
|
host: options.host
|
|
562
|
-
}
|
|
624
|
+
};
|
|
625
|
+
const configured = await resolveConfiguredCli(resolveOptions);
|
|
626
|
+
const resolution = configured ? {
|
|
627
|
+
...configured,
|
|
628
|
+
channel: inferHBuilderXChannel(configured.path)
|
|
629
|
+
} : await resolveHBuilderXCliInfoFromOptions(resolveOptions);
|
|
563
630
|
const cliEnv = {
|
|
564
631
|
...env,
|
|
565
632
|
HBUILDERX_CLI_PATH: resolution.path
|
|
566
633
|
};
|
|
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);
|
|
578
|
-
}
|
|
579
634
|
const requestedChannel = resolveHBuilderXChannel(options.channel ?? env.HBUILDERX_CHANNEL);
|
|
580
635
|
const expectedChannel = resolution.channel !== "unknown" ? resolution.channel : requestedChannel === "auto" ? inferHBuilderXChannel(resolution.path) : requestedChannel;
|
|
581
636
|
const explicitHost = options.host ?? env.HBUILDERX_HOST;
|
|
582
|
-
const
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
637
|
+
const selected = await connectHBuilderXHost({
|
|
638
|
+
cliPath: resolution.path,
|
|
639
|
+
cwd,
|
|
640
|
+
env: cliEnv,
|
|
641
|
+
expectedChannel,
|
|
642
|
+
explicitHost,
|
|
643
|
+
timeoutMs
|
|
644
|
+
});
|
|
586
645
|
const boundResolution = {
|
|
587
646
|
...resolution,
|
|
588
647
|
isRunning: true,
|
|
@@ -668,7 +727,9 @@ async function resolveHBuilderXCliInfo(candidatesOrOptions, env = process.env) {
|
|
|
668
727
|
} : candidatesOrOptions);
|
|
669
728
|
}
|
|
670
729
|
async function resolveHBuilderXCli(candidatesOrOptions) {
|
|
671
|
-
|
|
730
|
+
const options = Array.isArray(candidatesOrOptions) ? { candidates: candidatesOrOptions } : candidatesOrOptions ?? {};
|
|
731
|
+
resolveHBuilderXChannel(options.channel ?? (options.env ?? process.env).HBUILDERX_CHANNEL);
|
|
732
|
+
return (await resolveConfiguredCli(options))?.path ?? (await resolveHBuilderXCliInfoFromOptions(options)).path;
|
|
672
733
|
}
|
|
673
734
|
function createHBuilderXEnv(options = {}) {
|
|
674
735
|
return {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weapp-tailwindcss/hbuilderx-runner",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.5",
|
|
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",
|
|
@@ -42,6 +42,12 @@
|
|
|
42
42
|
"publishConfig": {
|
|
43
43
|
"access": "public"
|
|
44
44
|
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"cross-spawn": "^7.0.6"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/cross-spawn": "^6.0.6"
|
|
50
|
+
},
|
|
45
51
|
"scripts": {
|
|
46
52
|
"dev": "tsdown --watch --sourcemap",
|
|
47
53
|
"build": "tsdown",
|