autosnippet 3.1.10 → 3.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.
@@ -18,6 +18,38 @@ const __filename = fileURLToPath(import.meta.url);
18
18
  const __dirname = dirname(__filename);
19
19
 
20
20
  const NATIVE_UI_PATH = join(__dirname, '../../../resources/native-ui/native-ui');
21
+ const NATIVE_UI_SRC = join(__dirname, '../../../resources/native-ui/main.swift');
22
+ const NATIVE_UI_COMBINED = join(__dirname, '../../../resources/native-ui/combined-window.swift');
23
+
24
+ /** 记录是否已尝试过 lazy build,避免重复 */
25
+ let _lazyBuildAttempted = false;
26
+
27
+ /**
28
+ * 尝试即时编译 native-ui(仅 macOS + swiftc 可用时)
29
+ * 只调用一次,结果缓存到 _lazyBuildAttempted
30
+ */
31
+ function _tryLazyBuild() {
32
+ if (_lazyBuildAttempted) return;
33
+ _lazyBuildAttempted = true;
34
+
35
+ if (process.platform !== 'darwin') return;
36
+ if (!existsSync(NATIVE_UI_SRC) || !existsSync(NATIVE_UI_COMBINED)) return;
37
+
38
+ try {
39
+ execSync('which swiftc', { stdio: 'pipe' });
40
+ } catch {
41
+ return;
42
+ }
43
+
44
+ try {
45
+ execSync(
46
+ `swiftc "${NATIVE_UI_SRC}" "${NATIVE_UI_COMBINED}" -o "${NATIVE_UI_PATH}" -framework AppKit`,
47
+ { stdio: 'pipe', timeout: 120_000 }
48
+ );
49
+ } catch {
50
+ // 编译失败 — 静默降级
51
+ }
52
+ }
21
53
 
22
54
  /**
23
55
  * 检查 Swift Helper 是否可用
@@ -27,6 +59,11 @@ export function isNativeUiAvailable() {
27
59
  return false;
28
60
  }
29
61
  try {
62
+ if (existsSync(NATIVE_UI_PATH)) {
63
+ return true;
64
+ }
65
+ // 二进制不存在 — 尝试即时编译
66
+ _tryLazyBuild();
30
67
  return existsSync(NATIVE_UI_PATH);
31
68
  } catch {
32
69
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autosnippet",
3
- "version": "3.1.10",
3
+ "version": "3.1.11",
4
4
  "description": "Extract code patterns into a knowledge base for AI coding assistants",
5
5
  "type": "module",
6
6
  "main": "lib/bootstrap.js",
@@ -52,3 +52,22 @@ try {
52
52
  } else {
53
53
  }
54
54
  }
55
+
56
+ // ── 发布流程:最终校验二进制必须存在 ──
57
+ if (isPublishing) {
58
+ if (!fs.existsSync(out)) {
59
+ console.error('❌ 发布中止:native-ui 二进制不存在于', out);
60
+ console.error(' prepublishOnly 编译步骤可能被跳过或失败');
61
+ process.exit(1);
62
+ }
63
+ const stat = fs.statSync(out);
64
+ if (stat.size < 10_000) {
65
+ console.error('❌ 发布中止:native-ui 二进制异常(仅', stat.size, '字节)');
66
+ process.exit(1);
67
+ }
68
+ // 确保有执行权限
69
+ try {
70
+ fs.chmodSync(out, 0o755);
71
+ } catch { /* ignore */ }
72
+ console.log('✅ native-ui 二进制校验通过 (%s KB)', (stat.size / 1024).toFixed(1));
73
+ }
@@ -1,10 +1,14 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  /**
4
- * 安全的 postinstall 脚本 - 只检查不编译
5
- * 用于避免触发 npm 安全警告
4
+ * 安全的 postinstall 脚本
5
+ *
6
+ * 在 macOS 上如果 native-ui 二进制缺失但 Swift 源码存在,
7
+ * 尝试自动编译(需要 Xcode Command Line Tools)。
8
+ * 编译失败不阻塞安装流程。
6
9
  */
7
10
 
11
+ import { execSync } from 'node:child_process';
8
12
  import { dirname } from 'node:path';
9
13
  import { fileURLToPath } from 'node:url';
10
14
 
@@ -16,12 +20,59 @@ import path from 'node:path';
16
20
 
17
21
  const root = path.resolve(__dirname, '..');
18
22
 
23
+ const NATIVE_UI_BIN = path.join(root, 'resources', 'native-ui', 'native-ui');
24
+ const NATIVE_UI_SRC = path.join(root, 'resources', 'native-ui', 'main.swift');
25
+ const NATIVE_UI_COMBINED = path.join(root, 'resources', 'native-ui', 'combined-window.swift');
26
+
27
+ /**
28
+ * 在 macOS 上尝试编译 native-ui(如果二进制缺失)
29
+ */
30
+ function tryBuildNativeUi() {
31
+ // 非 macOS 跳过
32
+ if (process.platform !== 'darwin') return;
33
+
34
+ // 已有二进制,跳过
35
+ if (fs.existsSync(NATIVE_UI_BIN)) return;
36
+
37
+ // 检查源码是否存在
38
+ if (!fs.existsSync(NATIVE_UI_SRC) || !fs.existsSync(NATIVE_UI_COMBINED)) {
39
+ return;
40
+ }
41
+
42
+ // 检查 swiftc 是否可用
43
+ try {
44
+ execSync('which swiftc', { stdio: 'pipe' });
45
+ } catch {
46
+ console.log(
47
+ '💡 Native UI 需要 Swift 编译器。运行 xcode-select --install 后执行:\n' +
48
+ ` swiftc "${NATIVE_UI_SRC}" "${NATIVE_UI_COMBINED}" -o "${NATIVE_UI_BIN}" -framework AppKit`
49
+ );
50
+ return;
51
+ }
52
+
53
+ // 尝试编译
54
+ try {
55
+ execSync(
56
+ `swiftc "${NATIVE_UI_SRC}" "${NATIVE_UI_COMBINED}" -o "${NATIVE_UI_BIN}" -framework AppKit`,
57
+ { cwd: root, stdio: 'pipe', timeout: 120_000 }
58
+ );
59
+ if (fs.existsSync(NATIVE_UI_BIN)) {
60
+ console.log('✅ Native UI 已自动编译');
61
+ }
62
+ } catch {
63
+ console.log(
64
+ '⚠️ Native UI 自动编译失败,Xcode file watcher 将使用 AppleScript 降级方案。\n' +
65
+ ' 手动编译: npm run build:native-ui(需要 Xcode Command Line Tools)'
66
+ );
67
+ }
68
+ }
69
+
19
70
  // 检查预构建的二进制文件
20
71
  function checkBinaries() {
21
72
  const checks = [
22
73
  {
23
74
  name: 'Native UI',
24
- path: path.join(root, 'resources', 'native-ui', 'native-ui'),
75
+ path: NATIVE_UI_BIN,
25
76
  optional: true,
26
77
  platform: 'darwin',
27
78
  },
@@ -43,4 +94,5 @@ function checkBinaries() {
43
94
  });
44
95
  }
45
96
 
97
+ tryBuildNativeUi();
46
98
  checkBinaries();