lantern-harness 0.1.9 → 0.1.11

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.
Files changed (181) hide show
  1. package/CHANGELOG.md +90 -0
  2. package/README.md +6 -6
  3. package/builtin/session/lh-fs.mjs +32 -14
  4. package/dist/500.html +258 -22
  5. package/dist/_astro/Base.Bgxs1QQl.css +1 -0
  6. package/dist/_astro/Base.astro_astro_type_script_index_0_lang.CvbRi7Tl.js +1 -0
  7. package/dist/_astro/CaseExecBoard.Dttqp0wg.js +1 -0
  8. package/dist/_astro/CaseGenerateApp.DqW_nWMc.js +9 -0
  9. package/dist/_astro/CasesLibrary.8aMUUGp_.js +1 -0
  10. package/dist/_astro/ChatApp.BLxf0h1d.js +219 -0
  11. package/dist/_astro/ColumnResize.BEkEW7qw.js +1 -0
  12. package/dist/_astro/{ConfirmDialog.CBVsDJ8f.js → ConfirmDialog.BZzZWDci.js} +1 -1
  13. package/dist/_astro/{DashboardBoard.DCtfrrJz.js → DashboardBoard.CkSbhEDI.js} +3 -3
  14. package/dist/_astro/DatePicker.Bkl71M49.js +1 -0
  15. package/dist/_astro/DocsFileBrowser.MKVCfZrE.js +1 -0
  16. package/dist/_astro/DocsPager.DJY4foYL.js +1 -0
  17. package/dist/_astro/{DrawerField.CWMLhcPX.js → DrawerField.CHRg77LG.js} +1 -1
  18. package/dist/_astro/{ExcelFieldTags.B_XMwNNx.js → ExcelFieldTags.DXMeaDa5.js} +4 -4
  19. package/dist/_astro/KnowledgeLibrary.B-p1aoQN.js +1 -0
  20. package/dist/_astro/NamePromptDialog.Wa9O530a.js +1 -0
  21. package/dist/_astro/NavLibraries.C6LImMO_.js +1 -0
  22. package/dist/_astro/OverflowHint.D6v4Uj2N.js +1 -0
  23. package/dist/_astro/PluginsPage.-97n3x2U.js +39 -0
  24. package/dist/_astro/PluginsPage.DkmRPfbU.js +1 -0
  25. package/dist/_astro/PmBugsBoard.CMT4-eoB.js +1 -0
  26. package/dist/_astro/PmIterationsBoard.BQM_dzdZ.js +1 -0
  27. package/dist/_astro/PmProjectDetail.CzU80pat.js +1 -0
  28. package/dist/_astro/PmProjectsBoard.Cxsa7SYQ.js +1 -0
  29. package/dist/_astro/PmRequirementsBoard.4_JMDDN0.js +1 -0
  30. package/dist/_astro/PmRequirementsBoard.B8HL07cw.js +1 -0
  31. package/dist/_astro/PmShared.Ca2Ao7v_.js +1 -0
  32. package/dist/_astro/PromptsLibrary.C9BcIxKK.js +1 -0
  33. package/dist/_astro/ReportsPage.Fh5DRKLT.js +1 -0
  34. package/dist/_astro/RequirementsLibrary.CBkuWE8X.js +1 -0
  35. package/dist/_astro/SearchableSelect.DnjoSslK.js +1 -0
  36. package/dist/_astro/SelectDropdown.CuuDHa8s.js +1 -0
  37. package/dist/_astro/SettingsDialog.CVB5Gtk4.js +1 -0
  38. package/dist/_astro/SlideOverDrawer.B7B5Wu8I.js +1 -0
  39. package/dist/_astro/{SpaceSwitcher.CI3KDBOA.js → SpaceSwitcher.BVQLt_tL.js} +2 -2
  40. package/dist/_astro/TagSelect.CuN5RdF4.js +1 -0
  41. package/dist/_astro/TasksBoard.Bu-fpIZ7.js +1 -0
  42. package/dist/_astro/TemplatesLibrary.DSylQGlN.js +1 -0
  43. package/dist/_astro/TestStatsBoard.CPyKW4wc.js +1 -0
  44. package/dist/_astro/Toast.R8_2OWRX.js +1 -0
  45. package/dist/_astro/TodosBoard.8kvRTEW-.js +1 -0
  46. package/dist/_astro/UiAutoBoard.DFTiDqCp.js +9 -0
  47. package/dist/_astro/active-element-history.CED-m1Zp.js +1 -0
  48. package/dist/_astro/app-config.BUPzGl9f.js +1 -0
  49. package/dist/_astro/{client.Be_SakrB.js → client.BHNTqLjv.js} +3 -3
  50. package/dist/_astro/dialog.BAPTk9a_.js +1 -0
  51. package/dist/_astro/hydrate-cache.BXX604Bw.js +1 -0
  52. package/dist/_astro/{i18n.B52gLzii.js → i18n.BGPnP1Bg.js} +1 -1
  53. package/dist/_astro/index.astro_astro_type_script_index_0_lang.LPL9mPQY.js +1 -0
  54. package/dist/_astro/jsx-runtime.DUg_B_vD.js +1 -0
  55. package/dist/_astro/{lib.DJNDi_Ht.js → lib.CgXLzMMd.js} +15 -15
  56. package/dist/_astro/listbox.C96OCanh.js +1 -0
  57. package/dist/_astro/markdown-client.DRva7DS9.js +58 -0
  58. package/dist/_astro/menu.IPWdRoNM.js +1 -0
  59. package/dist/_astro/{open-docs-in-session.Xg3OSOfc.js → open-docs-in-session.CyFNgh1I.js} +1 -1
  60. package/dist/_astro/{plugins.BDZqJNOy.js → plugins.C2BMfzv1.js} +1 -1
  61. package/dist/_astro/{pm.Bl3hz9fF.js → pm.CagBA-ht.js} +2 -2
  62. package/dist/_astro/{portal.Cm9XlIPf.js → portal.C0hAhXDF.js} +2 -2
  63. package/dist/_astro/prompts-meta.Bua-j14I.js +2 -0
  64. package/dist/_astro/react-dom.DLW9pGCF.js +1 -0
  65. package/dist/_astro/{react-dom.CqAxpHMt.js → react.Blvh0GOs.js} +1 -1
  66. package/dist/_astro/report-prompt.CAfZ6RIo.js +1 -0
  67. package/dist/_astro/{settings.C6NWuiFO.js → settings.Ceefeil5.js} +2 -2
  68. package/dist/_astro/{spaces-client.k_ciWMai.js → spaces-client.BocUabwy.js} +1 -1
  69. package/dist/_astro/ui-auto-prompt.CX8fju7r.js +3 -0
  70. package/dist/_astro/ui-auto.BmC9JJ_X.js +1 -0
  71. package/dist/_astro/update.CtF8vsag.js +1 -0
  72. package/dist/_astro/use-text-value.DBQvHeNl.js +1 -0
  73. package/dist/_astro/use-tree-walker.DFU2AaAP.js +1 -0
  74. package/dist/_astro/useListAnim.BoerpmsB.js +1 -0
  75. package/dist/_astro/{view-prefs.Dyu_lmN2.js → view-prefs.BEAZRcxF.js} +1 -1
  76. package/dist/case-exec/index.html +458 -22
  77. package/dist/cases/generate/index.html +258 -22
  78. package/dist/cases/library/index.html +458 -22
  79. package/dist/dashboard/index.html +458 -22
  80. package/dist/docs/changelog/index.html +373 -23
  81. package/dist/docs/install/index.html +258 -22
  82. package/dist/docs/manual/index.html +314 -35
  83. package/dist/index.html +2 -281
  84. package/dist/knowledge/index.html +458 -22
  85. package/dist/knowledge//345/212/237/350/203/275/346/265/213/350/257/225/347/224/250/344/276/213/347/274/226/345/206/231/350/247/204/350/214/203/index.html +258 -22
  86. package/dist/plugins/index.html +258 -22
  87. package/dist/pm/bugs/index.html +458 -22
  88. package/dist/pm/iterations/index.html +458 -22
  89. package/dist/pm/projects/detail/index.html +458 -22
  90. package/dist/pm/projects/index.html +458 -22
  91. package/dist/pm/requirements/index.html +458 -22
  92. package/dist/prompts/index.html +458 -22
  93. package/dist/reports/index.html +458 -22
  94. package/dist/requirements/index.html +458 -22
  95. package/dist/tasks/index.html +458 -22
  96. package/dist/templates/index.html +458 -22
  97. package/dist/templates//345/212/237/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212//345/212/237/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212/346/250/241/346/235/277/index.html +258 -22
  98. package/dist/templates//346/200/247/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212//346/200/247/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212/346/250/241/346/235/277/index.html +258 -22
  99. package/dist/templates//346/200/247/350/203/275/346/265/213/350/257/225/347/224/250/344/276/213//346/200/247/350/203/275/346/265/213/350/257/225/347/224/250/344/276/213/346/250/241/346/235/277/index.html +258 -22
  100. package/dist/templates//346/216/245/345/217/243/346/265/213/350/257/225/346/212/245/345/221/212//346/216/245/345/217/243/346/265/213/350/257/225/346/212/245/345/221/212/346/250/241/346/235/277/index.html +258 -22
  101. package/dist/templates//350/207/252/345/212/250/345/214/226/346/265/213/350/257/225/347/224/250/344/276/213//350/207/252/345/212/250/345/214/226/346/265/213/350/257/225/347/224/250/344/276/213/346/250/241/346/235/277/index.html +258 -22
  102. package/dist/test-automation/index.html +717 -0
  103. package/dist/test-stats/index.html +458 -22
  104. package/dist/todos/index.html +458 -22
  105. package/dist/workbench/index.html +517 -0
  106. package/docs//346/223/215/344/275/234/346/211/213/345/206/214.md +80 -43
  107. package/lh-plugins/lh-project-docs.mjs +14 -0
  108. package/lh-plugins/lh-uiauto.mjs +1445 -386
  109. package/lh-plugins/plugin-prompts.mjs +200 -0
  110. package/lh-plugins/storage-domain.mjs +6 -0
  111. package/lh-plugins/uiauto-adapters/index.mjs +57 -0
  112. package/lh-plugins/uiauto-adapters/playwright.mjs +215 -0
  113. package/lh-plugins/uiauto-adapters/pytest.mjs +151 -0
  114. package/lh-plugins/uiauto-adapters/selenium.mjs +163 -0
  115. package/lh-plugins/uiauto-adapters/shared.mjs +371 -0
  116. package/lh-plugins/uiauto-adapters/tasks.mjs +369 -0
  117. package/lh-plugins/uiauto-explorer/apps.mjs +332 -0
  118. package/lh-plugins/uiauto-explorer/diff.mjs +159 -0
  119. package/lh-plugins/uiauto-explorer/emit-files.mjs +506 -0
  120. package/lh-plugins/uiauto-explorer/emit-python.mjs +160 -0
  121. package/lh-plugins/uiauto-explorer/emit-ts.mjs +118 -0
  122. package/lh-plugins/uiauto-explorer/emit.mjs +15 -0
  123. package/lh-plugins/uiauto-explorer/ir.mjs +114 -0
  124. package/lh-plugins/uiauto-explorer/locators.mjs +267 -0
  125. package/lh-plugins/uiauto-explorer/merge.mjs +235 -0
  126. package/lh-plugins/uiauto-explorer/plan.mjs +165 -0
  127. package/lh-plugins/uiauto-explorer/runner.mjs +370 -0
  128. package/lh-plugins/uiauto-explorer/sessions.mjs +1373 -0
  129. package/lh-plugins/uiauto-explorer/snapshot.mjs +492 -0
  130. package/lh-plugins/uiauto-prompt.mjs +122 -0
  131. package/lh-plugins/uiauto-report.mjs +111 -0
  132. package/package.json +1 -1
  133. package/dist/_astro/Base.4X_wsSoR.css +0 -1
  134. package/dist/_astro/Base.astro_astro_type_script_index_0_lang.Dy7fJoxu.js +0 -1
  135. package/dist/_astro/CaseExecBoard.DKdfW5fM.js +0 -1
  136. package/dist/_astro/CaseGenerateApp.Bm8AyH9s.js +0 -9
  137. package/dist/_astro/CasesLibrary.CvVJZ74h.js +0 -1
  138. package/dist/_astro/ChatApp.BS-wLBRa.js +0 -276
  139. package/dist/_astro/ColumnResize.Bs6GL3DJ.js +0 -1
  140. package/dist/_astro/DatePicker.CMfX7F6z.js +0 -1
  141. package/dist/_astro/DocsFileBrowser.BtRoZ787.js +0 -1
  142. package/dist/_astro/DocsPager.C20ue_JZ.js +0 -1
  143. package/dist/_astro/KnowledgeLibrary.CV4DyStj.js +0 -1
  144. package/dist/_astro/NamePromptDialog.Dl5kSnQI.js +0 -1
  145. package/dist/_astro/NavLibraries.DmD2lZBZ.js +0 -1
  146. package/dist/_astro/OverflowHint.lzmplP5j.js +0 -1
  147. package/dist/_astro/PluginsPage.B59AMDrB.js +0 -1
  148. package/dist/_astro/PluginsPage.BEAPymNL.js +0 -39
  149. package/dist/_astro/PmBugsBoard.CPEyBp0l.js +0 -1
  150. package/dist/_astro/PmIterationsBoard.zCeLpcBo.js +0 -1
  151. package/dist/_astro/PmProjectDetail.ejIam5sD.js +0 -1
  152. package/dist/_astro/PmProjectsBoard.DbXzir8o.js +0 -1
  153. package/dist/_astro/PmRequirementsBoard.CynewRfv.js +0 -1
  154. package/dist/_astro/PmRequirementsBoard.D9eUMJWI.js +0 -1
  155. package/dist/_astro/PmShared.BxTHKdjY.js +0 -1
  156. package/dist/_astro/PromptsLibrary.Bkruu80b.js +0 -1
  157. package/dist/_astro/ReportsPage.BoBQshMQ.js +0 -1
  158. package/dist/_astro/RequirementsLibrary.BC0A8qdk.js +0 -1
  159. package/dist/_astro/SearchableSelect.CXa1hiDd.js +0 -1
  160. package/dist/_astro/SelectDropdown.Buv7rQJ_.js +0 -1
  161. package/dist/_astro/SettingsDialog.Dc-9QNJI.js +0 -1
  162. package/dist/_astro/SlideOverDrawer.TPbGEoWn.js +0 -1
  163. package/dist/_astro/TagSelect.u8DTop7v.js +0 -1
  164. package/dist/_astro/TasksBoard.Bb--oVV8.js +0 -1
  165. package/dist/_astro/TemplatesLibrary.D1wZq94E.js +0 -1
  166. package/dist/_astro/TestStatsBoard.DlVezlXl.js +0 -1
  167. package/dist/_astro/TodosBoard.S84wqcWY.js +0 -1
  168. package/dist/_astro/UiAutoBoard.BKp9ifxg.js +0 -1
  169. package/dist/_astro/active-element-history.CeefGkFQ.js +0 -1
  170. package/dist/_astro/app-config.DUyozIL9.js +0 -1
  171. package/dist/_astro/dialog.DXHznyjD.js +0 -1
  172. package/dist/_astro/jsx-runtime.BUXbF_vF.js +0 -1
  173. package/dist/_astro/listbox.Di-t5U37.js +0 -1
  174. package/dist/_astro/menu.Bkluqxic.js +0 -1
  175. package/dist/_astro/prompts-meta.F4wwkjUA.js +0 -2
  176. package/dist/_astro/report-prompt.Cmw4XzfY.js +0 -1
  177. package/dist/_astro/update.Bl_HfSe6.js +0 -1
  178. package/dist/_astro/use-text-value.BuY0Cm1z.js +0 -1
  179. package/dist/_astro/use-tree-walker.CfTHFX43.js +0 -1
  180. package/dist/_astro/useListAnim.CpGB1cft.js +0 -1
  181. package/dist/ui-automation/index.html +0 -281
@@ -0,0 +1,369 @@
1
+ /**
2
+ * 「运行任务」发现。
3
+ *
4
+ * 对齐 Jenkins 的参数化构建:项目里本来就声明好的跑法(`package.json` 的 scripts、
5
+ * `Makefile` 的 target、pytest 的 markers、Maven 的 profiles)直接整理成下拉候选,
6
+ * 而不是替用户猜一条命令。纯函数,方便单测。
7
+ *
8
+ * 每个任务统一成:
9
+ * { id, label, display, base, args, hint, kind, failureFlags, custom }
10
+ * - `base` + `args` 拼出实际命令(走 shell 执行)
11
+ * - `failureFlags` 表示这条任务能不能追加「仅上次失败 / 遇到失败即停」
12
+ */
13
+ import fs from 'node:fs';
14
+ import path from 'node:path';
15
+
16
+ import { text } from './shared.mjs';
17
+
18
+ export const MAX_RUN_TASKS = 20;
19
+ export const CUSTOM_TASK_LIMIT = 20;
20
+
21
+ /** 要跑哪些 script:名字像测试的;排除 lint / format / 打开报告这类 */
22
+ const NODE_SCRIPT_ALLOW =
23
+ /^(test|tests|e2e|ui|api|smoke|regression|edge|sanity|spec|allure|report)([:._-].*)?$/i;
24
+ const NODE_SCRIPT_DENY =
25
+ /(lint|format|prepare|husky|install|build|serve|start|dev|watch|debug|open|show|clean|postinstall|pretest)/i;
26
+ /** 交互式命令挂后台会一直不退出,直接跳过 */
27
+ const INTERACTIVE = /(--ui\b|--debug\b|--watch\b|show-report|allure open|--headed\b)/;
28
+
29
+ /** Makefile 里要的目标 / 明显不是测试的目标 */
30
+ const MAKE_ALLOW = /(test|spec|smoke|regress|e2e|sanity|unit|contract|integration|api|ui|p0|p1)/i;
31
+ const MAKE_DENY =
32
+ /^(all|help|install|build|clean|lint|format|format-check|type|check|deps|lock|docker|dev|run|serve|export|version|pre-commit)$/i;
33
+
34
+ const PYTEST_TASK_MARKER = /^[\w.:-]+$/;
35
+
36
+ export function quoteIfNeeded(value) {
37
+ const raw = String(value ?? '');
38
+ return /\s/.test(raw) ? `"${raw.replace(/"/g, '\\"')}"` : raw;
39
+ }
40
+
41
+ /** 包管理器的 `run`:pnpm / yarn / npm,未知按 npm */
42
+ export function packageManagerRunner(packageManager) {
43
+ if (packageManager === 'pnpm') return { command: 'pnpm', args: ['run'] };
44
+ if (packageManager === 'yarn') return { command: 'yarn', args: ['run'] };
45
+ return { command: 'npm', args: ['run'] };
46
+ }
47
+
48
+ /** package.json 里哪些 script 值得放出来 */
49
+ export function pickRunScripts(scripts) {
50
+ const entries = scripts && typeof scripts === 'object' ? Object.entries(scripts) : [];
51
+ const picked = [];
52
+ for (const [name, body] of entries) {
53
+ if (!NODE_SCRIPT_ALLOW.test(name)) continue;
54
+ if (NODE_SCRIPT_DENY.test(name)) continue;
55
+ if (typeof body !== 'string') continue;
56
+ if (INTERACTIVE.test(body)) continue;
57
+ picked.push({ name, body });
58
+ }
59
+ // test 永远是第一个,其余按名字排序,避免每次刷新顺序都变
60
+ picked.sort((a, b) => {
61
+ if (a.name === 'test') return -1;
62
+ if (b.name === 'test') return 1;
63
+ return a.name.localeCompare(b.name);
64
+ });
65
+ return picked;
66
+ }
67
+
68
+ export function discoverNodeScripts(probe) {
69
+ const runner = packageManagerRunner(probe.packageManager);
70
+ return pickRunScripts(probe.pkg?.scripts).map(({ name, body }) => {
71
+ const isPlaywright = /playwright/i.test(body);
72
+ const label = `${runner.command} run ${name}`;
73
+ return {
74
+ id: `script:${name}`,
75
+ label,
76
+ display: name,
77
+ base: label,
78
+ // npm / pnpm / yarn 都能把 `--` 之后的参数透传给脚本;
79
+ // Playwright 脚本追加 reporters 才拿得到 JSON 汇总
80
+ args: isPlaywright ? ['--', '--reporter=list,json,html'] : [],
81
+ hint: body.trim().slice(0, 120),
82
+ kind: 'script',
83
+ failureFlags: isPlaywright,
84
+ custom: false,
85
+ };
86
+ });
87
+ }
88
+
89
+ /** 解析 Makefile:`target: ## 说明`,只取像测试的目标 */
90
+ export function parseMakeTargets(raw) {
91
+ const text_ = String(raw ?? '');
92
+ const out = [];
93
+ const seen = new Set();
94
+ for (const line of text_.split(/\r?\n/)) {
95
+ const match = /^([A-Za-z0-9][\w.-]*)\s*:(?!=)/.exec(line);
96
+ if (!match) continue;
97
+ const name = match[1];
98
+ if (seen.has(name)) continue;
99
+ if (!MAKE_ALLOW.test(name) || MAKE_DENY.test(name)) continue;
100
+ seen.add(name);
101
+ const hint = /##\s*(.+)$/.exec(line)?.[1]?.trim() ?? '';
102
+ out.push({ name, hint });
103
+ }
104
+ return out;
105
+ }
106
+
107
+ export function discoverMakeTargets(probe) {
108
+ const makefile = probe.readRel('Makefile', 64 * 1024) || probe.readRel('makefile', 64 * 1024);
109
+ if (!makefile) return [];
110
+ return parseMakeTargets(makefile).map(({ name, hint }) => ({
111
+ id: `make:${name}`,
112
+ label: `make ${name}`,
113
+ display: name,
114
+ base: `make ${name}`,
115
+ args: [],
116
+ hint,
117
+ kind: 'make',
118
+ // make 目标里怎么跑由项目自己决定,不往里追加参数
119
+ failureFlags: false,
120
+ custom: false,
121
+ }));
122
+ }
123
+
124
+ /** pytest markers:`[tool.pytest.ini_options] markers = [...]` 或 pytest.ini 的 `markers =` */
125
+ export function parsePytestMarkers({ pyproject = '', ini = '' } = {}) {
126
+ const out = [];
127
+ const seen = new Set();
128
+ const push = (name, hint) => {
129
+ const marker = name.trim();
130
+ if (!marker || seen.has(marker) || !PYTEST_TASK_MARKER.test(marker)) return;
131
+ seen.add(marker);
132
+ out.push({ name: marker, hint: hint.trim().slice(0, 80) });
133
+ };
134
+
135
+ // pyproject:markers = ["smoke: 核心主流程", ...](可能跨行)
136
+ const block = /\[tool\.pytest\.ini_options\]([\s\S]*?)(?:\n\[|$)/.exec(pyproject)?.[1] ?? '';
137
+ const arrayMatch = /markers\s*=\s*\[([\s\S]*?)\]/.exec(block);
138
+ if (arrayMatch) {
139
+ for (const item of arrayMatch[1].matchAll(/["']([^"']+)["']/g)) {
140
+ const [name, ...rest] = item[1].split(':');
141
+ push(name, rest.join(':'));
142
+ }
143
+ }
144
+
145
+ // pytest.ini / setup.cfg:markers = 后每行 `name: 说明`
146
+ const iniBlock = /(?:^|\n)markers\s*=\s*\n((?:[ \t]+.+\n?)+)/.exec(ini)?.[1] ?? '';
147
+ for (const line of iniBlock.split(/\r?\n/)) {
148
+ const trimmed = line.trim();
149
+ if (!trimmed) continue;
150
+ const [name, ...rest] = trimmed.split(':');
151
+ push(name, rest.join(':'));
152
+ }
153
+ return out;
154
+ }
155
+
156
+ /**
157
+ * Python 解释器:与 pytest 适配器同一套顺序(虚拟环境 → uv / poetry → python3),
158
+ * 这里单独实现一份,免得 tasks 反向依赖适配器。
159
+ */
160
+ export function detectPythonCommand(dir) {
161
+ const win = process.platform === 'win32';
162
+ const venv = win
163
+ ? ['.venv/Scripts/python.exe', 'venv/Scripts/python.exe']
164
+ : ['.venv/bin/python', 'venv/bin/python', '.venv/bin/python3', 'venv/bin/python3'];
165
+ for (const rel of venv) {
166
+ if (fs.existsSync(path.join(dir, rel))) return `${quoteIfNeeded(rel)} -m pytest`;
167
+ }
168
+ if (fs.existsSync(path.join(dir, 'uv.lock'))) return 'uv run python -m pytest';
169
+ if (fs.existsSync(path.join(dir, 'poetry.lock'))) return 'poetry run python -m pytest';
170
+ return `${win ? 'python' : 'python3'} -m pytest`;
171
+ }
172
+
173
+ export function discoverPytestMarkers(probe) {
174
+ const markers = parsePytestMarkers({
175
+ pyproject: probe.readRel('pyproject.toml', 128 * 1024),
176
+ ini: probe.readRel('pytest.ini', 64 * 1024) || probe.readRel('setup.cfg', 64 * 1024),
177
+ });
178
+ const interpreter = detectPythonCommand(probe.dir);
179
+ return markers.map(({ name, hint }) => ({
180
+ id: `marker:${name}`,
181
+ label: `pytest -m ${name}`,
182
+ display: name,
183
+ base: interpreter,
184
+ args: ['-m', name],
185
+ hint,
186
+ kind: 'pytest',
187
+ failureFlags: true,
188
+ custom: false,
189
+ }));
190
+ }
191
+
192
+ /** Maven profiles:`mvn test -P<id>` */
193
+ export function parseMavenProfiles(raw) {
194
+ const out = [];
195
+ const seen = new Set();
196
+ const xml = String(raw ?? '');
197
+ for (const block of xml.matchAll(/<profile>([\s\S]*?)<\/profile>/g)) {
198
+ const id = /<id>\s*([^<]+?)\s*<\/id>/.exec(block[1])?.[1]?.trim();
199
+ if (!id || seen.has(id)) continue;
200
+ // `-P` 只接受简单标识,带空格 / 中文的 profile 名跳过
201
+ if (!/^[\w.-]+$/.test(id)) continue;
202
+ seen.add(id);
203
+ out.push({ id });
204
+ }
205
+ return out;
206
+ }
207
+
208
+ export function discoverMavenTasks(probe) {
209
+ const pom = probe.readRel('pom.xml', 256 * 1024);
210
+ if (!pom) return [];
211
+ const mvn = fs.existsSync(path.join(probe.dir, 'mvnw')) ? './mvnw' : 'mvn';
212
+ return parseMavenProfiles(pom).map(({ id }) => ({
213
+ id: `profile:${id}`,
214
+ label: `${mvn} test -P${id}`,
215
+ display: id,
216
+ base: `${mvn} test`,
217
+ args: [`-P${id}`],
218
+ hint: `Maven profile ${id}`,
219
+ kind: 'maven',
220
+ failureFlags: false,
221
+ custom: false,
222
+ }));
223
+ }
224
+
225
+ /**
226
+ * 一个项目能跑的「任务」清单(不含「全量运行」,那项固定排第一,由调用方补)。
227
+ * @param {object} probe buildProbe 的结果
228
+ * @param {string} framework playwright / pytest / selenium
229
+ */
230
+ export function discoverRunTasks(probe, framework) {
231
+ if (!probe?.exists) return [];
232
+ let tasks = [];
233
+ if (framework === 'pytest') {
234
+ tasks = [...discoverMakeTargets(probe), ...discoverPytestMarkers(probe)];
235
+ } else if (framework === 'selenium' && (probe.hasPom || probe.hasGradle)) {
236
+ tasks = discoverMavenTasks(probe);
237
+ } else {
238
+ tasks = discoverNodeScripts(probe);
239
+ }
240
+ // 同一个 label 只留第一条(Makefile 和 marker 常常同名)
241
+ const seen = new Set();
242
+ return tasks
243
+ .filter((task) => {
244
+ if (seen.has(task.label)) return false;
245
+ seen.add(task.label);
246
+ return true;
247
+ })
248
+ .slice(0, MAX_RUN_TASKS);
249
+ }
250
+
251
+ /** 自定义任务:只保留名称 + 命令,走 shell 执行(名称为空时下拉里直接显示命令) */
252
+ export function normalizeCustomTasks(raw) {
253
+ if (!Array.isArray(raw)) return [];
254
+ const out = [];
255
+ for (const item of raw.slice(0, CUSTOM_TASK_LIMIT)) {
256
+ if (!item || typeof item !== 'object') continue;
257
+ const command = text(item.command, 1000).trim();
258
+ if (!command) continue;
259
+ const name = text(item.name, 60).trim();
260
+ const id = text(item.id, 64) || `ut_${out.length}_${Math.random().toString(36).slice(2, 8)}`;
261
+ out.push({ id, name, command });
262
+ }
263
+ return out;
264
+ }
265
+
266
+ /** 自定义任务 → 下拉里的任务(base 就是用户写的命令本身) */
267
+ export function customToTasks(customTasks) {
268
+ return normalizeCustomTasks(customTasks).map(({ id, name, command }) => ({
269
+ id,
270
+ // 有名称显示「名称(命令)」,没名称就直接显示命令
271
+ label: name ? `${name}(${command})` : command,
272
+ display: name || command,
273
+ base: command,
274
+ args: [],
275
+ hint: name ? command : '',
276
+ kind: 'custom',
277
+ failureFlags: false,
278
+ custom: true,
279
+ }));
280
+ }
281
+
282
+ /** 用户对自动发现任务的改动(名称 / 命令),按任务 id 存 */
283
+ export function normalizeTaskOverrides(raw) {
284
+ if (!Array.isArray(raw)) return [];
285
+ const out = [];
286
+ for (const item of raw.slice(0, MAX_RUN_TASKS)) {
287
+ if (!item || typeof item !== 'object') continue;
288
+ const id = text(item.id, 64);
289
+ if (!id || id === 'full') continue;
290
+ out.push({
291
+ id,
292
+ name: text(item.name, 60).trim(),
293
+ command: text(item.command, 1000).trim(),
294
+ });
295
+ }
296
+ return out;
297
+ }
298
+
299
+ /** 隐藏掉的自动发现任务 id(用户在运行配置里删掉的) */
300
+ export function normalizeHiddenTaskIds(raw) {
301
+ if (!Array.isArray(raw)) return [];
302
+ const seen = new Set();
303
+ const out = [];
304
+ for (const item of raw) {
305
+ const id = text(item, 64);
306
+ if (!id || id === 'full' || seen.has(id)) continue;
307
+ seen.add(id);
308
+ out.push(id);
309
+ }
310
+ return out.slice(0, MAX_RUN_TASKS);
311
+ }
312
+
313
+ /**
314
+ * 把用户对自动发现任务的改动套上去:
315
+ * - 删掉的直接不返回;
316
+ * - 改过命令的换成原样的 shell 命令,不再追加框架参数(`--lf` 之类塞给自定义命令没意义);
317
+ * - 改过名称的只换显示名,命令保持原样。
318
+ */
319
+ export function applyTaskOverrides(tasks, overrides, hiddenIds) {
320
+ const edits = new Map(normalizeTaskOverrides(overrides).map((item) => [item.id, item]));
321
+ const hidden = new Set(normalizeHiddenTaskIds(hiddenIds));
322
+ const out = [];
323
+ for (const task of tasks) {
324
+ if (hidden.has(task.id)) continue;
325
+ const edit = edits.get(task.id);
326
+ if (!edit) {
327
+ out.push(task);
328
+ continue;
329
+ }
330
+ const command = edit.command || taskCommandLine(task);
331
+ const name = edit.name || task.display || '';
332
+ out.push({
333
+ ...task,
334
+ base: command,
335
+ args: [],
336
+ failureFlags: false,
337
+ display: name,
338
+ label: name ? `${name}(${command})` : command,
339
+ hint: edit.name ? command : task.hint,
340
+ edited: true,
341
+ });
342
+ }
343
+ return out;
344
+ }
345
+
346
+ /** 任务最终执行的命令行(shell 用) */
347
+ export function taskCommandLine(task, flags = {}) {
348
+ const parts = [task.base, ...(task.args ?? [])];
349
+ if (task.failureFlags) {
350
+ if (flags.lastFailed) parts.push(task.kind === 'pytest' ? '--lf' : '--last-failed');
351
+ if (flags.bail) parts.push(task.kind === 'pytest' ? '-x' : '--max-failures=1');
352
+ }
353
+ return parts.filter(Boolean).join(' ');
354
+ }
355
+
356
+ /** 给前端看的任务(去掉执行细节,只留能显示 / 能选的部分) */
357
+ export function taskView(task) {
358
+ return {
359
+ id: task.id,
360
+ label: task.label,
361
+ display: task.display,
362
+ hint: task.hint,
363
+ kind: task.kind,
364
+ failureFlags: task.failureFlags,
365
+ custom: task.custom,
366
+ edited: Boolean(task.edited),
367
+ command: taskCommandLine(task),
368
+ };
369
+ }
@@ -0,0 +1,332 @@
1
+ /**
2
+ * 「打开方式」:探测本机装了什么能打开文件的应用,并按用户选择拉起。
3
+ *
4
+ * 只列真能打开的:先看 /Applications 与 ~/Applications 里的 .app 包,
5
+ * 再补 PATH 里的命令行工具;没装的项不出现,不写死整张表。
6
+ * 图标用 macOS 自带的 qlmanage 从 .app 里抽,缓存到数据空间,避免每次打开都抽。
7
+ */
8
+ import fs from 'node:fs';
9
+ import os from 'node:os';
10
+ import path from 'node:path';
11
+ import { execFile } from 'node:child_process';
12
+ import { promisify } from 'node:util';
13
+
14
+ const execFileAsync = promisify(execFile);
15
+
16
+ /**
17
+ * 已知应用表:bundle 名 / 进程名 / CLI 名。
18
+ * `order` 决定菜单顺序(访达固定第一,终端固定最后)。
19
+ */
20
+ const KNOWN_APPS = [
21
+ {
22
+ id: 'vscode',
23
+ label: 'VS Code',
24
+ kind: 'editor',
25
+ bundles: ['Visual Studio Code.app', 'Visual Studio Code - Insiders.app'],
26
+ clis: ['code'],
27
+ order: 10,
28
+ },
29
+ {
30
+ id: 'cursor',
31
+ label: 'Cursor',
32
+ kind: 'editor',
33
+ bundles: ['Cursor.app'],
34
+ clis: ['cursor'],
35
+ order: 20,
36
+ },
37
+ {
38
+ id: 'vscodium',
39
+ label: 'VSCodium',
40
+ kind: 'editor',
41
+ bundles: ['VSCodium.app'],
42
+ clis: ['codium'],
43
+ order: 30,
44
+ },
45
+ {
46
+ id: 'xcode',
47
+ label: 'Xcode',
48
+ kind: 'editor',
49
+ bundles: ['Xcode.app'],
50
+ clis: [],
51
+ order: 40,
52
+ },
53
+ {
54
+ id: 'sourcetree',
55
+ label: 'Sourcetree',
56
+ kind: 'editor',
57
+ bundles: ['Sourcetree.app'],
58
+ clis: [],
59
+ order: 50,
60
+ },
61
+ {
62
+ id: 'webstorm',
63
+ label: 'WebStorm',
64
+ kind: 'editor',
65
+ bundles: ['WebStorm.app'],
66
+ clis: ['webstorm'],
67
+ order: 60,
68
+ },
69
+ {
70
+ id: 'idea',
71
+ label: 'IntelliJ IDEA',
72
+ kind: 'editor',
73
+ bundles: ['IntelliJ IDEA.app', 'IntelliJ IDEA CE.app'],
74
+ clis: ['idea'],
75
+ order: 70,
76
+ },
77
+ {
78
+ id: 'sublime',
79
+ label: 'Sublime Text',
80
+ kind: 'editor',
81
+ bundles: ['Sublime Text.app'],
82
+ clis: ['subl'],
83
+ order: 80,
84
+ },
85
+ {
86
+ id: 'iterm',
87
+ label: 'iTerm2',
88
+ kind: 'terminal',
89
+ bundles: ['iTerm.app', 'iTerm2.app'],
90
+ clis: [],
91
+ order: 800,
92
+ },
93
+ {
94
+ id: 'terminal',
95
+ label: '终端',
96
+ kind: 'terminal',
97
+ bundles: ['Terminal.app'],
98
+ clis: [],
99
+ order: 900,
100
+ },
101
+ ];
102
+
103
+ /** 访达不真实存在于 /Applications,单独处理 */
104
+ const FINDER = { id: 'finder', label: '访达', kind: 'finder', order: 0 };
105
+
106
+ /** 默认打开方式:VS Code 优先,其次任一编辑器,最后访达 */
107
+ export const DEFAULT_OPEN_APP = 'vscode';
108
+
109
+ function appDirs() {
110
+ if (process.platform !== 'darwin') return [];
111
+ return ['/Applications', path.join(os.homedir(), 'Applications')];
112
+ }
113
+
114
+ function findBundle(names) {
115
+ for (const dir of appDirs()) {
116
+ for (const name of names) {
117
+ const abs = path.join(dir, name);
118
+ if (fs.existsSync(abs)) return abs;
119
+ }
120
+ }
121
+ return '';
122
+ }
123
+
124
+ async function findCli(name) {
125
+ const dirs = String(process.env.PATH ?? '')
126
+ .split(path.delimiter)
127
+ .filter(Boolean);
128
+ // 版本管理器装的 GUI 编辑器常常不在 GUI 进程的 PATH 里,补几个常见位置
129
+ dirs.push('/usr/local/bin', '/opt/homebrew/bin', path.join(os.homedir(), '.local/bin'));
130
+ for (const dir of dirs) {
131
+ const abs = path.join(dir, name);
132
+ try {
133
+ fs.accessSync(abs, fs.constants.X_OK);
134
+ return abs;
135
+ } catch {
136
+ /* 下一个 */
137
+ }
138
+ }
139
+ return '';
140
+ }
141
+
142
+ /**
143
+ * 本机可用的打开方式。
144
+ * 返回项已排序:访达 → 编辑器(常见度)→ 终端;一个都没有时只留访达(Windows / Linux 留资源管理器)。
145
+ */
146
+ export async function listOpenApps() {
147
+ const apps = [];
148
+ if (process.platform === 'darwin') apps.push({ ...FINDER });
149
+
150
+ for (const known of KNOWN_APPS) {
151
+ const appPath = findBundle(known.bundles);
152
+ const cli = await findCli(known.clis[0] ?? '');
153
+ if (!appPath && !cli) continue;
154
+ apps.push({
155
+ id: known.id,
156
+ label: known.label,
157
+ kind: known.kind,
158
+ order: known.order,
159
+ appPath,
160
+ cli,
161
+ });
162
+ }
163
+
164
+ // 没有任何编辑器时的兜底:系统默认程序也能打开文件
165
+ if (process.platform === 'darwin' && !apps.some((item) => item.kind === 'editor')) {
166
+ apps.push({ id: 'system-default', label: '系统默认程序', kind: 'editor', order: 500 });
167
+ }
168
+
169
+ return apps.sort((a, b) => a.order - b.order).map(({ order: _order, ...rest }) => rest);
170
+ }
171
+
172
+ export function defaultOpenApp(apps) {
173
+ const has = (id) => apps.some((item) => item.id === id);
174
+ if (has(DEFAULT_OPEN_APP)) return DEFAULT_OPEN_APP;
175
+ const editor = apps.find((item) => item.kind === 'editor');
176
+ if (editor) return editor.id;
177
+ return apps[0]?.id ?? '';
178
+ }
179
+
180
+ /**
181
+ * 应用图标:优先从 .app 里的 .icns 用系统自带 sips 转 PNG(快且不依赖 QuickLook),
182
+ * 抽不到再退回 qlmanage。缓存文件名带 app 的 mtime,应用升级后会自动重抽。
183
+ */
184
+ export async function appIcon(app, cacheDir) {
185
+ if (!app?.appPath || process.platform !== 'darwin') return '';
186
+ let stamp = 0;
187
+ try {
188
+ stamp = Math.floor(fs.statSync(app.appPath).mtimeMs);
189
+ } catch {
190
+ return '';
191
+ }
192
+ fs.mkdirSync(cacheDir, { recursive: true });
193
+ const file = path.join(cacheDir, `${app.id}-${stamp}.png`);
194
+ if (fs.existsSync(file)) return file;
195
+ const tmpDir = path.join(cacheDir, `.tmp-${process.pid}-${Date.now()}`);
196
+ try {
197
+ fs.mkdirSync(tmpDir, { recursive: true });
198
+ const icns = findIcns(app.appPath);
199
+ if (icns) {
200
+ try {
201
+ await execFileAsync(
202
+ 'sips',
203
+ ['-s', 'format', 'png', '-z', '64', '64', icns, '--out', file],
204
+ { timeout: 8000 },
205
+ );
206
+ if (fs.existsSync(file)) {
207
+ pruneOldIcons(cacheDir, app.id, file);
208
+ return file;
209
+ }
210
+ } catch {
211
+ // icns 转不出来就继续试 qlmanage
212
+ }
213
+ }
214
+ await execFileAsync('qlmanage', ['-t', '-s', '64', '-o', tmpDir, app.appPath], {
215
+ timeout: 5000,
216
+ });
217
+ const produced = fs.readdirSync(tmpDir).find((name) => name.toLowerCase().endsWith('.png'));
218
+ if (!produced) return '';
219
+ fs.renameSync(path.join(tmpDir, produced), file);
220
+ pruneOldIcons(cacheDir, app.id, file);
221
+ return file;
222
+ } catch {
223
+ return '';
224
+ } finally {
225
+ fs.rmSync(tmpDir, { recursive: true, force: true });
226
+ }
227
+ }
228
+
229
+ /** .app/Contents/Resources 里挑一个 .icns:优先与 app 同名的(VS Code 就是 Code.icns) */
230
+ function findIcns(appPath) {
231
+ const resources = path.join(appPath, 'Contents', 'Resources');
232
+ let names = [];
233
+ try {
234
+ names = fs.readdirSync(resources).filter((name) => name.toLowerCase().endsWith('.icns'));
235
+ } catch {
236
+ return '';
237
+ }
238
+ if (!names.length) return '';
239
+ const stem = path
240
+ .basename(appPath, '.app')
241
+ .toLowerCase()
242
+ .replace(/[^a-z0-9]+/g, '');
243
+ const preferred =
244
+ names.find((name) =>
245
+ name
246
+ .toLowerCase()
247
+ .replace(/[^a-z0-9]+/g, '')
248
+ .startsWith(stem),
249
+ ) ??
250
+ names.find((name) => /^app\.icns$/i.test(name)) ??
251
+ names[0];
252
+ return path.join(resources, preferred);
253
+ }
254
+
255
+ /** 只留当前这一版图标,避免每次升级攒一堆 */
256
+ function pruneOldIcons(cacheDir, appId, keep) {
257
+ try {
258
+ for (const name of fs.readdirSync(cacheDir)) {
259
+ if (
260
+ name.startsWith(`${appId}-`) &&
261
+ name.endsWith('.png') &&
262
+ path.join(cacheDir, name) !== keep
263
+ ) {
264
+ fs.rmSync(path.join(cacheDir, name), { force: true });
265
+ }
266
+ }
267
+ } catch {
268
+ // 清理失败不影响使用
269
+ }
270
+ }
271
+
272
+ /**
273
+ * 用指定应用打开文件或目录。
274
+ * - editor:有 CLI 用 `<cli> -g file:line`(能定位到行),否则 `open -a App file`
275
+ * - finder:`open -R file`(选中文件)/ `open dir`
276
+ * - terminal:`open -a App dir`(在目录里开终端)
277
+ * 全部走 argv,不做字符串拼接。
278
+ */
279
+ export async function openWith({ abs, dir, app, line }) {
280
+ const target = abs || dir;
281
+ if (!target) throw new Error('没有可打开的路径');
282
+ const appPath = app?.appPath ?? '';
283
+ const cli = app?.cli ?? '';
284
+ const kind = app?.kind ?? 'system';
285
+
286
+ if (kind === 'finder') {
287
+ if (process.platform === 'darwin') {
288
+ const exists = fs.existsSync(target);
289
+ await execFileAsync('open', exists && abs ? ['-R', abs] : [dir || target], {
290
+ timeout: 10000,
291
+ });
292
+ return;
293
+ }
294
+ await openWithSystem(target);
295
+ return;
296
+ }
297
+
298
+ if (kind === 'terminal') {
299
+ const where = dir || (abs ? path.dirname(abs) : '');
300
+ if (!where) throw new Error('没有可打开的目录');
301
+ if (process.platform === 'darwin' && appPath) {
302
+ await execFileAsync('open', ['-a', appPath, where], { timeout: 10000 });
303
+ return;
304
+ }
305
+ await openWithSystem(where);
306
+ return;
307
+ }
308
+
309
+ // editor:CLI 优先(能带行号),否则交给 open -a
310
+ if (cli) {
311
+ const arg = abs && line ? `${abs}:${line}` : abs || dir;
312
+ await execFileAsync(cli, ['-g', arg], { timeout: 10000 });
313
+ return;
314
+ }
315
+ if (process.platform === 'darwin' && appPath) {
316
+ await execFileAsync('open', ['-a', appPath, abs || dir], { timeout: 10000 });
317
+ return;
318
+ }
319
+ await openWithSystem(abs || dir);
320
+ }
321
+
322
+ async function openWithSystem(target) {
323
+ if (process.platform === 'darwin') {
324
+ await execFileAsync('open', [target], { timeout: 10000 });
325
+ return;
326
+ }
327
+ if (process.platform === 'win32') {
328
+ await execFileAsync('cmd', ['/c', 'start', '', target], { windowsHide: true, timeout: 10000 });
329
+ return;
330
+ }
331
+ await execFileAsync('xdg-open', [target], { timeout: 10000 });
332
+ }