weapp-ide-cli 6.2.0 → 6.3.0

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.
@@ -0,0 +1,186 @@
1
+ import { M as logger } from "./automatorProject-DjYLqMj6.js";
2
+ import { E as promptWechatIdeLoginRetry, M as i18nText, P as resolveAutomatorSessionOptions, c as getAutomatorProtocolTimeoutMethod, d as isAutomatorProtocolTimeoutError, f as isAutomatorWsConnectError, i as resolveProjectAutomatorPort, l as isAutomatorLoginError, m as isDevtoolsHttpPortError, n as launchAutomator, p as isDevtoolsExtensionContextInvalidatedError, s as formatAutomatorLoginError, t as connectOpenedAutomator, u as isAutomatorPortInUseError, y as createWechatIdeLoginRequiredExitError } from "./automator-BOe32upt.js";
3
+ import { acquireSharedMiniProgram, closeOwnedSharedMiniProgram, closeSharedMiniProgram, getSharedMiniProgramSessionCount, releaseSharedMiniProgram, withMiniProgram } from "@weapp-vite/devtools-runtime";
4
+ import { OperationLifecycle } from "@weapp-vite/miniprogram-automator/operation";
5
+ //#region src/cli/run-login-executor.ts
6
+ /** 命令、登录提示和有限重试共用一个 deadline,取消信号由执行边界传入 I/O。 */
7
+ async function runRetryableCommand(options) {
8
+ return await new OperationLifecycle(options.timeout ?? 3e4, "IDE command", options.signal).run(async (scope) => {
9
+ const maxAttempts = options.maxAttempts ?? 3;
10
+ for (let retryCount = 0;; retryCount += 1) {
11
+ scope.attempt();
12
+ const result = await scope.step(() => options.execute(scope), {
13
+ stage: "execute",
14
+ waitForExit: true,
15
+ disposeLate: options.disposeLate
16
+ });
17
+ if (!options.isRetryableResult(result)) return result;
18
+ const cause = options.createCancelError(result);
19
+ scope.recordFailure(cause);
20
+ if (retryCount + 1 >= maxAttempts) throw cause;
21
+ const action = await scope.step(() => options.promptRetry(result, retryCount, scope), {
22
+ stage: "login-retry",
23
+ waitForExit: true
24
+ });
25
+ if (options.shouldRetry(action)) {
26
+ scope.throwIfAborted();
27
+ options.onRetry?.();
28
+ continue;
29
+ }
30
+ options.onCancel?.(result);
31
+ throw cause;
32
+ }
33
+ });
34
+ }
35
+ //#endregion
36
+ //#region src/cli/automator-session.ts
37
+ function unwrapAutomatorConnectionError(result) {
38
+ return result.kind === "retryable" ? result.error : result.value;
39
+ }
40
+ async function launchFreshAutomator(options) {
41
+ const shouldUseProjectPort = !options.port && !options.sessionId;
42
+ return await launchAutomator({
43
+ ...options,
44
+ ...shouldUseProjectPort ? {
45
+ persistAsDefaultSession: true,
46
+ port: resolveProjectAutomatorPort(options.projectPath)
47
+ } : {}
48
+ });
49
+ }
50
+ function normalizeMiniProgramConnectionError(error, background = false) {
51
+ const diagnostics = background ? {
52
+ error: (message) => logger.debug(message),
53
+ warn: (message) => logger.debug(message)
54
+ } : logger;
55
+ if (isAutomatorLoginError(error)) {
56
+ diagnostics.error(i18nText("检测到微信开发者工具登录状态失效,请先登录后重试。", "Wechat DevTools login has expired. Please login and retry."));
57
+ diagnostics.warn(formatAutomatorLoginError(error));
58
+ return new Error("DEVTOOLS_LOGIN_REQUIRED", { cause: error });
59
+ }
60
+ if (isDevtoolsHttpPortError(error)) {
61
+ diagnostics.error(i18nText("无法连接到微信开发者工具,请确保已开启 HTTP 服务端口。", "Cannot connect to Wechat DevTools. Please ensure HTTP service port is enabled."));
62
+ diagnostics.warn(i18nText("请在微信开发者工具中:设置 -> 安全设置 -> 开启服务端口", "Please enable service port in Wechat DevTools: Settings -> Security -> Service Port"));
63
+ return new Error("DEVTOOLS_HTTP_PORT_ERROR", { cause: error });
64
+ }
65
+ if (isDevtoolsExtensionContextInvalidatedError(error)) {
66
+ diagnostics.error(i18nText("微信开发者工具自动化上下文尚未就绪,通常是刚启动或正在重载。", "Wechat DevTools automation context is not ready yet, usually because the IDE has just started or is still reloading."));
67
+ diagnostics.warn(i18nText("请稍后重试;若持续失败,关闭多余的开发者工具窗口后重试。", "Please retry shortly. If it keeps failing, close extra DevTools windows and try again."));
68
+ return new Error("DEVTOOLS_EXTENSION_CONTEXT_INVALIDATED", { cause: error });
69
+ }
70
+ if (isAutomatorWsConnectError(error) || isAutomatorPortInUseError(error)) {
71
+ diagnostics.error(i18nText("无法连接到当前项目的微信开发者工具自动化 websocket。", "Cannot connect to the Wechat DevTools automation websocket for the current project."));
72
+ diagnostics.warn(i18nText("请确认当前打开的是目标项目;若之前跑过其他 e2e / screenshot 任务,关闭多余的微信开发者工具窗口,或结束残留的 `wechatwebdevtools cli auto --project ...` 进程后重试。", "Please confirm the current DevTools window is the target project. If you recently ran other e2e / screenshot tasks, close extra windows or stop stale `wechatwebdevtools cli auto --project ...` processes and retry."));
73
+ return new Error("DEVTOOLS_WS_CONNECT_ERROR", { cause: error });
74
+ }
75
+ if (isAutomatorProtocolTimeoutError(error)) {
76
+ const method = getAutomatorProtocolTimeoutMethod(error) ?? "unknown";
77
+ diagnostics.error(i18nText(`微信开发者工具在协议调用 ${method} 上超时,未按预期返回结果。`, `Wechat DevTools timed out while executing protocol method ${method} and did not return a result.`));
78
+ diagnostics.warn(i18nText("这通常表示当前 DevTools 自动化会话已卡住、窗口不在目标项目、或当前 DevTools 版本对该协议调用无响应。请重开目标项目窗口后重试;若仍复现,优先记录当前 DevTools 版本与协议方法名继续排查。", "This usually means the current DevTools automation session is stuck, the window is not on the target project, or the current DevTools version is not responding to that protocol method. Reopen the target project window and retry. If it still reproduces, record the current DevTools version and protocol method name for follow-up debugging."));
79
+ return new Error("DEVTOOLS_PROTOCOL_TIMEOUT", { cause: error });
80
+ }
81
+ return error instanceof Error ? error : new Error(String(error));
82
+ }
83
+ /**
84
+ * @description 建立 automator 会话,并统一处理常见连接错误提示。
85
+ */
86
+ async function connectMiniProgramWithDiagnostics(options, background) {
87
+ options = await resolveAutomatorSessionOptions(options);
88
+ const result = await runRetryableCommand({
89
+ timeout: options.timeout,
90
+ signal: options.signal,
91
+ disposeLate: (result) => {
92
+ if (result.kind === "result") return result.value.disconnect();
93
+ },
94
+ createCancelError: (result) => createWechatIdeLoginRequiredExitError(unwrapAutomatorConnectionError(result), "cancelled"),
95
+ execute: async (operation) => {
96
+ const operationOptions = () => ({
97
+ ...options,
98
+ timeout: operation.remainingMs(),
99
+ signal: operation.signal
100
+ });
101
+ if (options.runtimeProvider === "headless" || options.preferOpenedSession === false && options.openedOnly !== true) try {
102
+ return {
103
+ kind: "result",
104
+ value: await launchFreshAutomator(operationOptions())
105
+ };
106
+ } catch (error) {
107
+ if (!isAutomatorLoginError(error)) throw normalizeMiniProgramConnectionError(error, background);
108
+ return {
109
+ error,
110
+ kind: "retryable"
111
+ };
112
+ }
113
+ try {
114
+ return {
115
+ kind: "result",
116
+ value: await connectOpenedAutomator(operationOptions())
117
+ };
118
+ } catch (error) {
119
+ const normalizedOpenSessionError = normalizeMiniProgramConnectionError(error, background);
120
+ if (options.openedOnly || options.preferOpenedSession !== false && Boolean(options.port || options.sessionId)) throw normalizedOpenSessionError;
121
+ if (normalizedOpenSessionError instanceof Error && ("code" in normalizedOpenSessionError && normalizedOpenSessionError.code === "WECHAT_DEVTOOLS_HOST_IDENTITY_MISMATCH" || normalizedOpenSessionError.message === "DEVTOOLS_PROTOCOL_TIMEOUT" || normalizedOpenSessionError.message.startsWith("DEVTOOLS_INSTALLATION_MISMATCH") || normalizedOpenSessionError.message.startsWith("DEVTOOLS_PORT_IDENTITY"))) throw normalizedOpenSessionError;
122
+ try {
123
+ return {
124
+ kind: "result",
125
+ value: await launchFreshAutomator(operationOptions())
126
+ };
127
+ } catch (launchError) {
128
+ if (!isAutomatorLoginError(launchError)) throw normalizeMiniProgramConnectionError(launchError, background);
129
+ return {
130
+ error: launchError,
131
+ kind: "retryable"
132
+ };
133
+ }
134
+ }
135
+ },
136
+ isRetryableResult: (result) => result.kind === "retryable",
137
+ onRetry: () => {
138
+ logger.info(i18nText("正在重试连接微信开发者工具...", "Retrying to connect Wechat DevTools..."));
139
+ },
140
+ promptRetry: async (result, _retryCount, operation) => await promptWechatIdeLoginRetry({
141
+ error: unwrapAutomatorConnectionError(result),
142
+ retryTimeoutMs: operation.remainingMs(),
143
+ signal: operation.signal,
144
+ logger,
145
+ promptOpenIdeLogin: true
146
+ }),
147
+ shouldRetry: (action) => action === "retry"
148
+ });
149
+ if (result.kind === "retryable") throw createWechatIdeLoginRequiredExitError(result.error);
150
+ return result.value;
151
+ }
152
+ /** 建立交互式会话,并报告最终连接错误。 */
153
+ async function connectMiniProgram(options) {
154
+ return await connectMiniProgramWithDiagnostics(options, false);
155
+ }
156
+ /** 后台日志桥接的连接探针,不在每次可恢复失败时输出终局错误。 */
157
+ async function connectConsoleMiniProgram(options) {
158
+ return await connectMiniProgramWithDiagnostics(options, true);
159
+ }
160
+ /** 为后台日志桥接保留共享会话所有权和诊断级别。 */
161
+ async function acquireConsoleMiniProgram(options) {
162
+ return await acquireSharedMiniProgram({
163
+ resolveSessionOptions: resolveAutomatorSessionOptions,
164
+ connectMiniProgram: connectConsoleMiniProgram,
165
+ normalizeConnectionError: (error) => normalizeMiniProgramConnectionError(error, true)
166
+ }, options);
167
+ }
168
+ const runtimeHooks = {
169
+ resolveSessionOptions: resolveAutomatorSessionOptions,
170
+ connectMiniProgram,
171
+ normalizeConnectionError: normalizeMiniProgramConnectionError
172
+ };
173
+ /**
174
+ * @description 获取指定项目的共享 automator 会话;若不存在则自动创建。
175
+ */
176
+ async function acquireSharedMiniProgram$1(options) {
177
+ return await acquireSharedMiniProgram(runtimeHooks, options);
178
+ }
179
+ /**
180
+ * @description 统一管理 automator 会话生命周期。
181
+ */
182
+ async function withMiniProgram$1(options, runner) {
183
+ return await withMiniProgram(runtimeHooks, options, runner);
184
+ }
185
+ //#endregion
186
+ export { connectConsoleMiniProgram as a, releaseSharedMiniProgram as c, closeSharedMiniProgram as i, withMiniProgram$1 as l, acquireSharedMiniProgram$1 as n, connectMiniProgram as o, closeOwnedSharedMiniProgram as r, getSharedMiniProgramSessionCount as s, acquireConsoleMiniProgram as t, runRetryableCommand as u };