xclaude-cli 0.2.2 → 0.3.0
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.
- package/README.md +3 -2
- package/dist/xclaude.js +367 -54
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -34,7 +34,8 @@ Update with `npm i -g xclaude-cli@latest`; xclaude checks npm once a day and pri
|
|
|
34
34
|
| `xclaude set main --enable\|--disable` | use the login in `~/.claude` (plain `claude`'s) as the account `main` |
|
|
35
35
|
| `xclaude ls` | accounts with their login, defaults and config dir |
|
|
36
36
|
| `xclaude rm <name> [--keep-login] [-y]` | remove an account and log it out; its folder keeps its links, so its old conversations still open their saved long outputs, and files of its own, such as a `CLAUDE.md` you edited with `/memory`. Run it on the name again to delete that folder (only once it holds nothing but links) |
|
|
37
|
-
| `xclaude rm --leftovers [-y]` | delete the leftover folders of all removed accounts, after a confirmation |
|
|
37
|
+
| `xclaude rm --leftovers [-y]` | delete the leftover folders of all removed and renamed accounts, after a confirmation |
|
|
38
|
+
| `xclaude rename <name> <new-name> [-y]` | rename an account: its folder, settings, defaults and tmux sessions follow, and on Linux its login. On macOS the login is tied to the folder's path, so the account is logged out first (after a confirmation) and you log in again. Quit the account's sessions first. Like `rm`, it leaves a folder of links under the old name for old conversations |
|
|
38
39
|
| `xclaude tmux new\|attach\|ls\|kill` | Claude Code in tmux sessions, by label |
|
|
39
40
|
| `xclaude shell install\|uninstall [--bash] [--zsh]` | completion (and the guard) in your shell |
|
|
40
41
|
| `xclaude shell completion bash\|zsh` | print the completion script, to load it some other way |
|
|
@@ -77,7 +78,7 @@ Sharing works by linking each account's directories into `~/.claude`, which plai
|
|
|
77
78
|
|
|
78
79
|
## Files
|
|
79
80
|
|
|
80
|
-
- `~/.xclaude/config.json` holds accounts, defaults and settings. Most of it is edited through the commands; by hand you can add `share.add` / `share.remove` (extra directories to share, or built-in ones to stop sharing), `claudePath` (a fixed `claude` binary) and `tmux.statusRight` ([docs/architecture.md](docs/architecture.md#2-layout) has the format). `~/.xclaude/accounts/<name>/` is each account's `CLAUDE_CONFIG_DIR` (`xclaude ls` prints it, e.g. for an editor's environment settings). `XCLAUDE_HOME` moves `~/.xclaude`; set it before adding accounts, since on macOS the login is tied to the exact directory path and moving it later means logging in again.
|
|
81
|
+
- `~/.xclaude/config.json` holds accounts, defaults and settings. Most of it is edited through the commands; by hand you can add `share.add` / `share.remove` (extra directories to share, or built-in ones to stop sharing), `claudePath` (a fixed `claude` binary) and `tmux.statusRight` ([docs/architecture.md](docs/architecture.md#2-layout) has the format). `~/.xclaude/accounts/<name>/` is each account's `CLAUDE_CONFIG_DIR` (`xclaude ls` prints it, e.g. for an editor's environment settings; after `xclaude rename`, use the new one there). `XCLAUDE_HOME` moves `~/.xclaude`; set it before adding accounts, since on macOS the login is tied to the exact directory path and moving it later means logging in again.
|
|
81
82
|
- `~/.claude` stays the shared store; nothing in it moves.
|
|
82
83
|
|
|
83
84
|
## Uninstall
|
package/dist/xclaude.js
CHANGED
|
@@ -531,7 +531,7 @@ function stamp(now = /* @__PURE__ */ new Date()) {
|
|
|
531
531
|
|
|
532
532
|
// src/core/config.ts
|
|
533
533
|
var EFFORTS = ["low", "medium", "high", "xhigh", "max", "ultracode"];
|
|
534
|
-
var COMMANDS = ["add", "rm", "ls", "set", "tmux", "doctor", "shell", "guard", "help"];
|
|
534
|
+
var COMMANDS = ["add", "rm", "rename", "ls", "set", "tmux", "doctor", "shell", "guard", "help"];
|
|
535
535
|
var MAIN = "main";
|
|
536
536
|
var ACCOUNT_NAME = /^[a-z][a-z0-9-]{0,31}$/;
|
|
537
537
|
function defaultConfig() {
|
|
@@ -548,9 +548,9 @@ function defaultConfig() {
|
|
|
548
548
|
function isEffort(value) {
|
|
549
549
|
return EFFORTS.includes(value);
|
|
550
550
|
}
|
|
551
|
-
function accountNameProblem(name) {
|
|
551
|
+
function accountNameProblem(name, opts = {}) {
|
|
552
552
|
if (name === MAIN) return `"${MAIN}" is reserved for the main identity (xclaude set main --enable)`;
|
|
553
|
-
if (COMMANDS.includes(name)) return `"${name}" is reserved: it's an xclaude command`;
|
|
553
|
+
if (!opts.existing && COMMANDS.includes(name)) return `"${name}" is reserved: it's an xclaude command`;
|
|
554
554
|
if (name.startsWith("-") || name.startsWith("_")) return `"${name}" is reserved: names can't start with - or _`;
|
|
555
555
|
if (!ACCOUNT_NAME.test(name)) {
|
|
556
556
|
return `"${name}" isn't a valid account name: use lowercase letters, digits and dashes, start with a letter, at most 32 characters`;
|
|
@@ -596,7 +596,7 @@ function validateConfig(raw) {
|
|
|
596
596
|
if (!isObject(accountsRaw)) throw new ConfigProblem("accounts must be an object");
|
|
597
597
|
const accounts = {};
|
|
598
598
|
for (const [name, value] of Object.entries(accountsRaw)) {
|
|
599
|
-
const problem2 = accountNameProblem(name);
|
|
599
|
+
const problem2 = accountNameProblem(name, { existing: true });
|
|
600
600
|
if (problem2) throw new ConfigProblem(`accounts: ${problem2}`);
|
|
601
601
|
accounts[name] = parseDefaults(value, `accounts.${name}.`);
|
|
602
602
|
}
|
|
@@ -657,6 +657,11 @@ function saveConfig(paths, config) {
|
|
|
657
657
|
writeFileAtomic(paths.config, `${JSON.stringify(config, null, 2)}
|
|
658
658
|
`, { mode: 384, fsync: true });
|
|
659
659
|
}
|
|
660
|
+
function renameAccount(config, from, to) {
|
|
661
|
+
const accounts = {};
|
|
662
|
+
for (const [name, defaults] of Object.entries(config.accounts)) accounts[name === from ? to : name] = defaults;
|
|
663
|
+
config.accounts = accounts;
|
|
664
|
+
}
|
|
660
665
|
function identities(config) {
|
|
661
666
|
const names = Object.keys(config.accounts);
|
|
662
667
|
if (config.main.enabled) names.push(MAIN);
|
|
@@ -852,6 +857,19 @@ function forgetAccount(state, account) {
|
|
|
852
857
|
state.inheritNoted = state.inheritNoted.filter((name) => name !== account);
|
|
853
858
|
delete state.credentialNoted[account];
|
|
854
859
|
}
|
|
860
|
+
function renameInState(state, from, to) {
|
|
861
|
+
forgetAccount(state, to);
|
|
862
|
+
for (const [dir, name] of Object.entries(state.lastAccountByDir)) {
|
|
863
|
+
if (name === from) state.lastAccountByDir[dir] = to;
|
|
864
|
+
}
|
|
865
|
+
if (state.lastUsedAccount === from) state.lastUsedAccount = to;
|
|
866
|
+
for (const key of ["seenUnknownEntries", "listedPlugins", "turnedOnPlugins", "credentialNoted"]) {
|
|
867
|
+
if (!Object.hasOwn(state[key], from)) continue;
|
|
868
|
+
state[key][to] = state[key][from];
|
|
869
|
+
delete state[key][from];
|
|
870
|
+
}
|
|
871
|
+
state.inheritNoted = state.inheritNoted.map((name) => name === from ? to : name);
|
|
872
|
+
}
|
|
855
873
|
|
|
856
874
|
// src/link/inherit.ts
|
|
857
875
|
import fs10 from "node:fs";
|
|
@@ -1890,6 +1908,7 @@ var APPENDIX_A = [
|
|
|
1890
1908
|
"policy-limits.json",
|
|
1891
1909
|
"remote-settings.json",
|
|
1892
1910
|
"hfi-auth.json",
|
|
1911
|
+
"config.json",
|
|
1893
1912
|
"daemon",
|
|
1894
1913
|
"jobs",
|
|
1895
1914
|
"teams",
|
|
@@ -2716,6 +2735,35 @@ import { spawnSync as spawnSync2 } from "node:child_process";
|
|
|
2716
2735
|
import fs23 from "node:fs";
|
|
2717
2736
|
import path17 from "node:path";
|
|
2718
2737
|
|
|
2738
|
+
// src/grammar.ts
|
|
2739
|
+
var HIDDEN = ["__complete", "__names", "__update-check"];
|
|
2740
|
+
function isCommand(word) {
|
|
2741
|
+
return COMMANDS.includes(word);
|
|
2742
|
+
}
|
|
2743
|
+
function isHidden(word) {
|
|
2744
|
+
return HIDDEN.includes(word);
|
|
2745
|
+
}
|
|
2746
|
+
function classify(argv, config) {
|
|
2747
|
+
const [first, ...rest] = argv;
|
|
2748
|
+
if (first === void 0) return { kind: "launch", account: null, args: [] };
|
|
2749
|
+
if (first === "-v" || first === "--version") return { kind: "version" };
|
|
2750
|
+
if (first === "-h" || first === "--help") return { kind: "help" };
|
|
2751
|
+
if (first.startsWith("-")) return { kind: "launch", account: null, args: argv };
|
|
2752
|
+
if (isCommand(first)) return { kind: "command", name: first, args: rest };
|
|
2753
|
+
if (isHidden(first)) return { kind: "hidden", name: first, args: rest };
|
|
2754
|
+
if (first === MAIN) {
|
|
2755
|
+
if (!config.main.enabled) throw new XError(`the main identity is disabled; enable it with: xclaude set main --enable`);
|
|
2756
|
+
return { kind: "launch", account: MAIN, args: rest };
|
|
2757
|
+
}
|
|
2758
|
+
if (Object.hasOwn(config.accounts, first)) return { kind: "launch", account: first, args: rest };
|
|
2759
|
+
throw unknownAccount(first, config);
|
|
2760
|
+
}
|
|
2761
|
+
function unknownAccount(word, config) {
|
|
2762
|
+
const names = identities(config);
|
|
2763
|
+
const list = names.length ? `accounts: ${names.join(", ")}` : "no accounts yet; add one with: xclaude add <name>";
|
|
2764
|
+
return new UsageError(`unknown account or command "${word}" (${list})`);
|
|
2765
|
+
}
|
|
2766
|
+
|
|
2719
2767
|
// src/link/normalize.ts
|
|
2720
2768
|
import fs19 from "node:fs";
|
|
2721
2769
|
import path13 from "node:path";
|
|
@@ -2813,7 +2861,7 @@ function strayFolders(paths, config) {
|
|
|
2813
2861
|
if (!st) continue;
|
|
2814
2862
|
if (st.isSymbolicLink()) out.push({ name, dir, kind: "link", entries: [] });
|
|
2815
2863
|
else if (!st.isDirectory() || accounts.some((a) => a.dev === st.dev && a.ino === st.ino)) continue;
|
|
2816
|
-
else if (accountNameProblem(name)) out.push({ name, dir, kind: "stray", entries: [] });
|
|
2864
|
+
else if (accountNameProblem(name, { existing: true })) out.push({ name, dir, kind: "stray", entries: [] });
|
|
2817
2865
|
else {
|
|
2818
2866
|
const entries = fs20.readdirSync(dir).filter((e) => e !== ".DS_Store" && !lstatOrNull(path14.join(dir, e))?.isSymbolicLink());
|
|
2819
2867
|
out.push({ name, dir, kind: entries.length ? "content" : "leftover", entries });
|
|
@@ -2852,6 +2900,18 @@ function keepOnlyLinks(dir, table) {
|
|
|
2852
2900
|
fs20.rmdirSync(dir);
|
|
2853
2901
|
return { kept: false, content };
|
|
2854
2902
|
}
|
|
2903
|
+
function leaveLinks(dir, moved, table) {
|
|
2904
|
+
const links = [...linkNames(table)].filter((name) => lstatOrNull(path14.join(moved, name))?.isSymbolicLink());
|
|
2905
|
+
if (!links.length) return false;
|
|
2906
|
+
try {
|
|
2907
|
+
fs20.mkdirSync(dir, { mode: 448 });
|
|
2908
|
+
} catch (e) {
|
|
2909
|
+
if (errCode(e) === "EEXIST") return false;
|
|
2910
|
+
throw e;
|
|
2911
|
+
}
|
|
2912
|
+
for (const name of links) fs20.symlinkSync(fs20.readlinkSync(path14.join(moved, name)), path14.join(dir, name));
|
|
2913
|
+
return true;
|
|
2914
|
+
}
|
|
2855
2915
|
|
|
2856
2916
|
// src/link/unknown.ts
|
|
2857
2917
|
import fs21 from "node:fs";
|
|
@@ -3198,6 +3258,7 @@ async function doctorCommand(ctx, args) {
|
|
|
3198
3258
|
if (name === MAIN) {
|
|
3199
3259
|
report.add("info", "the main identity: the login in ~/.claude, never linked", 2);
|
|
3200
3260
|
} else {
|
|
3261
|
+
if (isCommand(name)) report.add("warn", `"${name}" is also an xclaude command now, so launch it from the picker, or rename it: xclaude rename ${name} <new-name>`, 2);
|
|
3201
3262
|
const dir = accountDir(ctx.paths, name);
|
|
3202
3263
|
const insp = lstatOrNull(dir)?.isSymbolicLink() ? null : inspectAccount(ctx.paths, name, table, true);
|
|
3203
3264
|
if (!insp) {
|
|
@@ -3281,7 +3342,7 @@ ${s.orgId}`) ?? [], n]);
|
|
|
3281
3342
|
for (const f of configProblem ? [] : strayFolders(ctx.paths, config)) {
|
|
3282
3343
|
const where = t(f.dir);
|
|
3283
3344
|
if (f.kind === "leftover") {
|
|
3284
|
-
report.add("info", `${where}: links kept from the
|
|
3345
|
+
report.add("info", `${where}: links kept from the former account ${f.name}, for its old conversations; delete with: xclaude rm ${f.name}`);
|
|
3285
3346
|
} else if (f.kind === "content") {
|
|
3286
3347
|
report.add("warn", `${where} isn't an account but holds more than links (${f.entries.join(", ")}): \`xclaude add ${f.name}\` takes it back, or check it and delete it by hand`);
|
|
3287
3348
|
} else if (f.kind === "stray") {
|
|
@@ -3465,7 +3526,7 @@ function refreshInitFiles(paths, config, version) {
|
|
|
3465
3526
|
}
|
|
3466
3527
|
|
|
3467
3528
|
// package.json
|
|
3468
|
-
var package_default = { version: "0.
|
|
3529
|
+
var package_default = { version: "0.3.0" };
|
|
3469
3530
|
|
|
3470
3531
|
// src/version.ts
|
|
3471
3532
|
var VERSION = package_default.version;
|
|
@@ -3510,7 +3571,8 @@ Commands:
|
|
|
3510
3571
|
set <name> change an account's defaults
|
|
3511
3572
|
set main --enable|--disable use the login in ~/.claude as the account "main"
|
|
3512
3573
|
rm <name> remove an account and log it out
|
|
3513
|
-
rm --leftovers delete the folders
|
|
3574
|
+
rm --leftovers delete the folders former accounts left behind
|
|
3575
|
+
rename <name> <new-name> rename an account (on macOS, log in again)
|
|
3514
3576
|
tmux new|attach|ls|kill run Claude Code in tmux sessions, by label
|
|
3515
3577
|
shell install|uninstall completion (and the guard) in ~/.zshrc, ~/.bashrc
|
|
3516
3578
|
shell completion bash|zsh print the completion script to load elsewhere
|
|
@@ -3565,8 +3627,28 @@ delete that leftover folder, or --leftovers to delete every one, after a
|
|
|
3565
3627
|
confirmation.
|
|
3566
3628
|
|
|
3567
3629
|
--keep-login don't log the account out
|
|
3568
|
-
--leftovers delete the leftover folders of all removed accounts
|
|
3630
|
+
--leftovers delete the leftover folders of all removed and renamed accounts
|
|
3569
3631
|
-y don't ask for confirmation
|
|
3632
|
+
`,
|
|
3633
|
+
rename: `Usage: xclaude rename <name> <new-name> [-y]
|
|
3634
|
+
|
|
3635
|
+
Renames an account. Its folder becomes ~/.xclaude/accounts/<new-name>, with
|
|
3636
|
+
its settings, defaults and, on Linux, its login; its tmux sessions follow.
|
|
3637
|
+
Conversations and shared content stay where they are, in ~/.claude.
|
|
3638
|
+
|
|
3639
|
+
On macOS the login is tied to the folder's path, so after a confirmation the
|
|
3640
|
+
account is logged out first; log in again with xclaude <new-name> auth login.
|
|
3641
|
+
|
|
3642
|
+
The old name's folder keeps its links into ~/.claude, so conversations from
|
|
3643
|
+
before can still open their saved long outputs; xclaude rm <name> deletes it.
|
|
3644
|
+
The new name can take over such a leftover folder of a removed account.
|
|
3645
|
+
|
|
3646
|
+
Claude Code must not be running in the account: quit its sessions first,
|
|
3647
|
+
background ones too. Use the new folder wherever you set CLAUDE_CONFIG_DIR by
|
|
3648
|
+
hand, such as in an editor's settings.
|
|
3649
|
+
|
|
3650
|
+
-y don't ask before logging out (macOS), and go on when running
|
|
3651
|
+
sessions can't be checked for
|
|
3570
3652
|
`,
|
|
3571
3653
|
ls: `Usage: xclaude ls
|
|
3572
3654
|
|
|
@@ -3846,6 +3928,7 @@ var MODEL_ALIASES = ["default", "best", "fable", "opus", "sonnet", "haiku", "opu
|
|
|
3846
3928
|
var COMMAND_DESCRIPTIONS = {
|
|
3847
3929
|
add: "add an account and log in",
|
|
3848
3930
|
rm: "remove an account",
|
|
3931
|
+
rename: "rename an account",
|
|
3849
3932
|
ls: "list accounts and logins",
|
|
3850
3933
|
set: "change an account's defaults",
|
|
3851
3934
|
tmux: "Claude Code in tmux sessions",
|
|
@@ -3989,8 +4072,10 @@ var Completer = class {
|
|
|
3989
4072
|
{ "--keep-login": null, "--leftovers": null, "-y": null },
|
|
3990
4073
|
1,
|
|
3991
4074
|
() => [...Object.keys(this.config.accounts), ...leftoverNames(this.ctx.paths, this.config)],
|
|
3992
|
-
(n) => this.config.accounts[n] ? this.describeIdentity(n) : "leftover folder of a
|
|
4075
|
+
(n) => this.config.accounts[n] ? this.describeIdentity(n) : "leftover folder of a former account"
|
|
3993
4076
|
);
|
|
4077
|
+
case "rename":
|
|
4078
|
+
return this.own(rest, cur, { "-y": null }, 1, () => Object.keys(this.config.accounts));
|
|
3994
4079
|
case "set":
|
|
3995
4080
|
return this.own(
|
|
3996
4081
|
rest,
|
|
@@ -4264,68 +4349,270 @@ async function lsCommand(ctx, config, args) {
|
|
|
4264
4349
|
return EXIT_OK;
|
|
4265
4350
|
}
|
|
4266
4351
|
|
|
4267
|
-
// src/
|
|
4352
|
+
// src/commands/rename.ts
|
|
4353
|
+
import fs29 from "node:fs";
|
|
4354
|
+
import path23 from "node:path";
|
|
4355
|
+
|
|
4356
|
+
// src/claude/running.ts
|
|
4357
|
+
import { spawnSync as spawnSync5 } from "node:child_process";
|
|
4268
4358
|
import fs27 from "node:fs";
|
|
4359
|
+
import path22 from "node:path";
|
|
4360
|
+
var VAR = "CLAUDE_CONFIG_DIR=";
|
|
4361
|
+
function namesDir(value, dirs) {
|
|
4362
|
+
if (!path22.isAbsolute(value)) return false;
|
|
4363
|
+
const plain = path22.normalize(value).replace(/(.)\/+$/, "$1");
|
|
4364
|
+
if (dirs.includes(plain)) return true;
|
|
4365
|
+
const real = realpathOrNull(value);
|
|
4366
|
+
return real !== null && dirs.some((d) => realpathOrNull(d) === real);
|
|
4367
|
+
}
|
|
4368
|
+
function procEnv(pid) {
|
|
4369
|
+
try {
|
|
4370
|
+
return fs27.readFileSync(`/proc/${pid}/environ`, "utf8").split("\0");
|
|
4371
|
+
} catch {
|
|
4372
|
+
return null;
|
|
4373
|
+
}
|
|
4374
|
+
}
|
|
4375
|
+
function fromProc(dirs, exclude) {
|
|
4376
|
+
let pids;
|
|
4377
|
+
try {
|
|
4378
|
+
pids = fs27.readdirSync("/proc").filter((n) => /^\d+$/.test(n));
|
|
4379
|
+
} catch {
|
|
4380
|
+
return null;
|
|
4381
|
+
}
|
|
4382
|
+
if (procEnv("self") === null) return null;
|
|
4383
|
+
const out = [];
|
|
4384
|
+
for (const pid of pids) {
|
|
4385
|
+
if (exclude.includes(Number(pid))) continue;
|
|
4386
|
+
const value = procEnv(pid)?.find((v) => v.startsWith(VAR));
|
|
4387
|
+
if (value === void 0 || !namesDir(value.slice(VAR.length), dirs)) continue;
|
|
4388
|
+
let name = "?";
|
|
4389
|
+
try {
|
|
4390
|
+
name = fs27.readFileSync(`/proc/${pid}/comm`, "utf8").trim() || name;
|
|
4391
|
+
} catch {
|
|
4392
|
+
}
|
|
4393
|
+
out.push({ pid: Number(pid), name });
|
|
4394
|
+
}
|
|
4395
|
+
return out;
|
|
4396
|
+
}
|
|
4397
|
+
function parsePs(text, dirs, exclude, self) {
|
|
4398
|
+
const out = [];
|
|
4399
|
+
let sawSelf = false;
|
|
4400
|
+
for (const line of text.split("\n")) {
|
|
4401
|
+
const m = /^\s*(\d+)\s+(.*)$/.exec(line);
|
|
4402
|
+
if (!m) continue;
|
|
4403
|
+
const pid = Number(m[1]);
|
|
4404
|
+
const rest = m[2];
|
|
4405
|
+
if (pid === self) sawSelf = / (?:PATH|HOME|USER)=/.test(rest);
|
|
4406
|
+
if (exclude.includes(pid) || pid === self) continue;
|
|
4407
|
+
for (const v of rest.matchAll(/ CLAUDE_CONFIG_DIR=(.*?)(?= [A-Za-z_][A-Za-z0-9_]*=|$)/g)) {
|
|
4408
|
+
if (namesDir(v[1], dirs)) {
|
|
4409
|
+
out.push({ pid, name: path22.basename(rest.split(" ")[0]) || "?" });
|
|
4410
|
+
break;
|
|
4411
|
+
}
|
|
4412
|
+
}
|
|
4413
|
+
}
|
|
4414
|
+
return sawSelf ? out : null;
|
|
4415
|
+
}
|
|
4416
|
+
function fromPs(dirs, exclude) {
|
|
4417
|
+
const res = spawnSync5("ps", ["-axww", "-E", "-o", "pid=,command="], { encoding: "utf8", timeout: 1e4, maxBuffer: 64 * 1024 * 1024 });
|
|
4418
|
+
if (res.error || res.status !== 0) return null;
|
|
4419
|
+
return parsePs(res.stdout, dirs, [...exclude, res.pid], process.pid);
|
|
4420
|
+
}
|
|
4421
|
+
function processesUsing(dirs, platform = process.platform) {
|
|
4422
|
+
const exclude = [process.pid];
|
|
4423
|
+
if (platform === "linux") return fromProc(dirs, exclude);
|
|
4424
|
+
if (platform === "darwin") return fromPs(dirs, exclude);
|
|
4425
|
+
return null;
|
|
4426
|
+
}
|
|
4427
|
+
function describeProcs(procs) {
|
|
4428
|
+
const shown = procs.slice(0, 5).map((p) => `${p.pid} (${p.name})`);
|
|
4429
|
+
if (procs.length > 5) shown.push(`and ${procs.length - 5} more`);
|
|
4430
|
+
return shown.join(", ");
|
|
4431
|
+
}
|
|
4432
|
+
|
|
4433
|
+
// src/core/prompt.ts
|
|
4434
|
+
import fs28 from "node:fs";
|
|
4269
4435
|
function confirm(question) {
|
|
4270
|
-
|
|
4436
|
+
fs28.writeSync(2, `${question} [y/N] `);
|
|
4271
4437
|
let fd;
|
|
4272
4438
|
try {
|
|
4273
|
-
fd =
|
|
4439
|
+
fd = fs28.openSync("/dev/tty", "r");
|
|
4274
4440
|
} catch {
|
|
4275
4441
|
return false;
|
|
4276
4442
|
}
|
|
4277
4443
|
try {
|
|
4278
4444
|
const buf = Buffer.alloc(256);
|
|
4279
|
-
const n =
|
|
4445
|
+
const n = fs28.readSync(fd, buf, 0, buf.length, null);
|
|
4280
4446
|
const answer = buf.subarray(0, n).toString("utf8").trim().toLowerCase();
|
|
4281
4447
|
return answer === "y" || answer === "yes";
|
|
4282
4448
|
} catch {
|
|
4283
4449
|
return false;
|
|
4284
4450
|
} finally {
|
|
4285
|
-
|
|
4451
|
+
fs28.closeSync(fd);
|
|
4286
4452
|
}
|
|
4287
4453
|
}
|
|
4288
4454
|
|
|
4289
|
-
// src/
|
|
4290
|
-
var
|
|
4291
|
-
function
|
|
4292
|
-
|
|
4293
|
-
}
|
|
4294
|
-
|
|
4295
|
-
|
|
4296
|
-
|
|
4297
|
-
|
|
4298
|
-
|
|
4299
|
-
if (
|
|
4300
|
-
|
|
4301
|
-
if (
|
|
4302
|
-
if (
|
|
4303
|
-
|
|
4304
|
-
|
|
4305
|
-
|
|
4306
|
-
|
|
4307
|
-
|
|
4455
|
+
// src/commands/rename.ts
|
|
4456
|
+
var USAGE2 = "usage: xclaude rename <name> <new-name> [-y]";
|
|
4457
|
+
async function renameCommand(ctx, config, args, deps = {}) {
|
|
4458
|
+
const { platform = process.platform, processes = (dirs) => processesUsing(dirs), moveSessions = () => [] } = deps;
|
|
4459
|
+
const p = parseOptions(args, { yes: { short: "y" } }, "rename");
|
|
4460
|
+
const [from, to, ...extra] = p.positionals;
|
|
4461
|
+
if (!from || !to || extra.length) throw new UsageError(USAGE2);
|
|
4462
|
+
const yes = Boolean(p.values.yes);
|
|
4463
|
+
if (from === MAIN) throw new UsageError("the main identity is the login in ~/.claude, and its name is fixed");
|
|
4464
|
+
if (!Object.hasOwn(config.accounts, from)) throw unknownAccount(from, config);
|
|
4465
|
+
if (to === from) throw new UsageError(`${from} is already called that`);
|
|
4466
|
+
const problem2 = accountNameProblem(to);
|
|
4467
|
+
if (problem2) throw new UsageError(problem2);
|
|
4468
|
+
if (Object.hasOwn(config.accounts, to)) throw new XError(`account "${to}" already exists; pick another name`);
|
|
4469
|
+
const log = (line) => ctx.io.err(`${line}
|
|
4470
|
+
`);
|
|
4471
|
+
const t = (where) => tildify(where, ctx.paths.home);
|
|
4472
|
+
const dir = accountDir(ctx.paths, from);
|
|
4473
|
+
const target = accountDir(ctx.paths, to);
|
|
4474
|
+
const st = lstatOrNull(dir);
|
|
4475
|
+
if (st) {
|
|
4476
|
+
const refusal = unmanageable(dir, ctx.paths.store) ?? (st.isDirectory() ? null : "isn't a folder");
|
|
4477
|
+
if (refusal) throw new XError(`not renaming ${from}: ${t(dir)} ${refusal}`);
|
|
4478
|
+
}
|
|
4479
|
+
const replace = lstatOrNull(target) !== null;
|
|
4480
|
+
if (replace) {
|
|
4481
|
+
const f = strayFolders(ctx.paths, config).find((x) => x.name === to);
|
|
4482
|
+
if (f?.kind !== "leftover") {
|
|
4483
|
+
const what = f?.kind === "content" ? ` and holds more than links (${f.entries.join(", ")})` : "";
|
|
4484
|
+
throw new XError(`not renaming ${from}: ${t(target)} already exists${what}; move it away, or pick another name`);
|
|
4485
|
+
}
|
|
4308
4486
|
}
|
|
4309
|
-
|
|
4310
|
-
|
|
4487
|
+
const mine = processes([dir]);
|
|
4488
|
+
const theirs = replace ? processes([target]) : [];
|
|
4489
|
+
if (mine === null || theirs === null) {
|
|
4490
|
+
if (!yes) {
|
|
4491
|
+
throw new XError(`not renaming ${from}: couldn't check whether Claude Code still runs in it on this machine; quit its sessions, background ones too, then run this again with -y`);
|
|
4492
|
+
}
|
|
4493
|
+
log(`xclaude: couldn't check whether Claude Code still runs in ${from}; going on (-y)`);
|
|
4494
|
+
}
|
|
4495
|
+
const here = (dirs) => ctx.env.CLAUDE_CONFIG_DIR && namesDir(ctx.env.CLAUDE_CONFIG_DIR, dirs) ? " (this terminal is one of them)" : "";
|
|
4496
|
+
if (mine?.length) {
|
|
4497
|
+
throw new XError(
|
|
4498
|
+
`not renaming ${from}: these still run as ${from}: ${describeProcs(mine)}${here([dir])}. Quit its Claude Code sessions, background ones too, and what they started, then run this again.`
|
|
4499
|
+
);
|
|
4500
|
+
}
|
|
4501
|
+
if (theirs?.length) {
|
|
4502
|
+
throw new XError(`not renaming ${from}: these still run in ${t(target)}, the folder ${to} would take over: ${describeProcs(theirs)}${here([target])}. Quit them first.`);
|
|
4503
|
+
}
|
|
4504
|
+
let claude = null;
|
|
4505
|
+
let logOut = false;
|
|
4506
|
+
if (platform === "darwin" && st) {
|
|
4507
|
+
try {
|
|
4508
|
+
claude = findClaude(ctx.env, config, ctx.paths.home, ctx.selfPath);
|
|
4509
|
+
} catch (e) {
|
|
4510
|
+
if (!(e instanceof XError)) throw e;
|
|
4511
|
+
}
|
|
4512
|
+
const env = identityEnv(ctx, from);
|
|
4513
|
+
for (const name of CREDENTIAL_VARS) delete env[name];
|
|
4514
|
+
const status = claude ? await authStatus(claude, env) : null;
|
|
4515
|
+
logOut = status === null || status.loggedIn || status.error !== null;
|
|
4516
|
+
if (logOut && !yes) {
|
|
4517
|
+
if (!ctx.tty.stdin) throw new UsageError(`renaming ${from} on macOS logs it out: run it in a terminal, or pass -y`);
|
|
4518
|
+
const ok = confirm(`Rename ${from} to ${to}? On macOS the login is tied to the folder's path, so ${from} is logged out first, and ${to} logs in again.`);
|
|
4519
|
+
if (!ok) {
|
|
4520
|
+
log("xclaude: nothing renamed");
|
|
4521
|
+
throw new Cancelled();
|
|
4522
|
+
}
|
|
4523
|
+
}
|
|
4524
|
+
}
|
|
4525
|
+
const table = shareTable(config, ctx.switches);
|
|
4526
|
+
if (ctx.switches.normalizePaths && table.dirs.includes("plugins")) {
|
|
4527
|
+
normalizePaths(ctx.paths, { account: from, lockWaitMs: Infinity, log });
|
|
4528
|
+
}
|
|
4529
|
+
if (logOut) {
|
|
4530
|
+
const res = claude ? await runClaude(claude, ["auth", "logout"], identityEnv(ctx, from)) : null;
|
|
4531
|
+
if (res?.code !== 0) {
|
|
4532
|
+
const why = res ? (res.stderr || res.stdout).trim().split("\n")[0] : "claude wasn't found";
|
|
4533
|
+
log(`xclaude: logging ${from} out failed (${why}); its login may remain in the Keychain, under the old folder's path`);
|
|
4534
|
+
}
|
|
4535
|
+
}
|
|
4536
|
+
if (st) {
|
|
4537
|
+
try {
|
|
4538
|
+
if (replace) removeTree(target);
|
|
4539
|
+
fs29.renameSync(dir, target);
|
|
4540
|
+
} catch (e) {
|
|
4541
|
+
throw new XError(`not renaming ${from}: moving ${t(dir)} to ${t(target)} failed: ${e.message}`);
|
|
4542
|
+
}
|
|
4543
|
+
}
|
|
4544
|
+
try {
|
|
4545
|
+
const fresh = loadConfig(ctx.paths, { create: false }).config;
|
|
4546
|
+
if (!Object.hasOwn(fresh.accounts, from) || Object.hasOwn(fresh.accounts, to)) {
|
|
4547
|
+
throw new XError(`not renaming ${from}: the config changed meanwhile; run this again`);
|
|
4548
|
+
}
|
|
4549
|
+
renameAccount(fresh, from, to);
|
|
4550
|
+
saveConfig(ctx.paths, fresh);
|
|
4551
|
+
} catch (e) {
|
|
4552
|
+
if (st) {
|
|
4553
|
+
try {
|
|
4554
|
+
fs29.renameSync(target, dir);
|
|
4555
|
+
} catch {
|
|
4556
|
+
log(`xclaude: couldn't move ${t(target)} back to ${t(dir)}; move it by hand`);
|
|
4557
|
+
}
|
|
4558
|
+
}
|
|
4559
|
+
throw e;
|
|
4560
|
+
}
|
|
4561
|
+
const step2 = (what, run) => {
|
|
4562
|
+
try {
|
|
4563
|
+
run();
|
|
4564
|
+
} catch (e) {
|
|
4565
|
+
log(`xclaude: ${what} failed: ${e.message}`);
|
|
4566
|
+
}
|
|
4567
|
+
};
|
|
4568
|
+
let kept = false;
|
|
4569
|
+
if (st) {
|
|
4570
|
+
step2(`keeping links at ${t(dir)} for conversations from before`, () => {
|
|
4571
|
+
kept = leaveLinks(dir, target, table);
|
|
4572
|
+
});
|
|
4573
|
+
step2(`removing ${t(dir)}.lock`, () => removeTree(`${dir}.lock`));
|
|
4574
|
+
}
|
|
4575
|
+
step2(`moving what ${from} took from ~/.claude/settings.json`, () => {
|
|
4576
|
+
forgetInherit(ctx.paths, to);
|
|
4577
|
+
const base = basePath(ctx.paths, from);
|
|
4578
|
+
const baseStat = lstatOrNull(base);
|
|
4579
|
+
if (baseStat) moveNoClobber(base, basePath(ctx.paths, to), baseStat);
|
|
4580
|
+
});
|
|
4581
|
+
step2("removing the old --mcp-config file", () => {
|
|
4582
|
+
forgetMcp(ctx.paths, from);
|
|
4583
|
+
forgetMcp(ctx.paths, to);
|
|
4584
|
+
});
|
|
4585
|
+
step2("updating state.json", () => updateState(ctx.paths, (s) => renameInState(s, from, to)));
|
|
4586
|
+
let sessions = [];
|
|
4587
|
+
step2("moving its tmux sessions", () => {
|
|
4588
|
+
sessions = moveSessions(from, to);
|
|
4589
|
+
});
|
|
4590
|
+
log(`xclaude: renamed ${from} to ${to}; its config dir is now ${t(target)}, so use that wherever you set CLAUDE_CONFIG_DIR by hand (an editor's settings, say)`);
|
|
4591
|
+
if (kept) {
|
|
4592
|
+
log(`xclaude: ${t(dir)} keeps links into ~/.claude, so conversations from before can still open their saved long outputs; delete it with: xclaude rm ${from}`);
|
|
4593
|
+
}
|
|
4594
|
+
if (sessions.length) log(`xclaude: tmux sessions now on ${to}: ${sessions.join(", ")} (a command already typed in them still says ${from})`);
|
|
4595
|
+
const settings = readFileOrNull(path23.join(target, "settings.json"));
|
|
4596
|
+
if (settings && mentions(settings, [dir, t(dir)])) log(`xclaude: ${to}'s settings.json still names ${t(dir)}; change that to ${t(target)}`);
|
|
4597
|
+
if (logOut) log(`xclaude: log ${to} in again with \`xclaude ${to} auth login\`, or /login inside \`xclaude ${to}\``);
|
|
4598
|
+
return EXIT_OK;
|
|
4311
4599
|
}
|
|
4312
|
-
function
|
|
4313
|
-
const
|
|
4314
|
-
|
|
4315
|
-
return new UsageError(`unknown account or command "${word}" (${list})`);
|
|
4600
|
+
function mentions(text, paths) {
|
|
4601
|
+
const escaped = paths.map((p) => p.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
|
|
4602
|
+
return new RegExp(`(?:${escaped.join("|")})(?![A-Za-z0-9_-])`).test(text);
|
|
4316
4603
|
}
|
|
4317
4604
|
|
|
4318
4605
|
// src/commands/rm.ts
|
|
4319
|
-
var
|
|
4606
|
+
var USAGE3 = "usage: xclaude rm <name> [--keep-login] [-y], or xclaude rm --leftovers [-y]";
|
|
4320
4607
|
async function rmCommand(ctx, config, args, listSessions2 = () => []) {
|
|
4321
4608
|
const p = parseOptions(args, { "keep-login": {}, leftovers: {}, yes: { short: "y" } }, "rm");
|
|
4322
4609
|
const [name, ...extra] = p.positionals;
|
|
4323
4610
|
const yes = Boolean(p.values.yes);
|
|
4324
4611
|
if (p.values.leftovers) {
|
|
4325
|
-
if (name) throw new UsageError(
|
|
4612
|
+
if (name) throw new UsageError(USAGE3);
|
|
4326
4613
|
return removeLeftovers(ctx, config, null, yes, listSessions2);
|
|
4327
4614
|
}
|
|
4328
|
-
if (!name || extra.length) throw new UsageError(
|
|
4615
|
+
if (!name || extra.length) throw new UsageError(USAGE3);
|
|
4329
4616
|
if (name === MAIN) throw new UsageError("the main identity isn't removable; turn it off with: xclaude set main --disable");
|
|
4330
4617
|
if (!Object.hasOwn(config.accounts, name)) {
|
|
4331
4618
|
if (strayFolders(ctx.paths, config).some((f) => f.name === name)) return removeLeftovers(ctx, config, name, yes, listSessions2);
|
|
@@ -4479,13 +4766,13 @@ async function logout(ctx, config, claude, name, keep, log) {
|
|
|
4479
4766
|
}
|
|
4480
4767
|
|
|
4481
4768
|
// src/commands/set.ts
|
|
4482
|
-
var
|
|
4769
|
+
var USAGE4 = 'usage: xclaude set <name> [--model M] [--effort E] [--args "\u2026"] [--unset model|effort|args] [--inherit on|off|reset], or xclaude set main --enable|--disable';
|
|
4483
4770
|
var KEYS = ["model", "effort", "args"];
|
|
4484
4771
|
var INHERIT = ["on", "off", "reset"];
|
|
4485
4772
|
function setCommand(ctx, config, args) {
|
|
4486
4773
|
const p = parseOptions(args, { ...DEFAULT_OPTIONS, unset: { value: true, repeat: true }, inherit: { value: true }, enable: {}, disable: {} }, "set");
|
|
4487
4774
|
const [name, ...extra] = p.positionals;
|
|
4488
|
-
if (!name || extra.length) throw new UsageError(
|
|
4775
|
+
if (!name || extra.length) throw new UsageError(USAGE4);
|
|
4489
4776
|
const isMain = name === MAIN;
|
|
4490
4777
|
if (!isMain && !Object.hasOwn(config.accounts, name)) throw unknownAccount(name, config);
|
|
4491
4778
|
const target = isMain ? config.main : config.accounts[name];
|
|
@@ -4605,8 +4892,8 @@ function shellCommand(ctx, config, args) {
|
|
|
4605
4892
|
}
|
|
4606
4893
|
|
|
4607
4894
|
// src/commands/tmux.ts
|
|
4608
|
-
import
|
|
4609
|
-
import
|
|
4895
|
+
import fs31 from "node:fs";
|
|
4896
|
+
import path24 from "node:path";
|
|
4610
4897
|
|
|
4611
4898
|
// src/launch/args.ts
|
|
4612
4899
|
function hasFlag(args, flag) {
|
|
@@ -4764,8 +5051,8 @@ async function launch(ctx, config, account, userArgs) {
|
|
|
4764
5051
|
}
|
|
4765
5052
|
|
|
4766
5053
|
// src/tmux/claude-state.ts
|
|
4767
|
-
import { spawnSync as
|
|
4768
|
-
import
|
|
5054
|
+
import { spawnSync as spawnSync6 } from "node:child_process";
|
|
5055
|
+
import fs30 from "node:fs";
|
|
4769
5056
|
function describeAgent(e) {
|
|
4770
5057
|
if (e.status === "waiting") return { text: e.waitingFor ? `needs input (${e.waitingFor})` : "needs input", rank: 3 };
|
|
4771
5058
|
if (e.status === "busy") return { text: "working", rank: 2 };
|
|
@@ -4773,10 +5060,10 @@ function describeAgent(e) {
|
|
|
4773
5060
|
return null;
|
|
4774
5061
|
}
|
|
4775
5062
|
function parentLookup() {
|
|
4776
|
-
if (process.platform === "linux" &&
|
|
5063
|
+
if (process.platform === "linux" && fs30.existsSync("/proc/self/stat")) {
|
|
4777
5064
|
return (pid) => {
|
|
4778
5065
|
try {
|
|
4779
|
-
const stat =
|
|
5066
|
+
const stat = fs30.readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
4780
5067
|
const ppid = Number(stat.slice(stat.lastIndexOf(")") + 2).split(" ")[1]);
|
|
4781
5068
|
return Number.isFinite(ppid) ? ppid : null;
|
|
4782
5069
|
} catch {
|
|
@@ -4785,7 +5072,7 @@ function parentLookup() {
|
|
|
4785
5072
|
};
|
|
4786
5073
|
}
|
|
4787
5074
|
const map = /* @__PURE__ */ new Map();
|
|
4788
|
-
const res =
|
|
5075
|
+
const res = spawnSync6("ps", ["-axo", "pid=,ppid="], { encoding: "utf8", timeout: 1e4 });
|
|
4789
5076
|
for (const line of (res.stdout ?? "").split("\n")) {
|
|
4790
5077
|
const [pid, ppid] = line.trim().split(/\s+/).map(Number);
|
|
4791
5078
|
if (pid && ppid !== void 0 && Number.isFinite(ppid)) map.set(pid, ppid);
|
|
@@ -4902,8 +5189,8 @@ async function tmuxNew(ctx, config, args) {
|
|
|
4902
5189
|
account ??= await chooseAccount(ctx, config, `Run Claude Code in tmux session "${req.label}" as`);
|
|
4903
5190
|
if (!defaultsOf(config, account)) throw unknownAccount(account, config);
|
|
4904
5191
|
}
|
|
4905
|
-
const dir =
|
|
4906
|
-
if (!
|
|
5192
|
+
const dir = path24.resolve(ctx.cwd, req.dir ?? ".");
|
|
5193
|
+
if (!fs31.statSync(dir, { throwIfNoEntry: false })?.isDirectory()) throw new XError(`--dir ${req.dir}: not a directory`);
|
|
4907
5194
|
const existing = findByLabel(listSessions(tmux), req.label);
|
|
4908
5195
|
if (existing) {
|
|
4909
5196
|
throw new XError(`"${req.label}" is running (${describeSession(existing, ctx.paths.home)}): xclaude tmux attach ${req.label}, or kill it first`);
|
|
@@ -5012,6 +5299,30 @@ function accountSessionLabels(ctx, account) {
|
|
|
5012
5299
|
return [];
|
|
5013
5300
|
}
|
|
5014
5301
|
}
|
|
5302
|
+
function renameAccountSessions(ctx, from, to) {
|
|
5303
|
+
let tmux;
|
|
5304
|
+
let sessions;
|
|
5305
|
+
try {
|
|
5306
|
+
tmux = new Tmux(ctx.env);
|
|
5307
|
+
sessions = listSessions(tmux).filter((s) => s.account === from);
|
|
5308
|
+
} catch {
|
|
5309
|
+
return [];
|
|
5310
|
+
}
|
|
5311
|
+
const prefix = `[${from}] `;
|
|
5312
|
+
for (const s of sessions) {
|
|
5313
|
+
try {
|
|
5314
|
+
tmux.run(["set-option", "-t", s.id, "@xclaude_account", to]);
|
|
5315
|
+
tmux.run(["rename-session", "-t", s.id, sessionName(to, s.label)]);
|
|
5316
|
+
const right = tmux.run(["show-options", "-t", s.id, "-v", "status-right"]);
|
|
5317
|
+
if (right.code !== 0 || !right.stdout.startsWith(prefix)) continue;
|
|
5318
|
+
tmux.run(["set-option", "-t", s.id, "status-right", `[${to}] ${right.stdout.replace(/\n$/, "").slice(prefix.length)}`]);
|
|
5319
|
+
const length = Number(tmux.run(["show-options", "-t", s.id, "-v", "status-right-length"]).stdout.trim());
|
|
5320
|
+
if (length) tmux.run(["set-option", "-t", s.id, "status-right-length", String(length + to.length - from.length)]);
|
|
5321
|
+
} catch {
|
|
5322
|
+
}
|
|
5323
|
+
}
|
|
5324
|
+
return sessions.map((s) => s.label);
|
|
5325
|
+
}
|
|
5015
5326
|
|
|
5016
5327
|
// src/main.ts
|
|
5017
5328
|
async function main(argv, ctx) {
|
|
@@ -5072,6 +5383,8 @@ async function main(argv, ctx) {
|
|
|
5072
5383
|
return lsCommand(ctx, config, action.args);
|
|
5073
5384
|
case "rm":
|
|
5074
5385
|
return rmCommand(ctx, config, action.args, (account) => accountSessionLabels(ctx, account));
|
|
5386
|
+
case "rename":
|
|
5387
|
+
return renameCommand(ctx, config, action.args, { moveSessions: (from, to) => renameAccountSessions(ctx, from, to) });
|
|
5075
5388
|
case "tmux":
|
|
5076
5389
|
return tmuxCommand(ctx, config, action.args);
|
|
5077
5390
|
case "shell":
|