openclawsetup 2.0.1 → 2.0.2

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 +37 -3
  2. package/package.json +1 -1
package/bin/cli.mjs CHANGED
@@ -12,7 +12,7 @@
12
12
  */
13
13
 
14
14
  import { execSync, spawn } from 'child_process';
15
- import { existsSync, mkdirSync, writeFileSync, readFileSync, rmSync } from 'fs';
15
+ import { existsSync, mkdirSync, writeFileSync, readFileSync, rmSync, accessSync, constants as fsConstants } from 'fs';
16
16
  import { homedir, platform } from 'os';
17
17
  import { join } from 'path';
18
18
  import { createInterface } from 'readline';
@@ -188,11 +188,44 @@ function detectExistingInstall() {
188
188
 
189
189
  // ============ 安装 OpenClaw ============
190
190
 
191
+ // 检测是否需要 sudo
192
+ function needsSudo() {
193
+ const os = platform();
194
+ if (os === 'win32' || os === 'darwin') return false;
195
+
196
+ // Linux: 检查 /usr/lib/node_modules 是否可写
197
+ try {
198
+ const testDir = '/usr/lib/node_modules';
199
+ if (existsSync(testDir)) {
200
+ accessSync(testDir, fsConstants.W_OK);
201
+ return false;
202
+ }
203
+ } catch {
204
+ return true;
205
+ }
206
+ return true;
207
+ }
208
+
209
+ // 获取 npm 安装命令
210
+ function getNpmInstallCmd(pkg) {
211
+ const useSudo = needsSudo();
212
+ if (useSudo) {
213
+ return { cmd: 'sudo', args: ['npm', 'install', '-g', pkg] };
214
+ }
215
+ return { cmd: 'npm', args: ['install', '-g', pkg] };
216
+ }
217
+
191
218
  async function installOpenClaw() {
192
219
  log.info('\n[1/2] 安装 OpenClaw CLI...\n');
193
220
 
221
+ const useSudo = needsSudo();
222
+ if (useSudo) {
223
+ log.hint('检测到需要 sudo 权限,可能需要输入密码\n');
224
+ }
225
+
194
226
  return new Promise((resolve, reject) => {
195
- const child = spawn('npm', ['install', '-g', 'openclaw@latest'], {
227
+ const { cmd, args } = getNpmInstallCmd('openclaw@latest');
228
+ const child = spawn(cmd, args, {
196
229
  stdio: 'inherit',
197
230
  shell: true,
198
231
  });
@@ -204,7 +237,8 @@ async function installOpenClaw() {
204
237
  } else {
205
238
  // 尝试 clawdbot
206
239
  log.warn('openclaw 安装失败,尝试 clawdbot...');
207
- const fallback = spawn('npm', ['install', '-g', 'clawdbot@latest'], {
240
+ const { cmd: cmd2, args: args2 } = getNpmInstallCmd('clawdbot@latest');
241
+ const fallback = spawn(cmd2, args2, {
208
242
  stdio: 'inherit',
209
243
  shell: true,
210
244
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openclawsetup",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "OpenClaw 智能安装向导 - 调用官方交互界面,自动选择推荐配置",
5
5
  "type": "module",
6
6
  "bin": {