@42ailab/42plugin 0.2.16 → 0.2.18

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 (2) hide show
  1. package/bin/42plugin +61 -10
  2. package/package.json +6 -6
package/bin/42plugin CHANGED
@@ -34,8 +34,43 @@ function getPlatformKey() {
34
34
  return `${platform}-${normalizedArch}`;
35
35
  }
36
36
 
37
+ // 检测包管理器
38
+ function detectPackageManager() {
39
+ // 检查是否通过 bun 运行
40
+ if (process.versions.bun) {
41
+ return 'bun';
42
+ }
43
+ // 检查全局安装路径中是否包含 bun
44
+ if (__dirname.includes('.bun')) {
45
+ return 'bun';
46
+ }
47
+ // 默认使用 npm
48
+ return 'npm';
49
+ }
50
+
51
+ // 尝试自动安装平台包
52
+ function tryAutoInstall(packageName) {
53
+ const pm = detectPackageManager();
54
+ console.error(`正在自动安装平台包: ${packageName}...`);
55
+
56
+ let result;
57
+ if (pm === 'bun') {
58
+ result = spawnSync('bun', ['add', '-g', packageName], {
59
+ stdio: 'inherit',
60
+ shell: process.platform === 'win32',
61
+ });
62
+ } else {
63
+ result = spawnSync('npm', ['install', '-g', packageName], {
64
+ stdio: 'inherit',
65
+ shell: process.platform === 'win32',
66
+ });
67
+ }
68
+
69
+ return result.status === 0;
70
+ }
71
+
37
72
  // 查找二进制文件路径
38
- function findBinary() {
73
+ function findBinary(autoInstall = false) {
39
74
  const platformKey = getPlatformKey();
40
75
  const packageName = PLATFORMS[platformKey];
41
76
 
@@ -49,13 +84,15 @@ function findBinary() {
49
84
  const binaryName = process.platform === 'win32' ? '42plugin.exe' : '42plugin';
50
85
 
51
86
  // 尝试多个可能的路径
87
+ // __dirname = .../node_modules/@42ailab/42plugin/bin
88
+ // packageName = @42ailab/42plugin-darwin-arm64 (包含 scope,需要从 node_modules 开始)
52
89
  const possiblePaths = [
53
- // 1. 作为依赖安装时的路径 (node_modules/@42ailab/42plugin-xxx/bin/42plugin)
54
- join(__dirname, '..', '..', packageName, 'bin', binaryName),
55
- // 2. 全局安装时的路径
90
+ // 1. bun/npm 全局安装路径: 回退到 node_modules 再拼接 packageName
91
+ join(__dirname, '..', '..', '..', packageName, 'bin', binaryName),
92
+ // 2. npm 嵌套 node_modules 路径
56
93
  join(__dirname, '..', 'node_modules', packageName, 'bin', binaryName),
57
- // 3. pnpm 的路径
58
- join(__dirname, '..', '..', '.pnpm', 'node_modules', packageName, 'bin', binaryName),
94
+ // 3. pnpm 路径
95
+ join(__dirname, '..', '..', '..', '.pnpm', 'node_modules', packageName, 'bin', binaryName),
59
96
  ];
60
97
 
61
98
  for (const binPath of possiblePaths) {
@@ -64,14 +101,28 @@ function findBinary() {
64
101
  }
65
102
  }
66
103
 
67
- // 找不到二进制文件
104
+ // 找不到二进制文件,尝试自动安装
105
+ if (!autoInstall) {
106
+ if (tryAutoInstall(packageName)) {
107
+ // 安装成功,重新查找
108
+ return findBinary(true);
109
+ }
110
+ }
111
+
112
+ // 自动安装失败,显示手动安装提示
113
+ console.error('');
68
114
  console.error(`错误: 找不到 ${platformKey} 平台的二进制文件`);
69
- console.error(`请确保已安装: ${packageName}`);
115
+ console.error(`请手动安装: ${packageName}`);
116
+ console.error('');
117
+ console.error('尝试以下方法:');
118
+ console.error('');
119
+ console.error(' # Bun 用户');
120
+ console.error(` bun add -g ${packageName}`);
70
121
  console.error('');
71
- console.error('尝试重新安装:');
122
+ console.error(' # npm 用户');
72
123
  console.error(' npm install -g @42ailab/42plugin');
73
124
  console.error('');
74
- console.error('或使用 Homebrew (macOS/Linux):');
125
+ console.error(' # Homebrew (macOS/Linux)');
75
126
  console.error(' brew install 42ailab/42plugin/42plugin');
76
127
  process.exit(1);
77
128
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@42ailab/42plugin",
3
- "version": "0.2.16",
3
+ "version": "0.2.18",
4
4
  "description": "42plugin CLI - AI 插件管理工具",
5
5
  "type": "module",
6
6
  "bin": {
@@ -32,10 +32,10 @@
32
32
  "node": ">=18.0.0"
33
33
  },
34
34
  "optionalDependencies": {
35
- "@42ailab/42plugin-darwin-arm64": "0.2.16",
36
- "@42ailab/42plugin-darwin-x64": "0.2.16",
37
- "@42ailab/42plugin-linux-arm64": "0.2.16",
38
- "@42ailab/42plugin-linux-x64": "0.2.16",
39
- "@42ailab/42plugin-win32-x64": "0.2.16"
35
+ "@42ailab/42plugin-darwin-arm64": "0.2.18",
36
+ "@42ailab/42plugin-darwin-x64": "0.2.18",
37
+ "@42ailab/42plugin-linux-arm64": "0.2.18",
38
+ "@42ailab/42plugin-linux-x64": "0.2.18",
39
+ "@42ailab/42plugin-win32-x64": "0.2.18"
40
40
  }
41
41
  }