@42ailab/42plugin 0.2.17 → 0.2.19

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 +39 -18
  2. package/package.json +6 -6
package/bin/42plugin CHANGED
@@ -8,7 +8,7 @@
8
8
  */
9
9
 
10
10
  import { spawnSync } from 'node:child_process';
11
- import { existsSync } from 'node:fs';
11
+ import { existsSync, readFileSync } from 'node:fs';
12
12
  import { join, dirname } from 'node:path';
13
13
  import { fileURLToPath } from 'node:url';
14
14
 
@@ -23,6 +23,17 @@ const PLATFORMS = {
23
23
  'win32-x64': '@42ailab/42plugin-win32-x64',
24
24
  };
25
25
 
26
+ // 获取主包版本号
27
+ function getMainPackageVersion() {
28
+ try {
29
+ const pkgPath = join(__dirname, '..', 'package.json');
30
+ const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
31
+ return pkg.version;
32
+ } catch {
33
+ return null;
34
+ }
35
+ }
36
+
26
37
  // 获取当前平台标识
27
38
  function getPlatformKey() {
28
39
  const platform = process.platform;
@@ -49,18 +60,20 @@ function detectPackageManager() {
49
60
  }
50
61
 
51
62
  // 尝试自动安装平台包
52
- function tryAutoInstall(packageName) {
63
+ function tryAutoInstall(packageName, version) {
53
64
  const pm = detectPackageManager();
54
- console.error(`正在自动安装平台包: ${packageName}...`);
65
+ // 使用具体版本号,避免 latest tag 同步问题
66
+ const packageSpec = version ? `${packageName}@${version}` : packageName;
67
+ console.error(`正在自动安装平台包: ${packageSpec}...`);
55
68
 
56
69
  let result;
57
70
  if (pm === 'bun') {
58
- result = spawnSync('bun', ['add', '-g', packageName], {
71
+ result = spawnSync('bun', ['add', '-g', packageSpec], {
59
72
  stdio: 'inherit',
60
73
  shell: process.platform === 'win32',
61
74
  });
62
75
  } else {
63
- result = spawnSync('npm', ['install', '-g', packageName], {
76
+ result = spawnSync('npm', ['install', '-g', packageSpec], {
64
77
  stdio: 'inherit',
65
78
  shell: process.platform === 'win32',
66
79
  });
@@ -73,6 +86,7 @@ function tryAutoInstall(packageName) {
73
86
  function findBinary(autoInstall = false) {
74
87
  const platformKey = getPlatformKey();
75
88
  const packageName = PLATFORMS[platformKey];
89
+ const version = getMainPackageVersion();
76
90
 
77
91
  if (!packageName) {
78
92
  console.error(`错误: 不支持的平台 ${platformKey}`);
@@ -84,13 +98,15 @@ function findBinary(autoInstall = false) {
84
98
  const binaryName = process.platform === 'win32' ? '42plugin.exe' : '42plugin';
85
99
 
86
100
  // 尝试多个可能的路径
101
+ // __dirname = .../node_modules/@42ailab/42plugin/bin
102
+ // packageName = @42ailab/42plugin-darwin-arm64 (包含 scope,需要从 node_modules 开始)
87
103
  const possiblePaths = [
88
- // 1. 作为依赖安装时的路径 (node_modules/@42ailab/42plugin-xxx/bin/42plugin)
89
- join(__dirname, '..', '..', packageName, 'bin', binaryName),
90
- // 2. 全局安装时的路径
104
+ // 1. bun/npm 全局安装路径: 回退到 node_modules 再拼接 packageName
105
+ join(__dirname, '..', '..', '..', packageName, 'bin', binaryName),
106
+ // 2. npm 嵌套 node_modules 路径
91
107
  join(__dirname, '..', 'node_modules', packageName, 'bin', binaryName),
92
- // 3. pnpm 的路径
93
- join(__dirname, '..', '..', '.pnpm', 'node_modules', packageName, 'bin', binaryName),
108
+ // 3. pnpm 路径
109
+ join(__dirname, '..', '..', '..', '.pnpm', 'node_modules', packageName, 'bin', binaryName),
94
110
  ];
95
111
 
96
112
  for (const binPath of possiblePaths) {
@@ -101,27 +117,32 @@ function findBinary(autoInstall = false) {
101
117
 
102
118
  // 找不到二进制文件,尝试自动安装
103
119
  if (!autoInstall) {
104
- if (tryAutoInstall(packageName)) {
120
+ if (tryAutoInstall(packageName, version)) {
105
121
  // 安装成功,重新查找
106
122
  return findBinary(true);
107
123
  }
108
124
  }
109
125
 
110
126
  // 自动安装失败,显示手动安装提示
127
+ const versionSpec = version ? `@${version}` : '';
111
128
  console.error('');
112
129
  console.error(`错误: 找不到 ${platformKey} 平台的二进制文件`);
113
- console.error(`请手动安装: ${packageName}`);
130
+ console.error(`请手动安装: ${packageName}${versionSpec}`);
114
131
  console.error('');
115
132
  console.error('尝试以下方法:');
116
133
  console.error('');
117
- console.error(' # Bun 用户');
118
- console.error(` bun add -g ${packageName}`);
134
+ console.error(' # 推荐: Homebrew (macOS/Linux)');
135
+ console.error(' brew install 42ailab/42plugin/42plugin');
119
136
  console.error('');
120
- console.error(' # npm 用户');
121
- console.error(' npm install -g @42ailab/42plugin');
137
+ console.error(' # npm 用户 (指定版本)');
138
+ console.error(` npm install -g ${packageName}${versionSpec}`);
122
139
  console.error('');
123
- console.error(' # Homebrew (macOS/Linux)');
124
- console.error(' brew install 42ailab/42plugin/42plugin');
140
+ console.error(' # Bun 用户 (指定版本)');
141
+ console.error(` bun add -g ${packageName}${versionSpec}`);
142
+ console.error('');
143
+ console.error('如果问题持续,请尝试清除缓存后重新安装:');
144
+ console.error(' npm cache clean --force');
145
+ console.error(' bun pm cache rm');
125
146
  process.exit(1);
126
147
  }
127
148
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@42ailab/42plugin",
3
- "version": "0.2.17",
3
+ "version": "0.2.19",
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.17",
36
- "@42ailab/42plugin-darwin-x64": "0.2.17",
37
- "@42ailab/42plugin-linux-arm64": "0.2.17",
38
- "@42ailab/42plugin-linux-x64": "0.2.17",
39
- "@42ailab/42plugin-win32-x64": "0.2.17"
35
+ "@42ailab/42plugin-darwin-arm64": "0.2.19",
36
+ "@42ailab/42plugin-darwin-x64": "0.2.19",
37
+ "@42ailab/42plugin-linux-arm64": "0.2.19",
38
+ "@42ailab/42plugin-linux-x64": "0.2.19",
39
+ "@42ailab/42plugin-win32-x64": "0.2.19"
40
40
  }
41
41
  }