@42ailab/42plugin 0.2.21 → 0.2.22

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 +40 -146
  2. package/package.json +7 -13
package/bin/42plugin CHANGED
@@ -2,166 +2,60 @@
2
2
 
3
3
  /**
4
4
  * 42plugin CLI 启动脚本
5
- *
6
- * 查找并执行对应平台的二进制文件
7
- * 参考 esbuild、biome 的实现方式
5
+ * 参考 @biomejs/biome 实现
8
6
  */
9
7
 
10
- import { spawnSync } from 'node:child_process';
11
- import { existsSync, readFileSync } from 'node:fs';
12
- import { join, dirname } from 'node:path';
13
- import { fileURLToPath } from 'node:url';
8
+ const { platform, arch, env, version } = process;
9
+ const { spawnSync } = require("child_process");
14
10
 
15
- const __dirname = dirname(fileURLToPath(import.meta.url));
16
-
17
- // 平台映射
18
11
  const PLATFORMS = {
19
- 'darwin-arm64': '@42ailab/42plugin-darwin-arm64',
20
- 'darwin-x64': '@42ailab/42plugin-darwin-x64',
21
- 'linux-arm64': '@42ailab/42plugin-linux-arm64',
22
- 'linux-x64': '@42ailab/42plugin-linux-x64',
23
- 'win32-x64': '@42ailab/42plugin-win32-x64',
12
+ win32: {
13
+ x64: "@42ailab/42plugin-win32-x64/bin/42plugin.exe",
14
+ },
15
+ darwin: {
16
+ x64: "@42ailab/42plugin-darwin-x64/bin/42plugin",
17
+ arm64: "@42ailab/42plugin-darwin-arm64/bin/42plugin",
18
+ },
19
+ linux: {
20
+ x64: "@42ailab/42plugin-linux-x64/bin/42plugin",
21
+ arm64: "@42ailab/42plugin-linux-arm64/bin/42plugin",
22
+ },
24
23
  };
25
24
 
26
- // 获取主包版本号
27
- function getMainPackageVersion() {
25
+ const binPath = env.PLUGIN_BINARY || PLATFORMS?.[platform]?.[arch];
26
+
27
+ if (binPath) {
28
+ let resolvedPath;
28
29
  try {
29
- const pkgPath = join(__dirname, '..', 'package.json');
30
- const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
31
- return pkg.version;
30
+ resolvedPath = require.resolve(binPath);
32
31
  } catch {
33
- return null;
34
- }
35
- }
36
-
37
- // 获取当前平台标识
38
- function getPlatformKey() {
39
- const platform = process.platform;
40
- const arch = process.arch;
41
-
42
- // x86_64 在 Node.js 中是 x64
43
- const normalizedArch = arch === 'x64' ? 'x64' : arch;
44
-
45
- return `${platform}-${normalizedArch}`;
46
- }
47
-
48
- // 检测包管理器
49
- function detectPackageManager() {
50
- // 检查是否通过 bun 运行
51
- if (process.versions.bun) {
52
- return 'bun';
53
- }
54
- // 检查全局安装路径中是否包含 bun
55
- if (__dirname.includes('.bun')) {
56
- return 'bun';
57
- }
58
- // 默认使用 npm
59
- return 'npm';
60
- }
61
-
62
- // 尝试自动安装平台包
63
- function tryAutoInstall(packageName, version) {
64
- const pm = detectPackageManager();
65
- // 使用具体版本号,避免 latest tag 同步问题
66
- const packageSpec = version ? `${packageName}@${version}` : packageName;
67
- console.error(`正在自动安装平台包: ${packageSpec}...`);
68
-
69
- let result;
70
- if (pm === 'bun') {
71
- result = spawnSync('bun', ['add', '-g', packageSpec], {
72
- stdio: 'inherit',
73
- shell: process.platform === 'win32',
74
- });
75
- } else {
76
- result = spawnSync('npm', ['install', '-g', packageSpec], {
77
- stdio: 'inherit',
78
- shell: process.platform === 'win32',
79
- });
80
- }
81
-
82
- return result.status === 0;
83
- }
84
-
85
- // 查找二进制文件路径
86
- function findBinary(autoInstall = false) {
87
- const platformKey = getPlatformKey();
88
- const packageName = PLATFORMS[platformKey];
89
- const version = getMainPackageVersion();
90
-
91
- if (!packageName) {
92
- console.error(`错误: 不支持的平台 ${platformKey}`);
93
- console.error(`支持的平台: ${Object.keys(PLATFORMS).join(', ')}`);
32
+ console.error(`错误: 找不到平台二进制包 ${binPath.split("/")[0]}`);
33
+ console.error("");
34
+ console.error("可能原因:");
35
+ console.error(" - 包管理器未正确安装 optionalDependencies");
36
+ console.error(" - 版本不匹配 (主包和二进制包版本不一致)");
37
+ console.error("");
38
+ console.error("解决方案:");
39
+ console.error(" # 完全重新安装");
40
+ console.error(" bun remove -g @42ailab/42plugin && bun add -g @42ailab/42plugin");
41
+ console.error("");
42
+ console.error(" # 或使用 Homebrew (推荐)");
43
+ console.error(" brew install 42ailab/42plugin/42plugin");
94
44
  process.exit(1);
95
45
  }
96
46
 
97
- // 可能的二进制文件名
98
- const binaryName = process.platform === 'win32' ? '42plugin.exe' : '42plugin';
99
-
100
- // 尝试多个可能的路径
101
- // __dirname = .../node_modules/@42ailab/42plugin/bin
102
- // packageName = @42ailab/42plugin-darwin-arm64 (包含 scope,需要从 node_modules 开始)
103
- const possiblePaths = [
104
- // 1. bun/npm 全局安装路径: 回退到 node_modules 再拼接 packageName
105
- join(__dirname, '..', '..', '..', packageName, 'bin', binaryName),
106
- // 2. npm 嵌套 node_modules 路径
107
- join(__dirname, '..', 'node_modules', packageName, 'bin', binaryName),
108
- // 3. pnpm 路径
109
- join(__dirname, '..', '..', '..', '.pnpm', 'node_modules', packageName, 'bin', binaryName),
110
- ];
111
-
112
- for (const binPath of possiblePaths) {
113
- if (existsSync(binPath)) {
114
- return binPath;
115
- }
116
- }
117
-
118
- // 找不到二进制文件,尝试自动安装
119
- if (!autoInstall) {
120
- if (tryAutoInstall(packageName, version)) {
121
- // 安装成功,重新查找
122
- return findBinary(true);
123
- }
124
- }
125
-
126
- // 自动安装失败,显示手动安装提示
127
- const versionSpec = version ? `@${version}` : '';
128
- console.error('');
129
- console.error(`错误: 找不到 ${platformKey} 平台的二进制文件`);
130
- console.error(`请手动安装: ${packageName}${versionSpec}`);
131
- console.error('');
132
- console.error('尝试以下方法:');
133
- console.error('');
134
- console.error(' # 推荐: Homebrew (macOS/Linux)');
135
- console.error(' brew install 42ailab/42plugin/42plugin');
136
- console.error('');
137
- console.error(' # npm 用户 (指定版本)');
138
- console.error(` npm install -g ${packageName}${versionSpec}`);
139
- console.error('');
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');
146
- process.exit(1);
147
- }
148
-
149
- // 执行二进制文件
150
- function run() {
151
- const binPath = findBinary();
152
- const args = process.argv.slice(2);
153
-
154
- const result = spawnSync(binPath, args, {
155
- stdio: 'inherit',
156
- shell: false,
47
+ const result = spawnSync(resolvedPath, process.argv.slice(2), {
48
+ stdio: "inherit",
49
+ env: { ...env, JS_RUNTIME_VERSION: version },
157
50
  });
158
51
 
159
52
  if (result.error) {
160
- console.error(`执行失败: ${result.error.message}`);
161
- process.exit(1);
53
+ throw result.error;
162
54
  }
163
55
 
164
- process.exit(result.status ?? 0);
56
+ process.exitCode = result.status;
57
+ } else {
58
+ console.error(`错误: 不支持的平台 ${platform}-${arch}`);
59
+ console.error(`支持的平台: darwin-arm64, darwin-x64, linux-arm64, linux-x64, win32-x64`);
60
+ process.exit(1);
165
61
  }
166
-
167
- run();
package/package.json CHANGED
@@ -1,22 +1,19 @@
1
1
  {
2
2
  "name": "@42ailab/42plugin",
3
- "version": "0.2.21",
3
+ "version": "0.2.22",
4
4
  "description": "42plugin CLI - AI 插件管理工具",
5
- "type": "module",
6
5
  "bin": {
7
6
  "42plugin": "bin/42plugin"
8
7
  },
9
8
  "files": [
10
9
  "bin"
11
10
  ],
12
- "scripts": {},
13
11
  "keywords": [
14
12
  "claude",
15
13
  "claude-code",
16
14
  "plugin",
17
15
  "cli",
18
- "ai",
19
- "anthropic"
16
+ "ai"
20
17
  ],
21
18
  "author": "42ailab <y@42ailab.com>",
22
19
  "license": "MIT",
@@ -24,18 +21,15 @@
24
21
  "type": "git",
25
22
  "url": "git+https://github.com/42ailab/42plugin.git"
26
23
  },
27
- "bugs": {
28
- "url": "https://github.com/42ailab/42plugin/issues"
29
- },
30
24
  "homepage": "https://42plugin.com",
31
25
  "engines": {
32
26
  "node": ">=18.0.0"
33
27
  },
34
28
  "optionalDependencies": {
35
- "@42ailab/42plugin-darwin-arm64": "0.2.21",
36
- "@42ailab/42plugin-darwin-x64": "0.2.21",
37
- "@42ailab/42plugin-linux-arm64": "0.2.21",
38
- "@42ailab/42plugin-linux-x64": "0.2.21",
39
- "@42ailab/42plugin-win32-x64": "0.2.21"
29
+ "@42ailab/42plugin-darwin-arm64": "0.2.22",
30
+ "@42ailab/42plugin-darwin-x64": "0.2.22",
31
+ "@42ailab/42plugin-linux-arm64": "0.2.22",
32
+ "@42ailab/42plugin-linux-x64": "0.2.22",
33
+ "@42ailab/42plugin-win32-x64": "0.2.22"
40
34
  }
41
35
  }