helixlife-v5-cli 1.1.9 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/.cli.runtime.generated.json +7 -0
- package/lib/automation-overlay.init.js +208 -0
- package/lib/cli.js +78 -15
- package/lib/cli.runtime.config.json +5 -0
- package/lib/visual.js +248 -0
- package/package.json +1 -1
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* helixlife-v5-cli 页面内视觉反馈层。
|
|
4
|
+
* 挂载 window.__helix_visual__:节点高亮框 + 动作标签(pending/success/danger/info)+ 右上角 HUD。
|
|
5
|
+
* 可作为 Playwright initScript 注入,也可由 CLI eval 懒加载。
|
|
6
|
+
*/
|
|
7
|
+
(() => {
|
|
8
|
+
if (window.__helix_visual__) return;
|
|
9
|
+
|
|
10
|
+
const VERSION = '1.0.0';
|
|
11
|
+
const VISUAL_DEFAULTS = {
|
|
12
|
+
enabled: true,
|
|
13
|
+
durationMs: 420,
|
|
14
|
+
};
|
|
15
|
+
const VISUAL_STYLE_ID = '__helix_visual_style__';
|
|
16
|
+
const VISUAL_LAYER_ID = '__helix_visual_layer__';
|
|
17
|
+
const VISUAL_HUD_ID = '__helix_visual_hud__';
|
|
18
|
+
const VISUAL_TONE_MAP = {
|
|
19
|
+
pending: { border: '#faad14', fill: 'rgba(250, 173, 20, 0.16)', pill: '#ad6800', text: '#fffbe6' },
|
|
20
|
+
success: { border: '#52c41a', fill: 'rgba(82, 196, 26, 0.14)', pill: '#237804', text: '#f6ffed' },
|
|
21
|
+
danger: { border: '#ff4d4f', fill: 'rgba(255, 77, 79, 0.14)', pill: '#a8071a', text: '#fff1f0' },
|
|
22
|
+
info: { border: '#1677ff', fill: 'rgba(22, 119, 255, 0.14)', pill: '#0958d9', text: '#f0f5ff' },
|
|
23
|
+
};
|
|
24
|
+
const visualState = {
|
|
25
|
+
config: { ...VISUAL_DEFAULTS },
|
|
26
|
+
hudTimer: null,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function clamp(n, min, max) {
|
|
30
|
+
return Math.max(min, Math.min(max, n));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function normalizeDuration(ms, fallback = VISUAL_DEFAULTS.durationMs) {
|
|
34
|
+
const n = Number(ms);
|
|
35
|
+
if (!Number.isFinite(n) || n <= 0) return fallback;
|
|
36
|
+
return clamp(Math.round(n), 120, 4000);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function normalizeVisualOptions(options = {}) {
|
|
40
|
+
const base = { ...visualState.config };
|
|
41
|
+
if (!options || typeof options !== 'object') return base;
|
|
42
|
+
if (typeof options.enabled === 'boolean') base.enabled = options.enabled;
|
|
43
|
+
if (options.durationMs != null) base.durationMs = normalizeDuration(options.durationMs, base.durationMs);
|
|
44
|
+
return base;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function ensureVisualRoot() {
|
|
48
|
+
let style = document.getElementById(VISUAL_STYLE_ID);
|
|
49
|
+
if (!style) {
|
|
50
|
+
style = document.createElement('style');
|
|
51
|
+
style.id = VISUAL_STYLE_ID;
|
|
52
|
+
style.textContent = `
|
|
53
|
+
#${VISUAL_LAYER_ID}{
|
|
54
|
+
position:fixed;
|
|
55
|
+
inset:0;
|
|
56
|
+
pointer-events:none;
|
|
57
|
+
z-index:2147483646;
|
|
58
|
+
overflow:visible;
|
|
59
|
+
}
|
|
60
|
+
.__helix_visual_box{
|
|
61
|
+
position:fixed;
|
|
62
|
+
box-sizing:border-box;
|
|
63
|
+
border-radius:8px;
|
|
64
|
+
animation:__helix_visual_pulse .55s ease-out 1;
|
|
65
|
+
}
|
|
66
|
+
.__helix_visual_badge{
|
|
67
|
+
position:absolute;
|
|
68
|
+
left:0;
|
|
69
|
+
top:-28px;
|
|
70
|
+
max-width:280px;
|
|
71
|
+
padding:4px 10px;
|
|
72
|
+
border-radius:999px;
|
|
73
|
+
font:600 12px/1.2 system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;
|
|
74
|
+
letter-spacing:.01em;
|
|
75
|
+
white-space:nowrap;
|
|
76
|
+
text-overflow:ellipsis;
|
|
77
|
+
overflow:hidden;
|
|
78
|
+
box-shadow:0 8px 24px rgba(0,0,0,.18);
|
|
79
|
+
}
|
|
80
|
+
#${VISUAL_HUD_ID}{
|
|
81
|
+
position:fixed;
|
|
82
|
+
top:16px;
|
|
83
|
+
right:16px;
|
|
84
|
+
max-width:360px;
|
|
85
|
+
padding:10px 14px;
|
|
86
|
+
border-radius:12px;
|
|
87
|
+
box-shadow:0 12px 32px rgba(0,0,0,.22);
|
|
88
|
+
font:600 13px/1.35 system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;
|
|
89
|
+
white-space:pre-wrap;
|
|
90
|
+
}
|
|
91
|
+
@keyframes __helix_visual_pulse{
|
|
92
|
+
0%{transform:scale(.985);opacity:.2}
|
|
93
|
+
35%{transform:scale(1.003);opacity:1}
|
|
94
|
+
100%{transform:scale(1);opacity:1}
|
|
95
|
+
}
|
|
96
|
+
`;
|
|
97
|
+
(document.head || document.documentElement).appendChild(style);
|
|
98
|
+
}
|
|
99
|
+
let layer = document.getElementById(VISUAL_LAYER_ID);
|
|
100
|
+
if (!layer) {
|
|
101
|
+
layer = document.createElement('div');
|
|
102
|
+
layer.id = VISUAL_LAYER_ID;
|
|
103
|
+
(document.body || document.documentElement).appendChild(layer);
|
|
104
|
+
}
|
|
105
|
+
return layer;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function toneSpec(tone) {
|
|
109
|
+
return VISUAL_TONE_MAP[tone] || VISUAL_TONE_MAP.info;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function removeLater(el, durationMs) {
|
|
113
|
+
if (!el) return;
|
|
114
|
+
const ms = normalizeDuration(durationMs);
|
|
115
|
+
window.setTimeout(() => {
|
|
116
|
+
if (el && el.parentNode) el.remove();
|
|
117
|
+
}, ms);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function flashElement(el, options = {}) {
|
|
121
|
+
const { tone = 'info', label = '', durationMs, inset = 0 } = options;
|
|
122
|
+
if (!el || !visualState.config.enabled) return false;
|
|
123
|
+
const rect = el.getBoundingClientRect();
|
|
124
|
+
if (!rect || rect.width <= 0 || rect.height <= 0) return false;
|
|
125
|
+
const layer = ensureVisualRoot();
|
|
126
|
+
const spec = toneSpec(tone);
|
|
127
|
+
const box = document.createElement('div');
|
|
128
|
+
box.className = '__helix_visual_box';
|
|
129
|
+
box.style.left = Math.max(4, rect.left - inset) + 'px';
|
|
130
|
+
box.style.top = Math.max(4, rect.top - inset) + 'px';
|
|
131
|
+
box.style.width = Math.max(18, rect.width + inset * 2) + 'px';
|
|
132
|
+
box.style.height = Math.max(18, rect.height + inset * 2) + 'px';
|
|
133
|
+
box.style.border = `2px solid ${spec.border}`;
|
|
134
|
+
box.style.background = spec.fill;
|
|
135
|
+
box.style.boxShadow = `0 0 0 1px ${spec.border}33, 0 12px 28px ${spec.border}22`;
|
|
136
|
+
if (label) {
|
|
137
|
+
const badge = document.createElement('div');
|
|
138
|
+
badge.className = '__helix_visual_badge';
|
|
139
|
+
badge.textContent = label;
|
|
140
|
+
badge.style.background = spec.pill;
|
|
141
|
+
badge.style.color = spec.text;
|
|
142
|
+
badge.style.top = (rect.top < 38 ? rect.height + 8 : -28) + 'px';
|
|
143
|
+
box.appendChild(badge);
|
|
144
|
+
}
|
|
145
|
+
layer.appendChild(box);
|
|
146
|
+
removeLater(box, durationMs);
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function showHud(options = {}) {
|
|
151
|
+
const { action = '', target = '', status = 'pending', detail = '', durationMs } = options;
|
|
152
|
+
if (!visualState.config.enabled) return false;
|
|
153
|
+
const layer = ensureVisualRoot();
|
|
154
|
+
let hud = document.getElementById(VISUAL_HUD_ID);
|
|
155
|
+
if (!hud) {
|
|
156
|
+
hud = document.createElement('div');
|
|
157
|
+
hud.id = VISUAL_HUD_ID;
|
|
158
|
+
layer.appendChild(hud);
|
|
159
|
+
}
|
|
160
|
+
const spec = toneSpec(status);
|
|
161
|
+
const lines = [];
|
|
162
|
+
if (action) lines.push(action);
|
|
163
|
+
if (target) lines.push(target);
|
|
164
|
+
if (detail) lines.push(detail);
|
|
165
|
+
hud.textContent = lines.join('\n');
|
|
166
|
+
hud.style.border = `1px solid ${spec.border}`;
|
|
167
|
+
hud.style.background = spec.fill.replace(/0\.\d+\)$/, '0.92)');
|
|
168
|
+
hud.style.color = spec.pill;
|
|
169
|
+
if (visualState.hudTimer) clearTimeout(visualState.hudTimer);
|
|
170
|
+
visualState.hudTimer = window.setTimeout(() => {
|
|
171
|
+
if (hud && hud.parentNode) hud.remove();
|
|
172
|
+
visualState.hudTimer = null;
|
|
173
|
+
}, normalizeDuration(durationMs, Math.max(900, visualState.config.durationMs * 2)));
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function cleanupVisualArtifacts() {
|
|
178
|
+
const layer = document.getElementById(VISUAL_LAYER_ID);
|
|
179
|
+
if (!layer) return;
|
|
180
|
+
Array.from(layer.querySelectorAll('.__helix_visual_box')).forEach((el) => el.remove());
|
|
181
|
+
const hud = document.getElementById(VISUAL_HUD_ID);
|
|
182
|
+
if (hud) hud.remove();
|
|
183
|
+
if (visualState.hudTimer) {
|
|
184
|
+
clearTimeout(visualState.hudTimer);
|
|
185
|
+
visualState.hudTimer = null;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function setVisualOptions(options = {}) {
|
|
190
|
+
if (!options || typeof options !== 'object') {
|
|
191
|
+
visualState.config = { ...VISUAL_DEFAULTS };
|
|
192
|
+
cleanupVisualArtifacts();
|
|
193
|
+
return { ok: true, data: { ...visualState.config } };
|
|
194
|
+
}
|
|
195
|
+
visualState.config = normalizeVisualOptions(options);
|
|
196
|
+
if (!visualState.config.enabled) cleanupVisualArtifacts();
|
|
197
|
+
return { ok: true, data: { ...visualState.config } };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
window.__helix_visual__ = {
|
|
201
|
+
version: () => VERSION,
|
|
202
|
+
setVisualOptions,
|
|
203
|
+
flash: flashElement,
|
|
204
|
+
showHud,
|
|
205
|
+
cleanup: cleanupVisualArtifacts,
|
|
206
|
+
__meta: { version: VERSION, loadedAt: new Date().toISOString() },
|
|
207
|
+
};
|
|
208
|
+
})();
|
package/lib/cli.js
CHANGED
|
@@ -4,6 +4,13 @@ const path = require('node:path');
|
|
|
4
4
|
const { spawnSync } = require('node:child_process');
|
|
5
5
|
|
|
6
6
|
const PACKAGE_JSON = require(path.join(__dirname, '..', 'package.json'));
|
|
7
|
+
const {
|
|
8
|
+
parseVisualFlags,
|
|
9
|
+
injectVisualConfig,
|
|
10
|
+
shouldWrapWithVisual,
|
|
11
|
+
runWithVisualFeedback,
|
|
12
|
+
DEFAULT_VISUAL_ENABLED,
|
|
13
|
+
} = require('./visual');
|
|
7
14
|
|
|
8
15
|
/** 默认快照/下载等工作区目录名(相对 cwd),替代引擎默认的 .playwright-cli */
|
|
9
16
|
const DEFAULT_OUTPUT_DIR_NAME = 'helixlife-v5-cli';
|
|
@@ -34,21 +41,38 @@ function buildChildEnv() {
|
|
|
34
41
|
/**
|
|
35
42
|
* 将参数原样交给底层浏览器自动化引擎,返回子进程退出码。
|
|
36
43
|
* 使用 node 直接执行入口脚本,避免依赖 npx、Shell 与 PATH,在 Windows / macOS 上一致。
|
|
44
|
+
* @param {string[]} args
|
|
45
|
+
* @param {{ capture?: boolean }} [options]
|
|
46
|
+
* @returns {number | { code: number, stdout: string, stderr: string }}
|
|
37
47
|
*/
|
|
38
|
-
function runPlaywrightCliPassthrough(args) {
|
|
39
|
-
const
|
|
40
|
-
stdio: 'inherit',
|
|
48
|
+
function runPlaywrightCliPassthrough(args, options = {}) {
|
|
49
|
+
const spawnOpts = {
|
|
41
50
|
env: buildChildEnv(),
|
|
42
51
|
windowsHide: true,
|
|
43
|
-
}
|
|
52
|
+
};
|
|
53
|
+
if (options.capture) {
|
|
54
|
+
spawnOpts.encoding = 'utf8';
|
|
55
|
+
spawnOpts.stdio = ['ignore', 'pipe', 'pipe'];
|
|
56
|
+
} else {
|
|
57
|
+
spawnOpts.stdio = 'inherit';
|
|
58
|
+
}
|
|
59
|
+
const result = spawnSync(process.execPath, [playwrightCliEntry(), ...args], spawnOpts);
|
|
44
60
|
if (result.error) {
|
|
45
61
|
throw result.error;
|
|
46
62
|
}
|
|
47
63
|
if (result.signal) {
|
|
48
|
-
|
|
49
|
-
|
|
64
|
+
if (!options.capture) {
|
|
65
|
+
console.error(`[${PACKAGE_JSON.name}] 子进程被终止: ${result.signal}`);
|
|
66
|
+
}
|
|
67
|
+
return options.capture
|
|
68
|
+
? { code: 1, stdout: result.stdout || '', stderr: result.stderr || '' }
|
|
69
|
+
: 1;
|
|
50
70
|
}
|
|
51
|
-
|
|
71
|
+
const code = result.status === null || result.status === undefined ? 1 : result.status;
|
|
72
|
+
if (options.capture) {
|
|
73
|
+
return { code, stdout: result.stdout || '', stderr: result.stderr || '' };
|
|
74
|
+
}
|
|
75
|
+
return code;
|
|
52
76
|
}
|
|
53
77
|
|
|
54
78
|
function parseHelixFlags(tokens, startIndex) {
|
|
@@ -57,11 +81,21 @@ function parseHelixFlags(tokens, startIndex) {
|
|
|
57
81
|
cdpUrl: process.env.HELIX_CDP_URL || 'http://127.0.0.1:9222',
|
|
58
82
|
profile:
|
|
59
83
|
process.env.HELIX_PROFILE_DIR || path.join(process.cwd(), '.helixlife-profile'),
|
|
84
|
+
visual: { enabled: DEFAULT_VISUAL_ENABLED, durationMs: null },
|
|
60
85
|
};
|
|
61
86
|
for (let i = startIndex; i < tokens.length; i += 1) {
|
|
62
87
|
const t = tokens[i];
|
|
63
88
|
if (t === '--headless') {
|
|
64
89
|
options.headless = true;
|
|
90
|
+
} else if (t === '--visual') {
|
|
91
|
+
options.visual.enabled = true;
|
|
92
|
+
} else if (t === '--no-visual') {
|
|
93
|
+
options.visual.enabled = false;
|
|
94
|
+
} else if (t === '--visual-ms') {
|
|
95
|
+
options.visual.durationMs = tokens[i + 1];
|
|
96
|
+
i += 1;
|
|
97
|
+
} else if (t.startsWith('--visual-ms=')) {
|
|
98
|
+
options.visual.durationMs = t.slice('--visual-ms='.length);
|
|
65
99
|
} else if (t === '--profile') {
|
|
66
100
|
const next = tokens[i + 1];
|
|
67
101
|
if (next === undefined) {
|
|
@@ -85,6 +119,14 @@ function parseHelixFlags(tokens, startIndex) {
|
|
|
85
119
|
return null;
|
|
86
120
|
}
|
|
87
121
|
}
|
|
122
|
+
if (options.visual.durationMs != null) {
|
|
123
|
+
const n = Number(options.visual.durationMs);
|
|
124
|
+
if (!Number.isFinite(n) || n <= 0) {
|
|
125
|
+
console.error(`[${PACKAGE_JSON.name}] --visual-ms 需要正数,收到 ${JSON.stringify(options.visual.durationMs)}`);
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
options.visual.durationMs = Math.round(n);
|
|
129
|
+
}
|
|
88
130
|
return options;
|
|
89
131
|
}
|
|
90
132
|
|
|
@@ -105,6 +147,11 @@ function printHelp() {
|
|
|
105
147
|
` ${n} helix doctor [--cdp-url <url>] [--profile <dir>] 先 list,再执行 helix home`
|
|
106
148
|
);
|
|
107
149
|
console.log('');
|
|
150
|
+
console.log('页面内视觉反馈(默认开启,作用于 click/fill/hover 等元素命令):');
|
|
151
|
+
console.log(` ${n} click e5 操作前黄框 + 标签,成功后绿框`);
|
|
152
|
+
console.log(` ${n} click e5 --no-visual 静默执行,无页面高亮`);
|
|
153
|
+
console.log(` ${n} fill e3 "文本" --visual-ms 700 自定义高亮时长(毫秒)`);
|
|
154
|
+
console.log('');
|
|
108
155
|
console.log('本地开发可使用:');
|
|
109
156
|
console.log(' node helix-cli.js <同上参数>');
|
|
110
157
|
console.log('');
|
|
@@ -112,8 +159,9 @@ function printHelp() {
|
|
|
112
159
|
'环境变量:HELIX_BASE_URL、HELIX_CDP_URL、HELIX_PROFILE_DIR(默认 profile 目录)、'
|
|
113
160
|
);
|
|
114
161
|
console.log(
|
|
115
|
-
` HELIX_OUTPUT_DIR / PLAYWRIGHT_MCP_OUTPUT_DIR(默认输出目录 <cwd>/${DEFAULT_OUTPUT_DIR_NAME}
|
|
162
|
+
` HELIX_OUTPUT_DIR / PLAYWRIGHT_MCP_OUTPUT_DIR(默认输出目录 <cwd>/${DEFAULT_OUTPUT_DIR_NAME})、`
|
|
116
163
|
);
|
|
164
|
+
console.log(' HELIX_VISUAL=0(全局关闭视觉反馈,可用 --visual 单次开启)');
|
|
117
165
|
}
|
|
118
166
|
|
|
119
167
|
function getBaseUrl() {
|
|
@@ -122,8 +170,7 @@ function getBaseUrl() {
|
|
|
122
170
|
|
|
123
171
|
function helixHome(options) {
|
|
124
172
|
const baseUrl = getBaseUrl();
|
|
125
|
-
|
|
126
|
-
const openArgs = ['open', baseUrl];
|
|
173
|
+
const openArgs = injectVisualConfig(['open', baseUrl], options.visual);
|
|
127
174
|
if (options.profile) {
|
|
128
175
|
openArgs.push(`--profile=${options.profile}`);
|
|
129
176
|
}
|
|
@@ -152,6 +199,25 @@ function helixDoctor(options) {
|
|
|
152
199
|
return helixHome(options);
|
|
153
200
|
}
|
|
154
201
|
|
|
202
|
+
async function runPassthroughWithVisual(tokens) {
|
|
203
|
+
let visual;
|
|
204
|
+
let stripped;
|
|
205
|
+
try {
|
|
206
|
+
({ tokens: stripped, visual } = parseVisualFlags(tokens));
|
|
207
|
+
} catch (e) {
|
|
208
|
+
console.error(`[${PACKAGE_JSON.name}] ${e.message}`);
|
|
209
|
+
return 3;
|
|
210
|
+
}
|
|
211
|
+
const withConfig = injectVisualConfig(stripped, visual);
|
|
212
|
+
if (shouldWrapWithVisual(stripped, visual)) {
|
|
213
|
+
return runWithVisualFeedback(stripped, visual, (args, opts) => {
|
|
214
|
+
const result = runPlaywrightCliPassthrough(args, opts);
|
|
215
|
+
return opts && opts.capture ? result.code : result;
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
return runPlaywrightCliPassthrough(withConfig);
|
|
219
|
+
}
|
|
220
|
+
|
|
155
221
|
/**
|
|
156
222
|
* @param {string[]} argv process.argv
|
|
157
223
|
* @returns {Promise<number>} exit code
|
|
@@ -164,7 +230,6 @@ async function main(argv = process.argv) {
|
|
|
164
230
|
return 0;
|
|
165
231
|
}
|
|
166
232
|
|
|
167
|
-
// 勿透传到底层引擎:否则 --version 会变成 @playwright/cli 的版本(如 0.1.x),易与本体版本混淆。
|
|
168
233
|
if (tokens[0] === '--version' || tokens[0] === '-v') {
|
|
169
234
|
console.log(PACKAGE_JSON.version);
|
|
170
235
|
return 0;
|
|
@@ -198,19 +263,17 @@ async function main(argv = process.argv) {
|
|
|
198
263
|
return 2;
|
|
199
264
|
}
|
|
200
265
|
|
|
201
|
-
return
|
|
266
|
+
return runPassthroughWithVisual(tokens);
|
|
202
267
|
}
|
|
203
268
|
|
|
204
269
|
main.printHelp = printHelp;
|
|
205
270
|
main.runPlaywrightCliPassthrough = runPlaywrightCliPassthrough;
|
|
206
271
|
main.playwrightCliEntry = playwrightCliEntry;
|
|
207
272
|
|
|
208
|
-
// 注意:`require.main === module` 入口守卫统一放在 helix-cli.js;
|
|
209
|
-
// 本文件仅导出,不在此处重复启动。
|
|
210
|
-
|
|
211
273
|
module.exports = {
|
|
212
274
|
main,
|
|
213
275
|
resolveOutputDir,
|
|
214
276
|
buildChildEnv,
|
|
215
277
|
DEFAULT_OUTPUT_DIR_NAME,
|
|
278
|
+
runPassthroughWithVisual,
|
|
216
279
|
};
|
package/lib/visual.js
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('node:fs');
|
|
4
|
+
const path = require('node:path');
|
|
5
|
+
|
|
6
|
+
const INIT_SCRIPT_PATH = path.join(__dirname, 'automation-overlay.init.js');
|
|
7
|
+
const GENERATED_CONFIG_PATH = path.join(__dirname, '.cli.runtime.generated.json');
|
|
8
|
+
|
|
9
|
+
/** 默认开启页面内视觉反馈;HELIX_VISUAL=0 可全局关闭 */
|
|
10
|
+
const DEFAULT_VISUAL_ENABLED = process.env.HELIX_VISUAL !== '0';
|
|
11
|
+
|
|
12
|
+
const VISUAL_COMMANDS = {
|
|
13
|
+
click: { pending: '正在点击', success: '已点击', pendingTone: 'pending', successTone: 'success' },
|
|
14
|
+
dblclick: { pending: '正在双击', success: '已双击', pendingTone: 'pending', successTone: 'success' },
|
|
15
|
+
fill: { pending: '正在填写', success: '已填写', pendingTone: 'pending', successTone: 'success' },
|
|
16
|
+
hover: { pending: '正在悬停', success: '已悬停', pendingTone: 'info', successTone: 'info' },
|
|
17
|
+
check: { pending: '正在勾选', success: '已勾选', pendingTone: 'pending', successTone: 'success' },
|
|
18
|
+
uncheck: { pending: '正在取消勾选', success: '已取消勾选', pendingTone: 'pending', successTone: 'info' },
|
|
19
|
+
select: { pending: '正在选择', success: '已选择', pendingTone: 'pending', successTone: 'success' },
|
|
20
|
+
drag: { pending: '正在拖拽', success: '已拖拽', pendingTone: 'pending', successTone: 'success' },
|
|
21
|
+
drop: { pending: '正在放置', success: '已放置', pendingTone: 'pending', successTone: 'success' },
|
|
22
|
+
upload: { hudOnly: true, pending: '正在上传', success: '已上传', pendingTone: 'pending', successTone: 'success' },
|
|
23
|
+
press: { hudOnly: true, pending: '按键', success: '按键完成', pendingTone: 'info', successTone: 'success' },
|
|
24
|
+
type: { hudOnly: true, pending: '正在输入', success: '输入完成', pendingTone: 'pending', successTone: 'success' },
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const SESSION_START_COMMANDS = new Set(['open', 'attach']);
|
|
28
|
+
|
|
29
|
+
function readInitScriptSource() {
|
|
30
|
+
return fs.readFileSync(INIT_SCRIPT_PATH, 'utf8');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function resolveGeneratedConfigPath() {
|
|
34
|
+
const payload = {
|
|
35
|
+
browser: {
|
|
36
|
+
initScript: [INIT_SCRIPT_PATH.replace(/\\/g, '/')],
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
fs.writeFileSync(GENERATED_CONFIG_PATH, JSON.stringify(payload, null, 2), 'utf8');
|
|
40
|
+
return GENERATED_CONFIG_PATH;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 从 argv 中提取并剥离 --visual / --no-visual / --visual-ms。
|
|
45
|
+
* @returns {{ tokens: string[], visual: { enabled: boolean, durationMs: number | null } }}
|
|
46
|
+
*/
|
|
47
|
+
function parseVisualFlags(tokens) {
|
|
48
|
+
const visual = {
|
|
49
|
+
enabled: DEFAULT_VISUAL_ENABLED,
|
|
50
|
+
durationMs: null,
|
|
51
|
+
};
|
|
52
|
+
const out = [];
|
|
53
|
+
for (let i = 0; i < tokens.length; i += 1) {
|
|
54
|
+
const t = tokens[i];
|
|
55
|
+
if (t === '--visual') {
|
|
56
|
+
visual.enabled = true;
|
|
57
|
+
} else if (t === '--no-visual') {
|
|
58
|
+
visual.enabled = false;
|
|
59
|
+
} else if (t === '--visual-ms') {
|
|
60
|
+
visual.durationMs = tokens[i + 1];
|
|
61
|
+
i += 1;
|
|
62
|
+
} else if (t.startsWith('--visual-ms=')) {
|
|
63
|
+
visual.durationMs = t.slice('--visual-ms='.length);
|
|
64
|
+
} else {
|
|
65
|
+
out.push(t);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (visual.durationMs != null) {
|
|
69
|
+
const n = Number(visual.durationMs);
|
|
70
|
+
if (!Number.isFinite(n) || n <= 0) {
|
|
71
|
+
throw Object.assign(new Error(`--visual-ms 需要正数,收到 ${JSON.stringify(visual.durationMs)}`), { code: 'E_BAD_ARG' });
|
|
72
|
+
}
|
|
73
|
+
visual.durationMs = Math.round(n);
|
|
74
|
+
}
|
|
75
|
+
return { tokens: out, visual };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function hasConfigFlag(tokens) {
|
|
79
|
+
return tokens.some((t) => t === '--config' || t.startsWith('--config='));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* open / attach 时自动注入 initScript 配置,使新页面预装视觉层。
|
|
84
|
+
*/
|
|
85
|
+
function injectVisualConfig(tokens, visual) {
|
|
86
|
+
if (!visual.enabled) return tokens;
|
|
87
|
+
const cmd = resolveCommandName(tokens);
|
|
88
|
+
if (!SESSION_START_COMMANDS.has(cmd) || hasConfigFlag(tokens)) {
|
|
89
|
+
return tokens;
|
|
90
|
+
}
|
|
91
|
+
const configPath = resolveGeneratedConfigPath();
|
|
92
|
+
return [...tokens, `--config=${configPath}`];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function resolveCommandName(tokens) {
|
|
96
|
+
let i = 0;
|
|
97
|
+
if (tokens[i] === '--raw') i += 1;
|
|
98
|
+
if (tokens[i] === '--json') i += 1;
|
|
99
|
+
if (tokens[i] && tokens[i].startsWith('-s=')) i += 1;
|
|
100
|
+
return tokens[i] || '';
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function isElementRef(token) {
|
|
104
|
+
if (!token || token.startsWith('-')) return false;
|
|
105
|
+
if (/^e\d+$/.test(token)) return true;
|
|
106
|
+
if (token.startsWith('"') || token.startsWith("'")) return true;
|
|
107
|
+
if (token.startsWith('#') || token.startsWith('.')) return true;
|
|
108
|
+
if (token.startsWith('getBy')) return true;
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 从命令参数中提取主要元素 target(用于高亮)。
|
|
114
|
+
*/
|
|
115
|
+
function extractElementTarget(tokens) {
|
|
116
|
+
const cmd = resolveCommandName(tokens);
|
|
117
|
+
if (!VISUAL_COMMANDS[cmd]) return null;
|
|
118
|
+
const def = VISUAL_COMMANDS[cmd];
|
|
119
|
+
if (def.hudOnly) return null;
|
|
120
|
+
|
|
121
|
+
let i = 0;
|
|
122
|
+
if (tokens[i] === '--raw') i += 1;
|
|
123
|
+
if (tokens[i] === '--json') i += 1;
|
|
124
|
+
if (tokens[i] && tokens[i].startsWith('-s=')) i += 1;
|
|
125
|
+
i += 1; // skip command name
|
|
126
|
+
|
|
127
|
+
const positional = [];
|
|
128
|
+
for (; i < tokens.length; i += 1) {
|
|
129
|
+
const t = tokens[i];
|
|
130
|
+
if (t.startsWith('--')) {
|
|
131
|
+
if (t.includes('=')) continue;
|
|
132
|
+
const next = tokens[i + 1];
|
|
133
|
+
if (next !== undefined && !next.startsWith('-')) i += 1;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
positional.push(t);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (cmd === 'drag' && positional.length >= 1) return positional[0];
|
|
140
|
+
if (cmd === 'fill' && positional.length >= 1) return positional[0];
|
|
141
|
+
if (cmd === 'select' && positional.length >= 1) return positional[0];
|
|
142
|
+
if (cmd === 'drop' && positional.length >= 1) return positional[0];
|
|
143
|
+
if (positional.length >= 1 && isElementRef(positional[0])) return positional[0];
|
|
144
|
+
if (positional.length >= 2 && isElementRef(positional[positional.length - 1])) {
|
|
145
|
+
return positional[positional.length - 1];
|
|
146
|
+
}
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function buildFlashEval(target, options) {
|
|
151
|
+
const optsLit = JSON.stringify(options);
|
|
152
|
+
return [
|
|
153
|
+
'eval',
|
|
154
|
+
`el => { const v = window.__helix_visual__; if (!v) return false; return v.flash(el, ${optsLit}); }`,
|
|
155
|
+
target,
|
|
156
|
+
];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function buildHudEval(options) {
|
|
160
|
+
const optsLit = JSON.stringify(options);
|
|
161
|
+
return ['eval', `() => { const v = window.__helix_visual__; if (!v) return false; return v.showHud(${optsLit}); }`];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function buildSetOptionsEval(visual) {
|
|
165
|
+
const payload = JSON.stringify({
|
|
166
|
+
enabled: visual.enabled,
|
|
167
|
+
durationMs: visual.durationMs,
|
|
168
|
+
});
|
|
169
|
+
return ['eval', `() => { const v = window.__helix_visual__; if (!v) return null; return v.setVisualOptions(${payload}); }`];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function buildInstallEval() {
|
|
173
|
+
const src = readInitScriptSource();
|
|
174
|
+
return ['eval', src];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function buildBridgeCheckEval() {
|
|
178
|
+
return ['eval', '--raw', 'Boolean(window.__helix_visual__)'];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function shouldWrapWithVisual(tokens, visual) {
|
|
182
|
+
if (!visual.enabled) return false;
|
|
183
|
+
const cmd = resolveCommandName(tokens);
|
|
184
|
+
return Boolean(VISUAL_COMMANDS[cmd]);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* 在元素交互命令前后注入视觉反馈。
|
|
189
|
+
* @param {string[]} tokens 已剥离 visual flags 的参数
|
|
190
|
+
* @param {{ enabled: boolean, durationMs: number | null }} visual
|
|
191
|
+
* @param {(args: string[], opts?: object) => number} runPassthrough
|
|
192
|
+
*/
|
|
193
|
+
async function runWithVisualFeedback(tokens, visual, runPassthrough) {
|
|
194
|
+
const cmd = resolveCommandName(tokens);
|
|
195
|
+
const def = VISUAL_COMMANDS[cmd];
|
|
196
|
+
const target = extractElementTarget(tokens);
|
|
197
|
+
|
|
198
|
+
const runCapture = (args) => runPassthrough(args, { capture: true });
|
|
199
|
+
|
|
200
|
+
const check = runCapture(buildBridgeCheckEval());
|
|
201
|
+
if (String(check.stdout || '').trim() !== 'true') {
|
|
202
|
+
runCapture(buildInstallEval());
|
|
203
|
+
}
|
|
204
|
+
runCapture(buildSetOptionsEval(visual));
|
|
205
|
+
|
|
206
|
+
const hudBase = { action: cmd, target: target || '', status: 'pending' };
|
|
207
|
+
runCapture(buildHudEval({ ...hudBase, detail: def.pending }));
|
|
208
|
+
|
|
209
|
+
if (target) {
|
|
210
|
+
runCapture(buildFlashEval(target, {
|
|
211
|
+
tone: def.pendingTone || 'pending',
|
|
212
|
+
label: def.pending,
|
|
213
|
+
durationMs: visual.durationMs,
|
|
214
|
+
}));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const code = runPassthrough(tokens);
|
|
218
|
+
|
|
219
|
+
const successTone = code === 0 ? (def.successTone || 'success') : 'danger';
|
|
220
|
+
const successLabel = code === 0 ? def.success : '操作失败';
|
|
221
|
+
runCapture(buildSetOptionsEval(visual));
|
|
222
|
+
if (target) {
|
|
223
|
+
runCapture(buildFlashEval(target, {
|
|
224
|
+
tone: successTone,
|
|
225
|
+
label: successLabel,
|
|
226
|
+
durationMs: visual.durationMs,
|
|
227
|
+
}));
|
|
228
|
+
}
|
|
229
|
+
runCapture(buildHudEval({
|
|
230
|
+
action: cmd,
|
|
231
|
+
target: target || '',
|
|
232
|
+
status: code === 0 ? 'success' : 'danger',
|
|
233
|
+
detail: successLabel,
|
|
234
|
+
}));
|
|
235
|
+
|
|
236
|
+
return code;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
module.exports = {
|
|
240
|
+
INIT_SCRIPT_PATH,
|
|
241
|
+
VISUAL_COMMANDS,
|
|
242
|
+
DEFAULT_VISUAL_ENABLED,
|
|
243
|
+
parseVisualFlags,
|
|
244
|
+
injectVisualConfig,
|
|
245
|
+
shouldWrapWithVisual,
|
|
246
|
+
runWithVisualFeedback,
|
|
247
|
+
resolveGeneratedConfigPath,
|
|
248
|
+
};
|