openclawsetup 2.4.5 → 2.4.6

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/cli.mjs +49 -10
  2. package/package.json +1 -1
package/bin/cli.mjs CHANGED
@@ -148,16 +148,41 @@ function checkNodeVersion() {
148
148
 
149
149
  function needsSudo() {
150
150
  const os = platform();
151
- if (os === 'win32' || os === 'darwin') return false;
151
+ if (os === 'win32') return false;
152
152
 
153
- try {
154
- const testDir = '/usr/lib/node_modules';
155
- if (existsSync(testDir)) {
156
- accessSync(testDir, fsConstants.W_OK);
157
- return false;
153
+ // 获取 npm 全局安装目录并检查写权限
154
+ const npmPrefixResult = safeExec('npm prefix -g');
155
+ if (npmPrefixResult.ok && npmPrefixResult.output) {
156
+ const globalDir = join(npmPrefixResult.output.trim(), 'lib', 'node_modules');
157
+ try {
158
+ if (existsSync(globalDir)) {
159
+ accessSync(globalDir, fsConstants.W_OK);
160
+ return false;
161
+ }
162
+ // 目录不存在,检查父目录
163
+ const parentDir = npmPrefixResult.output.trim();
164
+ if (existsSync(parentDir)) {
165
+ accessSync(parentDir, fsConstants.W_OK);
166
+ return false;
167
+ }
168
+ } catch {
169
+ return true;
170
+ }
171
+ }
172
+
173
+ // 回退检查
174
+ const testDirs = os === 'darwin'
175
+ ? ['/usr/local/lib/node_modules', '/opt/homebrew/lib/node_modules']
176
+ : ['/usr/lib/node_modules'];
177
+ for (const testDir of testDirs) {
178
+ try {
179
+ if (existsSync(testDir)) {
180
+ accessSync(testDir, fsConstants.W_OK);
181
+ return false;
182
+ }
183
+ } catch {
184
+ return true;
158
185
  }
159
- } catch {
160
- return true;
161
186
  }
162
187
  return true;
163
188
  }
@@ -397,7 +422,8 @@ async function installOpenClaw() {
397
422
  console.log(colors.bold(colors.cyan('\n[1/2] 安装 OpenClaw CLI\n')));
398
423
 
399
424
  if (useSudo) {
400
- log.hint('Linux 系统需要 sudo 权限安装全局包');
425
+ const osName = platform() === 'darwin' ? 'macOS' : 'Linux';
426
+ log.hint(`${osName} 系统需要 sudo 权限安装全局包`);
401
427
  console.log(colors.yellow('\n请运行以下命令:'));
402
428
  console.log(colors.green(' sudo npm install -g openclaw@latest\n'));
403
429
 
@@ -413,7 +439,7 @@ async function installOpenClaw() {
413
439
  return 'openclaw';
414
440
  }
415
441
 
416
- // macOS 或有权限的 Linux:直接安装
442
+ // 有权限:直接安装
417
443
  console.log(colors.gray('正在安装 openclaw...\n'));
418
444
 
419
445
  const result = spawnSync('npm', ['install', '-g', 'openclaw@latest'], {
@@ -426,6 +452,19 @@ async function installOpenClaw() {
426
452
  return 'openclaw';
427
453
  }
428
454
 
455
+ // 安装失败,可能是权限问题,尝试 sudo(非 Windows)
456
+ if (platform() !== 'win32') {
457
+ log.warn('安装失败,可能是权限不足,尝试使用 sudo...');
458
+ const sudoResult = spawnSync('sudo', ['npm', 'install', '-g', 'openclaw@latest'], {
459
+ stdio: 'inherit',
460
+ shell: true,
461
+ });
462
+ if (sudoResult.status === 0) {
463
+ log.success('OpenClaw CLI 安装完成(sudo)');
464
+ return 'openclaw';
465
+ }
466
+ }
467
+
429
468
  // 尝试 clawdbot
430
469
  log.warn('openclaw 安装失败,尝试 clawdbot...');
431
470
  const fallback = spawnSync('npm', ['install', '-g', 'clawdbot@latest'], {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openclawsetup",
3
- "version": "2.4.5",
3
+ "version": "2.4.6",
4
4
  "description": "OpenClaw 安装向导 - 智能安装、诊断、自动修复",
5
5
  "type": "module",
6
6
  "bin": {