lantern-harness 0.1.8 → 0.1.10

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 (183) hide show
  1. package/CHANGELOG.md +106 -0
  2. package/README.md +7 -7
  3. package/builtin/session/lh-fs.mjs +32 -14
  4. package/cli/server.mjs +2 -0
  5. package/config/app.defaults.json +1 -0
  6. package/dist/500.html +258 -22
  7. package/dist/_astro/Base.BOY6F0mi.css +1 -0
  8. package/dist/_astro/Base.astro_astro_type_script_index_0_lang.C30xtpc6.js +1 -0
  9. package/dist/_astro/CaseExecBoard.Dttqp0wg.js +1 -0
  10. package/dist/_astro/CaseGenerateApp.DqW_nWMc.js +9 -0
  11. package/dist/_astro/CasesLibrary.8aMUUGp_.js +1 -0
  12. package/dist/_astro/ChatApp.BLxf0h1d.js +219 -0
  13. package/dist/_astro/ColumnResize.BEkEW7qw.js +1 -0
  14. package/dist/_astro/{ConfirmDialog.CBVsDJ8f.js → ConfirmDialog.BZzZWDci.js} +1 -1
  15. package/dist/_astro/{DashboardBoard.Bd3eUPwZ.js → DashboardBoard.CkSbhEDI.js} +3 -3
  16. package/dist/_astro/DatePicker.Bkl71M49.js +1 -0
  17. package/dist/_astro/DocsFileBrowser.MKVCfZrE.js +1 -0
  18. package/dist/_astro/DocsPager.DJY4foYL.js +1 -0
  19. package/dist/_astro/{DrawerField.CWMLhcPX.js → DrawerField.CHRg77LG.js} +1 -1
  20. package/dist/_astro/{ExcelFieldTags.B_XMwNNx.js → ExcelFieldTags.DXMeaDa5.js} +4 -4
  21. package/dist/_astro/KnowledgeLibrary.B-p1aoQN.js +1 -0
  22. package/dist/_astro/NamePromptDialog.Wa9O530a.js +1 -0
  23. package/dist/_astro/NavLibraries.C6LImMO_.js +1 -0
  24. package/dist/_astro/OverflowHint.D6v4Uj2N.js +1 -0
  25. package/dist/_astro/PluginsPage.-97n3x2U.js +39 -0
  26. package/dist/_astro/PluginsPage.DkmRPfbU.js +1 -0
  27. package/dist/_astro/PmBugsBoard.CMT4-eoB.js +1 -0
  28. package/dist/_astro/PmIterationsBoard.BQM_dzdZ.js +1 -0
  29. package/dist/_astro/PmProjectDetail.CzU80pat.js +1 -0
  30. package/dist/_astro/PmProjectsBoard.Cxsa7SYQ.js +1 -0
  31. package/dist/_astro/PmRequirementsBoard.4_JMDDN0.js +1 -0
  32. package/dist/_astro/PmRequirementsBoard.B8HL07cw.js +1 -0
  33. package/dist/_astro/PmShared.Ca2Ao7v_.js +1 -0
  34. package/dist/_astro/PromptsLibrary.C9BcIxKK.js +1 -0
  35. package/dist/_astro/ReportsPage.Fh5DRKLT.js +1 -0
  36. package/dist/_astro/RequirementsLibrary.CBkuWE8X.js +1 -0
  37. package/dist/_astro/SearchableSelect.DnjoSslK.js +1 -0
  38. package/dist/_astro/SelectDropdown.CuuDHa8s.js +1 -0
  39. package/dist/_astro/SettingsDialog.CJvBRcs9.js +1 -0
  40. package/dist/_astro/SlideOverDrawer.B7B5Wu8I.js +1 -0
  41. package/dist/_astro/{SpaceSwitcher.BEJo5KFi.js → SpaceSwitcher.BVQLt_tL.js} +2 -2
  42. package/dist/_astro/TagSelect.CuN5RdF4.js +1 -0
  43. package/dist/_astro/TasksBoard.Bu-fpIZ7.js +1 -0
  44. package/dist/_astro/TemplatesLibrary.DSylQGlN.js +1 -0
  45. package/dist/_astro/TestStatsBoard.CPyKW4wc.js +1 -0
  46. package/dist/_astro/Toast.R8_2OWRX.js +1 -0
  47. package/dist/_astro/TodosBoard.8kvRTEW-.js +1 -0
  48. package/dist/_astro/UiAutoBoard.DFTiDqCp.js +9 -0
  49. package/dist/_astro/active-element-history.CED-m1Zp.js +1 -0
  50. package/dist/_astro/app-config.BUPzGl9f.js +1 -0
  51. package/dist/_astro/{client.Be_SakrB.js → client.BHNTqLjv.js} +3 -3
  52. package/dist/_astro/dialog.BAPTk9a_.js +1 -0
  53. package/dist/_astro/hydrate-cache.BXX604Bw.js +1 -0
  54. package/dist/_astro/i18n.BGPnP1Bg.js +7 -0
  55. package/dist/_astro/index.astro_astro_type_script_index_0_lang.LPL9mPQY.js +1 -0
  56. package/dist/_astro/jsx-runtime.DUg_B_vD.js +1 -0
  57. package/dist/_astro/{lib.DJNDi_Ht.js → lib.CgXLzMMd.js} +15 -15
  58. package/dist/_astro/listbox.C96OCanh.js +1 -0
  59. package/dist/_astro/markdown-client.DRva7DS9.js +58 -0
  60. package/dist/_astro/menu.IPWdRoNM.js +1 -0
  61. package/dist/_astro/{open-docs-in-session.B0ZdZNj2.js → open-docs-in-session.CyFNgh1I.js} +1 -1
  62. package/dist/_astro/{plugins.D5ypJIXn.js → plugins.C2BMfzv1.js} +1 -1
  63. package/dist/_astro/{pm.BsnTv63L.js → pm.CagBA-ht.js} +4 -4
  64. package/dist/_astro/{portal.Cm9XlIPf.js → portal.C0hAhXDF.js} +2 -2
  65. package/dist/_astro/prompts-meta.Bua-j14I.js +2 -0
  66. package/dist/_astro/react-dom.DLW9pGCF.js +1 -0
  67. package/dist/_astro/{react-dom.CqAxpHMt.js → react.Blvh0GOs.js} +1 -1
  68. package/dist/_astro/report-prompt.CAfZ6RIo.js +1 -0
  69. package/dist/_astro/{settings.CnDCvwnQ.js → settings.Ceefeil5.js} +2 -2
  70. package/dist/_astro/{spaces-client.Cai2tHEI.js → spaces-client.BocUabwy.js} +1 -1
  71. package/dist/_astro/ui-auto-prompt.CX8fju7r.js +3 -0
  72. package/dist/_astro/ui-auto.BmC9JJ_X.js +1 -0
  73. package/dist/_astro/update.CvfIVwnq.js +1 -0
  74. package/dist/_astro/use-text-value.DBQvHeNl.js +1 -0
  75. package/dist/_astro/use-tree-walker.DFU2AaAP.js +1 -0
  76. package/dist/_astro/useListAnim.BoerpmsB.js +1 -0
  77. package/dist/_astro/{view-prefs.HCowOWXL.js → view-prefs.BEAZRcxF.js} +1 -1
  78. package/dist/case-exec/index.html +458 -22
  79. package/dist/cases/generate/index.html +258 -22
  80. package/dist/cases/library/index.html +458 -22
  81. package/dist/dashboard/index.html +458 -22
  82. package/dist/docs/changelog/index.html +389 -23
  83. package/dist/docs/install/index.html +258 -22
  84. package/dist/docs/manual/index.html +324 -27
  85. package/dist/index.html +2 -281
  86. package/dist/knowledge/index.html +458 -22
  87. 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
  88. package/dist/plugins/index.html +258 -22
  89. package/dist/pm/bugs/index.html +458 -22
  90. package/dist/pm/iterations/index.html +458 -22
  91. package/dist/pm/projects/detail/index.html +458 -22
  92. package/dist/pm/projects/index.html +458 -22
  93. package/dist/pm/requirements/index.html +458 -22
  94. package/dist/prompts/index.html +458 -22
  95. package/dist/reports/index.html +458 -22
  96. package/dist/requirements/index.html +458 -22
  97. package/dist/tasks/index.html +458 -22
  98. package/dist/templates/index.html +458 -22
  99. 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
  100. 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
  101. 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
  102. 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
  103. 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
  104. package/dist/test-automation/index.html +717 -0
  105. package/dist/test-stats/index.html +458 -22
  106. package/dist/todos/index.html +458 -22
  107. package/dist/workbench/index.html +517 -0
  108. package/docs//346/223/215/344/275/234/346/211/213/345/206/214.md +85 -34
  109. package/lh-plugins/lh-project-docs.mjs +14 -0
  110. package/lh-plugins/lh-uiauto.mjs +2296 -0
  111. package/lh-plugins/plugin-prompts.mjs +200 -0
  112. package/lh-plugins/storage-domain.mjs +13 -0
  113. package/lh-plugins/uiauto-adapters/index.mjs +57 -0
  114. package/lh-plugins/uiauto-adapters/playwright.mjs +215 -0
  115. package/lh-plugins/uiauto-adapters/pytest.mjs +151 -0
  116. package/lh-plugins/uiauto-adapters/selenium.mjs +163 -0
  117. package/lh-plugins/uiauto-adapters/shared.mjs +371 -0
  118. package/lh-plugins/uiauto-adapters/tasks.mjs +369 -0
  119. package/lh-plugins/uiauto-argv-guard.cjs +41 -0
  120. package/lh-plugins/uiauto-explorer/apps.mjs +332 -0
  121. package/lh-plugins/uiauto-explorer/diff.mjs +159 -0
  122. package/lh-plugins/uiauto-explorer/emit-files.mjs +506 -0
  123. package/lh-plugins/uiauto-explorer/emit-python.mjs +160 -0
  124. package/lh-plugins/uiauto-explorer/emit-ts.mjs +118 -0
  125. package/lh-plugins/uiauto-explorer/emit.mjs +15 -0
  126. package/lh-plugins/uiauto-explorer/ir.mjs +114 -0
  127. package/lh-plugins/uiauto-explorer/locators.mjs +267 -0
  128. package/lh-plugins/uiauto-explorer/merge.mjs +235 -0
  129. package/lh-plugins/uiauto-explorer/plan.mjs +165 -0
  130. package/lh-plugins/uiauto-explorer/runner.mjs +370 -0
  131. package/lh-plugins/uiauto-explorer/sessions.mjs +1373 -0
  132. package/lh-plugins/uiauto-explorer/snapshot.mjs +492 -0
  133. package/lh-plugins/uiauto-prompt.mjs +122 -0
  134. package/lh-plugins/uiauto-report.mjs +111 -0
  135. package/package.json +1 -1
  136. package/dist/_astro/Base.DjIEQkT7.css +0 -1
  137. package/dist/_astro/Base.astro_astro_type_script_index_0_lang.D0snmAJ_.js +0 -1
  138. package/dist/_astro/CaseExecBoard.BAV4EE3o.js +0 -1
  139. package/dist/_astro/CaseGenerateApp.Bz4ryNJG.js +0 -9
  140. package/dist/_astro/CasesLibrary.BPifyHUA.js +0 -1
  141. package/dist/_astro/ChatApp.DLo31SYZ.js +0 -276
  142. package/dist/_astro/ColumnResize.Bs6GL3DJ.js +0 -1
  143. package/dist/_astro/DatePicker.CMfX7F6z.js +0 -1
  144. package/dist/_astro/DocsFileBrowser.D3cKurQE.js +0 -1
  145. package/dist/_astro/DocsPager.rzu4mnzw.js +0 -1
  146. package/dist/_astro/KnowledgeLibrary.B0sO4QR1.js +0 -1
  147. package/dist/_astro/NamePromptDialog.Dl5kSnQI.js +0 -1
  148. package/dist/_astro/NavLibraries.Cvg3wery.js +0 -1
  149. package/dist/_astro/OverflowHint.lzmplP5j.js +0 -1
  150. package/dist/_astro/PluginsPage.DIltPILi.js +0 -39
  151. package/dist/_astro/PluginsPage.uvmSuXzi.js +0 -1
  152. package/dist/_astro/PmBugsBoard.CiaIhCHv.js +0 -1
  153. package/dist/_astro/PmIterationsBoard.H67QwKFw.js +0 -1
  154. package/dist/_astro/PmProjectDetail.DqDoIg8a.js +0 -1
  155. package/dist/_astro/PmProjectsBoard.nOC1Hugt.js +0 -1
  156. package/dist/_astro/PmRequirementsBoard.BPjZ7khP.js +0 -1
  157. package/dist/_astro/PmRequirementsBoard.CanxvcKg.js +0 -1
  158. package/dist/_astro/PmShared.CZnXaUq-.js +0 -1
  159. package/dist/_astro/PromptsLibrary.DU9EuSim.js +0 -1
  160. package/dist/_astro/ReportsPage.Cr88iz8c.js +0 -1
  161. package/dist/_astro/RequirementsLibrary.BwriF8F0.js +0 -1
  162. package/dist/_astro/SearchableSelect.Ctg8V1Z1.js +0 -1
  163. package/dist/_astro/SelectDropdown.Buv7rQJ_.js +0 -1
  164. package/dist/_astro/SettingsDialog.DEgsVfaL.js +0 -1
  165. package/dist/_astro/SlideOverDrawer.TPbGEoWn.js +0 -1
  166. package/dist/_astro/TagSelect.u8DTop7v.js +0 -1
  167. package/dist/_astro/TasksBoard.BZi_GBPG.js +0 -1
  168. package/dist/_astro/TemplatesLibrary.T4K9jUex.js +0 -1
  169. package/dist/_astro/TestStatsBoard.Dd1Kcmx6.js +0 -1
  170. package/dist/_astro/TodosBoard.C4yd_JFb.js +0 -1
  171. package/dist/_astro/active-element-history.CeefGkFQ.js +0 -1
  172. package/dist/_astro/app-config.DKMxjv3g.js +0 -1
  173. package/dist/_astro/dialog.DXHznyjD.js +0 -1
  174. package/dist/_astro/i18n.BXeGGutk.js +0 -7
  175. package/dist/_astro/jsx-runtime.BUXbF_vF.js +0 -1
  176. package/dist/_astro/listbox.Di-t5U37.js +0 -1
  177. package/dist/_astro/menu.Bkluqxic.js +0 -1
  178. package/dist/_astro/prompts-meta.F4wwkjUA.js +0 -2
  179. package/dist/_astro/report-prompt.Cmw4XzfY.js +0 -1
  180. package/dist/_astro/update.DXCPfU4a.js +0 -1
  181. package/dist/_astro/use-text-value.BuY0Cm1z.js +0 -1
  182. package/dist/_astro/use-tree-walker.CfTHFX43.js +0 -1
  183. package/dist/_astro/useListAnim.CpGB1cft.js +0 -1
@@ -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
+ }
@@ -0,0 +1,159 @@
1
+ /**
2
+ * 极简行级 diff:给「文件变更」面板用,不引第三方依赖。
3
+ *
4
+ * 做法:先剥掉公共前后缀,再对中间段做 LCS;段太大时退化成整块替换,
5
+ * 宁可粗一点也不要卡住浏览器(AI 生成的 spec 一般几十行,退化很少发生)。
6
+ */
7
+
8
+ const MAX_LINES = 4000;
9
+ const MAX_CELLS = 4_000_000;
10
+
11
+ function toLines(text) {
12
+ const value = String(text ?? '');
13
+ if (!value) return [];
14
+ const lines = value.split(/\r?\n/);
15
+ // 末尾换行不单独算一行,否则每个文件都会多出一条空上下文
16
+ if (lines.length && lines[lines.length - 1] === '') lines.pop();
17
+ return lines;
18
+ }
19
+
20
+ /** 中间段 LCS → 操作序列 */
21
+ function diffMiddle(a, b) {
22
+ if (!a.length) return b.map((text) => ({ type: 'add', text }));
23
+ if (!b.length) return a.map((text) => ({ type: 'del', text }));
24
+ if (a.length * b.length > MAX_CELLS) {
25
+ return [
26
+ ...a.map((text) => ({ type: 'del', text })),
27
+ ...b.map((text) => ({ type: 'add', text })),
28
+ ];
29
+ }
30
+ const rows = a.length;
31
+ const cols = b.length;
32
+ // 从后往前填表,回溯时正好从前往后走
33
+ const table = new Uint32Array((rows + 1) * (cols + 1));
34
+ const at = (i, j) => i * (cols + 1) + j;
35
+ for (let i = rows - 1; i >= 0; i -= 1) {
36
+ for (let j = cols - 1; j >= 0; j -= 1) {
37
+ table[at(i, j)] =
38
+ a[i] === b[j]
39
+ ? table[at(i + 1, j + 1)] + 1
40
+ : Math.max(table[at(i + 1, j)], table[at(i, j + 1)]);
41
+ }
42
+ }
43
+ const ops = [];
44
+ let i = 0;
45
+ let j = 0;
46
+ while (i < rows && j < cols) {
47
+ if (a[i] === b[j]) {
48
+ ops.push({ type: 'equal', text: a[i] });
49
+ i += 1;
50
+ j += 1;
51
+ } else if (table[at(i + 1, j)] >= table[at(i, j + 1)]) {
52
+ ops.push({ type: 'del', text: a[i] });
53
+ i += 1;
54
+ } else {
55
+ ops.push({ type: 'add', text: b[j] });
56
+ j += 1;
57
+ }
58
+ }
59
+ while (i < rows) {
60
+ ops.push({ type: 'del', text: a[i] });
61
+ i += 1;
62
+ }
63
+ while (j < cols) {
64
+ ops.push({ type: 'add', text: b[j] });
65
+ j += 1;
66
+ }
67
+ return ops;
68
+ }
69
+
70
+ function diffOps(a, b) {
71
+ const maxPrefix = Math.min(a.length, b.length);
72
+ let prefix = 0;
73
+ while (prefix < maxPrefix && a[prefix] === b[prefix]) prefix += 1;
74
+ let suffix = 0;
75
+ while (suffix < maxPrefix - prefix && a[a.length - 1 - suffix] === b[b.length - 1 - suffix]) {
76
+ suffix += 1;
77
+ }
78
+ const head = a.slice(0, prefix).map((text) => ({ type: 'equal', text }));
79
+ const middleA = a.slice(prefix, a.length - suffix);
80
+ const middleB = b.slice(prefix, b.length - suffix);
81
+ const tail = suffix ? a.slice(a.length - suffix).map((text) => ({ type: 'equal', text })) : [];
82
+ return [...head, ...diffMiddle(middleA, middleB), ...tail];
83
+ }
84
+
85
+ /**
86
+ * @param {string} before 旧内容(新文件传空串)
87
+ * @param {string} after 新内容
88
+ * @param {{ context?: number, maxLines?: number }} [options]
89
+ */
90
+ export function diffText(before, after, options = {}) {
91
+ const context = Math.max(0, Math.min(20, Number(options.context ?? 3)));
92
+ const maxLines = Math.max(100, Number(options.maxLines ?? MAX_LINES));
93
+ const a = toLines(before);
94
+ const b = toLines(after);
95
+
96
+ if (a.length + b.length > maxLines) {
97
+ return {
98
+ hunks: [],
99
+ added: b.length,
100
+ removed: a.length,
101
+ truncated: true,
102
+ message: `文件较大(${a.length + b.length} 行),只显示增删行数`,
103
+ };
104
+ }
105
+
106
+ const ops = diffOps(a, b);
107
+ const added = ops.filter((op) => op.type === 'add').length;
108
+ const removed = ops.filter((op) => op.type === 'del').length;
109
+
110
+ // 只保留「有改动」的行及其上下文,拼成 hunk
111
+ const keep = new Array(ops.length).fill(false);
112
+ ops.forEach((op, index) => {
113
+ if (op.type === 'equal') return;
114
+ for (let k = index - context; k <= index + context; k += 1) {
115
+ if (k >= 0 && k < ops.length) keep[k] = true;
116
+ }
117
+ });
118
+
119
+ const hunks = [];
120
+ let index = 0;
121
+ // 行号按「改动前的旧文件 / 改动后的新文件」分别累加
122
+ let oldNo = 1;
123
+ let newNo = 1;
124
+ while (index < ops.length) {
125
+ if (!keep[index]) {
126
+ if (ops[index].type !== 'add') oldNo += 1;
127
+ if (ops[index].type !== 'del') newNo += 1;
128
+ index += 1;
129
+ continue;
130
+ }
131
+ const lines = [];
132
+ const oldStart = oldNo;
133
+ const newStart = newNo;
134
+ let oldCount = 0;
135
+ let newCount = 0;
136
+ while (index < ops.length && keep[index]) {
137
+ const op = ops[index];
138
+ lines.push({ type: op.type, text: op.text });
139
+ if (op.type !== 'add') {
140
+ oldNo += 1;
141
+ oldCount += 1;
142
+ }
143
+ if (op.type !== 'del') {
144
+ newNo += 1;
145
+ newCount += 1;
146
+ }
147
+ index += 1;
148
+ }
149
+ hunks.push({ oldStart, oldLines: oldCount, newStart, newLines: newCount, lines });
150
+ }
151
+
152
+ return { hunks, added, removed, truncated: false };
153
+ }
154
+
155
+ /** 写进会话账本的一行统计,不存整份内容 */
156
+ export function summarizeDiff(before, after) {
157
+ const result = diffText(before, after, { context: 0 });
158
+ return { added: result.added, removed: result.removed };
159
+ }