@rse/claudex 1.0.13 → 1.0.14

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 (3) hide show
  1. package/claudex.js +42 -25
  2. package/claudex.ts +45 -25
  3. package/package.json +1 -1
package/claudex.js CHANGED
@@ -124,7 +124,7 @@ const execInherit = (file, args, opts = {}) => {
124
124
  const executeCommand = (config) => {
125
125
  const platform = detectPlatform();
126
126
  const osName = platform.split(":")[0];
127
- const command = config[platform] ?? config[`${osName}:*`];
127
+ const command = config[platform] ?? config[`${osName}:*`] ?? config["*"];
128
128
  if (command === undefined)
129
129
  fatal(`no command configured for platform "${platform}"`);
130
130
  const cmd = command instanceof Array ? command : command.split(/\s+/);
@@ -137,6 +137,39 @@ const executeCommand = (config) => {
137
137
  });
138
138
  return result.exitCode;
139
139
  };
140
+ /* helper to determine whether a global NPM installation requires sudo(8),
141
+ i.e., whether the target directories of the NPM global prefix are not
142
+ writable by us. The result is cached after the first call. */
143
+ let npmNeedsSudoCached = null;
144
+ const npmNeedsSudo = () => {
145
+ if (npmNeedsSudoCached !== null)
146
+ return npmNeedsSudoCached;
147
+ npmNeedsSudoCached = false;
148
+ if (process.platform === "win32" || (process.getuid?.() ?? 0) === 0 || findTool("sudo") === null)
149
+ return npmNeedsSudoCached;
150
+ const r = execaSync("npm", ["prefix", "-g"], { reject: false });
151
+ const prefix = (r.stdout ?? "").trim();
152
+ if ((r.exitCode ?? 1) !== 0 || prefix === "") {
153
+ /* on a failed detection stay with the conservative variant */
154
+ npmNeedsSudoCached = true;
155
+ return npmNeedsSudoCached;
156
+ }
157
+ npmNeedsSudoCached = ![path.join(prefix, "lib", "node_modules"), path.join(prefix, "bin")].every((dir) => {
158
+ /* fall back to the nearest already existing ancestor directory */
159
+ while (!fs.existsSync(dir) && path.dirname(dir) !== dir)
160
+ dir = path.dirname(dir);
161
+ try {
162
+ fs.accessSync(dir, fs.constants.W_OK);
163
+ return true;
164
+ }
165
+ catch (_e) {
166
+ return false;
167
+ }
168
+ });
169
+ return npmNeedsSudoCached;
170
+ };
171
+ /* helper to assemble a global NPM installation command */
172
+ const npmInstallGlobal = (...args) => `${npmNeedsSudo() ? "sudo " : ""}npm install -g ${args.join(" ")}`;
140
173
  /* helper to ensure a tool is available */
141
174
  const ensureTool = (tool, options = {}) => {
142
175
  const tools = typeof tool === "string" ? [tool] : tool;
@@ -340,36 +373,28 @@ const actionInstall = async (capsula) => {
340
373
  ensureTool("ansi-recolor", {
341
374
  hint: "https://github.com/rse/ansi-recolor",
342
375
  install: {
343
- "windows:*": "npm install -g --allow-scripts=node-pty,tty-attr ansi-recolor",
344
- "macos:*": "sudo npm install -g --allow-scripts=node-pty,tty-attr ansi-recolor",
345
- "linux:*": "sudo npm install -g --allow-scripts=node-pty,tty-attr ansi-recolor"
376
+ "*": npmInstallGlobal("--allow-scripts=node-pty,tty-attr", "ansi-recolor")
346
377
  }
347
378
  });
348
379
  info("install TypeScript-Language-Server");
349
380
  ensureTool("typescript-language-server", {
350
381
  hint: "https://github.com/typescript-language-server/typescript-language-server",
351
382
  install: {
352
- "windows:*": "npm install -g typescript-language-server",
353
- "macos:*": "sudo npm install -g typescript-language-server",
354
- "linux:*": "sudo npm install -g typescript-language-server"
383
+ "*": npmInstallGlobal("typescript-language-server")
355
384
  }
356
385
  });
357
386
  info("install CodeBurn");
358
387
  ensureTool("codeburn", {
359
388
  hint: "https://www.npmjs.com/package/codeburn",
360
389
  install: {
361
- "windows:*": "npm install -g codeburn",
362
- "macos:*": "sudo npm install -g codeburn",
363
- "linux:*": "sudo npm install -g codeburn"
390
+ "*": npmInstallGlobal("codeburn")
364
391
  }
365
392
  });
366
393
  info("install ASE");
367
394
  ensureTool("ase", {
368
395
  hint: "https://ase.tools",
369
396
  install: {
370
- "windows:*": "npm install -g @rse/ase",
371
- "macos:*": "sudo npm install -g @rse/ase",
372
- "linux:*": "sudo npm install -g @rse/ase"
397
+ "*": npmInstallGlobal("@rse/ase")
373
398
  }
374
399
  });
375
400
  info("install Claude Code");
@@ -466,27 +491,19 @@ const actionUpdate = async (capsula) => {
466
491
  });
467
492
  info("update ANSI-Recolor");
468
493
  executeCommand({
469
- "windows:*": "npm install -g --allow-scripts=node-pty,tty-attr ansi-recolor",
470
- "macos:*": "sudo npm install -g --allow-scripts=node-pty,tty-attr ansi-recolor",
471
- "linux:*": "sudo npm install -g --allow-scripts=node-pty,tty-attr ansi-recolor"
494
+ "*": npmInstallGlobal("--allow-scripts=node-pty,tty-attr", "ansi-recolor")
472
495
  });
473
496
  info("update TypeScript-Language-Server");
474
497
  executeCommand({
475
- "windows:*": "npm install -g typescript-language-server",
476
- "macos:*": "sudo npm install -g typescript-language-server",
477
- "linux:*": "sudo npm install -g typescript-language-server"
498
+ "*": npmInstallGlobal("typescript-language-server")
478
499
  });
479
500
  info("update CodeBurn");
480
501
  executeCommand({
481
- "windows:*": "npm install -g codeburn",
482
- "macos:*": "sudo npm install -g codeburn",
483
- "linux:*": "sudo npm install -g codeburn"
502
+ "*": npmInstallGlobal("codeburn")
484
503
  });
485
504
  info("update ASE");
486
505
  executeCommand({
487
- "windows:*": "npm install -g @rse/ase",
488
- "macos:*": "sudo npm install -g @rse/ase",
489
- "linux:*": "sudo npm install -g @rse/ase"
506
+ "*": npmInstallGlobal("@rse/ase")
490
507
  });
491
508
  info("update Claude Code");
492
509
  if (process.platform !== "win32") {
package/claudex.ts CHANGED
@@ -139,7 +139,7 @@ const execInherit = (file: string, args: string[], opts: { env?: Env } = {}): ne
139
139
  const executeCommand = (config: { [ platform: string ]: string[] | string }) => {
140
140
  const platform = detectPlatform()
141
141
  const osName = platform.split(":")[0]
142
- const command = config[platform] ?? config[`${osName}:*`]
142
+ const command = config[platform] ?? config[`${osName}:*`] ?? config["*"]
143
143
  if (command === undefined)
144
144
  fatal(`no command configured for platform "${platform}"`)
145
145
  const cmd = command instanceof Array ? command : command.split(/\s+/)
@@ -153,6 +153,42 @@ const executeCommand = (config: { [ platform: string ]: string[] | string }) =>
153
153
  return result.exitCode
154
154
  }
155
155
 
156
+ /* helper to determine whether a global NPM installation requires sudo(8),
157
+ i.e., whether the target directories of the NPM global prefix are not
158
+ writable by us. The result is cached after the first call. */
159
+ let npmNeedsSudoCached: boolean | null = null
160
+ const npmNeedsSudo = (): boolean => {
161
+ if (npmNeedsSudoCached !== null)
162
+ return npmNeedsSudoCached
163
+ npmNeedsSudoCached = false
164
+ if (process.platform === "win32" || (process.getuid?.() ?? 0) === 0 || findTool("sudo") === null)
165
+ return npmNeedsSudoCached
166
+ const r = execaSync("npm", [ "prefix", "-g" ], { reject: false })
167
+ const prefix = (r.stdout ?? "").trim()
168
+ if ((r.exitCode ?? 1) !== 0 || prefix === "") {
169
+ /* on a failed detection stay with the conservative variant */
170
+ npmNeedsSudoCached = true
171
+ return npmNeedsSudoCached
172
+ }
173
+ npmNeedsSudoCached = ![ path.join(prefix, "lib", "node_modules"), path.join(prefix, "bin") ].every((dir) => {
174
+ /* fall back to the nearest already existing ancestor directory */
175
+ while (!fs.existsSync(dir) && path.dirname(dir) !== dir)
176
+ dir = path.dirname(dir)
177
+ try {
178
+ fs.accessSync(dir, fs.constants.W_OK)
179
+ return true
180
+ }
181
+ catch (_e) {
182
+ return false
183
+ }
184
+ })
185
+ return npmNeedsSudoCached
186
+ }
187
+
188
+ /* helper to assemble a global NPM installation command */
189
+ const npmInstallGlobal = (...args: string[]): string =>
190
+ `${npmNeedsSudo() ? "sudo " : ""}npm install -g ${args.join(" ")}`
191
+
156
192
  /* helper to ensure a tool is available */
157
193
  const ensureTool = (tool: string | string[], options: {
158
194
  install?: { [ platform: string ]: string[] | string },
@@ -392,9 +428,7 @@ const actionInstall = async (capsula: boolean): Promise<void> => {
392
428
  ensureTool("ansi-recolor", {
393
429
  hint: "https://github.com/rse/ansi-recolor",
394
430
  install: {
395
- "windows:*": "npm install -g --allow-scripts=node-pty,tty-attr ansi-recolor",
396
- "macos:*": "sudo npm install -g --allow-scripts=node-pty,tty-attr ansi-recolor",
397
- "linux:*": "sudo npm install -g --allow-scripts=node-pty,tty-attr ansi-recolor"
431
+ "*": npmInstallGlobal("--allow-scripts=node-pty,tty-attr", "ansi-recolor")
398
432
  }
399
433
  })
400
434
 
@@ -402,9 +436,7 @@ const actionInstall = async (capsula: boolean): Promise<void> => {
402
436
  ensureTool("typescript-language-server", {
403
437
  hint: "https://github.com/typescript-language-server/typescript-language-server",
404
438
  install: {
405
- "windows:*": "npm install -g typescript-language-server",
406
- "macos:*": "sudo npm install -g typescript-language-server",
407
- "linux:*": "sudo npm install -g typescript-language-server"
439
+ "*": npmInstallGlobal("typescript-language-server")
408
440
  }
409
441
  })
410
442
 
@@ -412,9 +444,7 @@ const actionInstall = async (capsula: boolean): Promise<void> => {
412
444
  ensureTool("codeburn", {
413
445
  hint: "https://www.npmjs.com/package/codeburn",
414
446
  install: {
415
- "windows:*": "npm install -g codeburn",
416
- "macos:*": "sudo npm install -g codeburn",
417
- "linux:*": "sudo npm install -g codeburn"
447
+ "*": npmInstallGlobal("codeburn")
418
448
  }
419
449
  })
420
450
 
@@ -422,9 +452,7 @@ const actionInstall = async (capsula: boolean): Promise<void> => {
422
452
  ensureTool("ase", {
423
453
  hint: "https://ase.tools",
424
454
  install: {
425
- "windows:*": "npm install -g @rse/ase",
426
- "macos:*": "sudo npm install -g @rse/ase",
427
- "linux:*": "sudo npm install -g @rse/ase"
455
+ "*": npmInstallGlobal("@rse/ase")
428
456
  }
429
457
  })
430
458
 
@@ -538,30 +566,22 @@ const actionUpdate = async (capsula: boolean): Promise<void> => {
538
566
 
539
567
  info("update ANSI-Recolor")
540
568
  executeCommand({
541
- "windows:*": "npm install -g --allow-scripts=node-pty,tty-attr ansi-recolor",
542
- "macos:*": "sudo npm install -g --allow-scripts=node-pty,tty-attr ansi-recolor",
543
- "linux:*": "sudo npm install -g --allow-scripts=node-pty,tty-attr ansi-recolor"
569
+ "*": npmInstallGlobal("--allow-scripts=node-pty,tty-attr", "ansi-recolor")
544
570
  })
545
571
 
546
572
  info("update TypeScript-Language-Server")
547
573
  executeCommand({
548
- "windows:*": "npm install -g typescript-language-server",
549
- "macos:*": "sudo npm install -g typescript-language-server",
550
- "linux:*": "sudo npm install -g typescript-language-server"
574
+ "*": npmInstallGlobal("typescript-language-server")
551
575
  })
552
576
 
553
577
  info("update CodeBurn")
554
578
  executeCommand({
555
- "windows:*": "npm install -g codeburn",
556
- "macos:*": "sudo npm install -g codeburn",
557
- "linux:*": "sudo npm install -g codeburn"
579
+ "*": npmInstallGlobal("codeburn")
558
580
  })
559
581
 
560
582
  info("update ASE")
561
583
  executeCommand({
562
- "windows:*": "npm install -g @rse/ase",
563
- "macos:*": "sudo npm install -g @rse/ase",
564
- "linux:*": "sudo npm install -g @rse/ase"
584
+ "*": npmInstallGlobal("@rse/ase")
565
585
  })
566
586
 
567
587
  info("update Claude Code")
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rse/claudex",
3
3
  "publishConfig": { "access": "public" },
4
- "version": "1.0.13",
4
+ "version": "1.0.14",
5
5
  "description": "Claude Code eXtended",
6
6
  "type": "module",
7
7
  "keywords": [ "claude", "code", "extension", "wrapper" ],