claudeup 4.17.0 → 4.18.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.
Files changed (76) hide show
  1. package/package.json +1 -1
  2. package/src/__tests__/alias-parser.test.ts +317 -0
  3. package/src/__tests__/alias-shell-writer.test.ts +661 -0
  4. package/src/__tests__/alias-store.test.ts +86 -0
  5. package/src/__tests__/gitignore-fixer.test.ts +64 -1
  6. package/src/__tests__/gitignore-prerun.test.ts +2 -2
  7. package/src/__tests__/gitignore-service.test.ts +42 -0
  8. package/src/__tests__/marketplaces.test.ts +40 -0
  9. package/src/__tests__/plugin-manager-fallback.test.ts +120 -0
  10. package/src/__tests__/useGitignoreModal.test.ts +2 -2
  11. package/src/data/alias-flags.js +196 -0
  12. package/src/data/alias-flags.ts +291 -0
  13. package/src/data/gitignore-reasons.js +97 -0
  14. package/src/data/gitignore-reasons.ts +103 -0
  15. package/src/data/marketplaces.js +5 -3
  16. package/src/data/marketplaces.ts +5 -4
  17. package/src/services/alias-settings.js +51 -0
  18. package/src/services/alias-settings.ts +63 -0
  19. package/src/services/alias-shell-writer.js +764 -0
  20. package/src/services/alias-shell-writer.ts +873 -0
  21. package/src/services/alias-store.js +77 -0
  22. package/src/services/alias-store.ts +112 -0
  23. package/src/services/gitignore-fixer.js +70 -10
  24. package/src/services/gitignore-fixer.ts +76 -9
  25. package/src/services/gitignore-prerun.js +3 -3
  26. package/src/services/gitignore-prerun.ts +3 -3
  27. package/src/services/gitignore-service.js +20 -2
  28. package/src/services/gitignore-service.ts +23 -1
  29. package/src/services/marketplace-fetcher.js +96 -0
  30. package/src/services/marketplace-fetcher.ts +137 -0
  31. package/src/services/plugin-manager.js +6 -59
  32. package/src/services/plugin-manager.ts +16 -91
  33. package/src/services/skillsmp-client.js +29 -9
  34. package/src/services/skillsmp-client.ts +38 -8
  35. package/src/types/gitignore.ts +1 -1
  36. package/src/ui/App.js +10 -4
  37. package/src/ui/App.tsx +9 -3
  38. package/src/ui/components/TabBar.js +2 -1
  39. package/src/ui/components/TabBar.tsx +2 -1
  40. package/src/ui/components/layout/FooterHints.js +29 -0
  41. package/src/ui/components/layout/FooterHints.tsx +52 -0
  42. package/src/ui/components/layout/ScreenLayout.js +2 -1
  43. package/src/ui/components/layout/ScreenLayout.tsx +12 -3
  44. package/src/ui/components/layout/index.js +1 -0
  45. package/src/ui/components/layout/index.ts +5 -0
  46. package/src/ui/components/modals/SelectModal.js +8 -1
  47. package/src/ui/components/modals/SelectModal.tsx +12 -1
  48. package/src/ui/hooks/useGitignoreModal.js +7 -8
  49. package/src/ui/hooks/useGitignoreModal.ts +8 -9
  50. package/src/ui/renderers/gitignoreRenderers.js +36 -23
  51. package/src/ui/renderers/gitignoreRenderers.tsx +50 -41
  52. package/src/ui/screens/AliasScreen.js +1008 -0
  53. package/src/ui/screens/AliasScreen.tsx +1402 -0
  54. package/src/ui/screens/CliToolsScreen.js +6 -1
  55. package/src/ui/screens/CliToolsScreen.tsx +6 -1
  56. package/src/ui/screens/EnvVarsScreen.js +6 -1
  57. package/src/ui/screens/EnvVarsScreen.tsx +6 -1
  58. package/src/ui/screens/GitignoreScreen.js +189 -88
  59. package/src/ui/screens/GitignoreScreen.tsx +312 -132
  60. package/src/ui/screens/McpRegistryScreen.js +13 -2
  61. package/src/ui/screens/McpRegistryScreen.tsx +13 -2
  62. package/src/ui/screens/McpScreen.js +6 -1
  63. package/src/ui/screens/McpScreen.tsx +6 -1
  64. package/src/ui/screens/ModelSelectorScreen.js +8 -2
  65. package/src/ui/screens/ModelSelectorScreen.tsx +8 -2
  66. package/src/ui/screens/PluginsScreen.js +13 -2
  67. package/src/ui/screens/PluginsScreen.tsx +13 -2
  68. package/src/ui/screens/ProfilesScreen.js +8 -1
  69. package/src/ui/screens/ProfilesScreen.tsx +8 -1
  70. package/src/ui/screens/SkillsScreen.js +21 -4
  71. package/src/ui/screens/SkillsScreen.tsx +39 -5
  72. package/src/ui/screens/StatusLineScreen.js +7 -1
  73. package/src/ui/screens/StatusLineScreen.tsx +7 -1
  74. package/src/ui/screens/index.js +1 -0
  75. package/src/ui/screens/index.ts +1 -0
  76. package/src/ui/state/types.ts +4 -2
@@ -0,0 +1,764 @@
1
+ /**
2
+ * Render the managed `claude` alias and splice it into shell rc files.
3
+ *
4
+ * Two layers:
5
+ * - Pure render functions (POSIX vs fish) that take a config and emit text.
6
+ * - I/O helpers that detect installed shells and replace the managed block
7
+ * idempotently using sentinel comment markers.
8
+ */
9
+ import { readFile, writeFile, stat } from "node:fs/promises";
10
+ import { existsSync } from "node:fs";
11
+ import { join } from "node:path";
12
+ import { homedir } from "node:os";
13
+ import { ALIAS_FLAGS, getFlagById, } from "../data/alias-flags.js";
14
+ const BLOCK_BEGIN = "# >>> claudeup managed alias >>>";
15
+ const BLOCK_END = "# <<< claudeup managed alias <<<";
16
+ /**
17
+ * Probe the user's home dir for the three supported shell rc files.
18
+ *
19
+ * Path discovery is intentionally simple: we don't follow `$ZDOTDIR` or
20
+ * fish's `$XDG_CONFIG_HOME` overrides yet. If users hit those, we expose
21
+ * a path override later — for now the conventional locations cover the
22
+ * default install on macOS / Linux.
23
+ */
24
+ export async function detectShells(home = homedir(), shellEnv = process.env.SHELL ?? "") {
25
+ const candidates = [
26
+ { kind: "zsh", path: join(home, ".zshrc") },
27
+ { kind: "bash", path: join(home, ".bashrc") },
28
+ { kind: "fish", path: join(home, ".config", "fish", "config.fish") },
29
+ ];
30
+ const defaultKind = inferDefaultShell(shellEnv);
31
+ const results = [];
32
+ for (const c of candidates) {
33
+ let exists = existsSync(c.path);
34
+ if (exists) {
35
+ // existsSync can lie on broken symlinks — check stat too
36
+ try {
37
+ await stat(c.path);
38
+ }
39
+ catch {
40
+ exists = false;
41
+ }
42
+ }
43
+ results.push({
44
+ kind: c.kind,
45
+ path: c.path,
46
+ exists,
47
+ isDefault: c.kind === defaultKind,
48
+ });
49
+ }
50
+ return results;
51
+ }
52
+ function inferDefaultShell(shellEnv) {
53
+ if (shellEnv.endsWith("/zsh"))
54
+ return "zsh";
55
+ if (shellEnv.endsWith("/bash"))
56
+ return "bash";
57
+ if (shellEnv.endsWith("/fish"))
58
+ return "fish";
59
+ return null;
60
+ }
61
+ /**
62
+ * Build the alias body as a list of segments. Pure function.
63
+ *
64
+ * Validation:
65
+ * - `xorGroup` collisions are resolved by emitting only the first enabled
66
+ * flag in the group. The UI is responsible for warning the user; the
67
+ * renderer never silently keeps both.
68
+ * - `requires` is enforced: a flag whose dependency is disabled is dropped.
69
+ */
70
+ export function renderArgs(config) {
71
+ const out = [];
72
+ const seenXor = new Set();
73
+ for (const flag of ALIAS_FLAGS) {
74
+ if (flag.requires) {
75
+ const dep = config.flags[flag.requires];
76
+ if (!isFlagEnabled(dep))
77
+ continue;
78
+ }
79
+ if (flag.xorGroup) {
80
+ if (seenXor.has(flag.xorGroup))
81
+ continue;
82
+ }
83
+ const value = config.flags[flag.id];
84
+ if (!value)
85
+ continue;
86
+ const rendered = renderFlag(flag, value);
87
+ if (rendered.length === 0)
88
+ continue;
89
+ out.push(...rendered);
90
+ if (flag.xorGroup)
91
+ seenXor.add(flag.xorGroup);
92
+ }
93
+ return out;
94
+ }
95
+ /**
96
+ * Convenience: render each segment to its literal-token approximation.
97
+ * Composite segments are stringified by joining their literal parts with
98
+ * `<sub>` placeholders for raw parts. Used by tests and validation summaries
99
+ * where the actual shell substitution doesn't matter.
100
+ */
101
+ export function renderArgsAsTokens(config) {
102
+ return renderArgs(config).map((seg) => {
103
+ if (seg.kind === "literal")
104
+ return seg.text;
105
+ return seg.parts
106
+ .map((p) => (p.kind === "literal" ? p.text : `<${p.posix}>`))
107
+ .join("");
108
+ });
109
+ }
110
+ function isFlagEnabled(value) {
111
+ if (!value)
112
+ return false;
113
+ switch (value.kind) {
114
+ case "boolean":
115
+ return value.enabled;
116
+ case "tri-state":
117
+ return value.state !== "unset";
118
+ case "select":
119
+ case "text":
120
+ case "optional-text":
121
+ case "text-list":
122
+ case "multi-with-custom":
123
+ return value.enabled;
124
+ }
125
+ }
126
+ function lit(text) {
127
+ return { kind: "literal", text };
128
+ }
129
+ /**
130
+ * Wrap a templated value as a composite segment if it contains any tokens,
131
+ * otherwise return it as a plain literal.
132
+ */
133
+ function templatedSegment(value) {
134
+ const parts = expandTemplateValue(value);
135
+ if (parts.length === 1 && parts[0].kind === "literal") {
136
+ return { kind: "literal", text: parts[0].text };
137
+ }
138
+ return { kind: "composite", parts };
139
+ }
140
+ function renderFlag(flag, value) {
141
+ switch (value.kind) {
142
+ case "boolean":
143
+ return value.enabled ? [lit(flag.flag)] : [];
144
+ case "tri-state":
145
+ if (value.state === "on")
146
+ return [lit(flag.flag)];
147
+ if (value.state === "off" && flag.triStateOff)
148
+ return [lit(flag.triStateOff)];
149
+ return [];
150
+ case "select":
151
+ if (!value.enabled)
152
+ return [];
153
+ // Empty value means "bare flag, default variant" (e.g. `--tmux` alone).
154
+ return value.value === ""
155
+ ? [lit(flag.flag)]
156
+ : [lit(flag.flag), lit(value.value)];
157
+ case "text":
158
+ if (!value.enabled || !value.value)
159
+ return [];
160
+ return [
161
+ lit(flag.flag),
162
+ flag.templated ? templatedSegment(value.value) : lit(value.value),
163
+ ];
164
+ case "optional-text":
165
+ if (!value.enabled)
166
+ return [];
167
+ if (!value.value)
168
+ return [lit(flag.flag)];
169
+ return [
170
+ lit(flag.flag),
171
+ flag.templated ? templatedSegment(value.value) : lit(value.value),
172
+ ];
173
+ case "text-list": {
174
+ if (!value.enabled || value.values.length === 0)
175
+ return [];
176
+ const out = [];
177
+ for (const v of value.values) {
178
+ if (!v)
179
+ continue;
180
+ out.push(lit(flag.flag), flag.templated ? templatedSegment(v) : lit(v));
181
+ }
182
+ return out;
183
+ }
184
+ case "multi-with-custom": {
185
+ if (!value.enabled)
186
+ return [];
187
+ const tokens = dedupe([...value.picked, ...value.custom]).filter((t) => t.length > 0);
188
+ if (tokens.length === 0)
189
+ return [lit(flag.flag)];
190
+ return [lit(flag.flag), lit(tokens.join(","))];
191
+ }
192
+ }
193
+ }
194
+ /**
195
+ * Per-token shell-substitution code. POSIX and fish forms diverge slightly:
196
+ *
197
+ * - POSIX: classic `$(cmd)` with double quotes around `$PWD` so paths with
198
+ * spaces survive word-splitting in `basename`. The double quotes are
199
+ * tolerable inside the outer-double-quoted segment because we're already
200
+ * committed to a "embedded substitution" layer.
201
+ * - fish: `(cmd)` and fish handles variable expansion without the
202
+ * word-splitting concern, so `$PWD` doesn't need inner quoting.
203
+ *
204
+ * Note for POSIX: when this segment is emitted as `'"$(basename "$PWD")"'`,
205
+ * the shell sees `"$(basename "$PWD")"` — outer `"…"` enclosing an inner
206
+ * `"$PWD"`. POSIX (and bash/zsh) handle nested double quotes inside command
207
+ * substitution correctly: the inner `"$PWD"` is parsed within the `$(...)`
208
+ * subshell scope, not the outer shell's scope.
209
+ */
210
+ const TOKEN_TO_SUB = {
211
+ "{folder}": {
212
+ posix: '$(basename "$PWD")',
213
+ fish: "(basename $PWD)",
214
+ },
215
+ "{day}": { posix: "$(date +%d)", fish: "(date +%d)" },
216
+ "{month}": { posix: "$(date +%m)", fish: "(date +%m)" },
217
+ "{year}": { posix: "$(date +%Y)", fish: "(date +%Y)" },
218
+ };
219
+ const TEMPLATE_RE = /\{(folder|day|month|year)\}/g;
220
+ /**
221
+ * Split a templated value into alternating literal and raw parts.
222
+ * Empty literal pieces are dropped. If the value contains no tokens, returns
223
+ * a single literal part covering the full string.
224
+ */
225
+ export function expandTemplateValue(value) {
226
+ const out = [];
227
+ let last = 0;
228
+ for (const match of value.matchAll(TEMPLATE_RE)) {
229
+ const start = match.index;
230
+ if (start > last) {
231
+ out.push({ kind: "literal", text: value.slice(last, start) });
232
+ }
233
+ const sub = TOKEN_TO_SUB[match[0]];
234
+ if (sub) {
235
+ out.push({ kind: "raw", posix: sub.posix, fish: sub.fish });
236
+ }
237
+ last = start + match[0].length;
238
+ }
239
+ if (last < value.length) {
240
+ out.push({ kind: "literal", text: value.slice(last) });
241
+ }
242
+ if (out.length === 0) {
243
+ out.push({ kind: "literal", text: value });
244
+ }
245
+ return out;
246
+ }
247
+ function dedupe(arr) {
248
+ return Array.from(new Set(arr));
249
+ }
250
+ /**
251
+ * Fish quoting. Fish single quotes are simpler — only `\` and `'` need escapes.
252
+ */
253
+ function quoteFish(s) {
254
+ return `'${s.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
255
+ }
256
+ export function renderAlias(config, shell) {
257
+ const args = renderArgs(config);
258
+ const name = config.aliasName;
259
+ if (shell === "fish") {
260
+ return { shell, block: renderFishBlock(name, args) };
261
+ }
262
+ return { shell, block: renderPosixBlock(name, args) };
263
+ }
264
+ function renderPosixBlock(name, segments) {
265
+ // The alias body lives inside outer single quotes:
266
+ // alias <name>='claude … '
267
+ // Each segment becomes one shell-arg inside that body. Composite segments
268
+ // (templated values) interleave literal text with double-quoted shell
269
+ // substitutions via close-outer + double-quote + reopen-outer.
270
+ const body = segments.length === 0
271
+ ? ""
272
+ : " " + segments.map(renderPosixSegment).join(" ");
273
+ const line = `alias ${name}='claude${body}'`;
274
+ return `${BLOCK_BEGIN}\n${line}\n${BLOCK_END}\n`;
275
+ }
276
+ function renderPosixSegment(seg) {
277
+ if (seg.kind === "literal")
278
+ return quoteInsidePosixAlias(seg.text);
279
+ return seg.parts
280
+ .map((p) => p.kind === "literal"
281
+ ? quoteInsidePosixAlias(p.text)
282
+ : embedPosixSubstitution(p.posix))
283
+ .join("");
284
+ }
285
+ /**
286
+ * Embed shell substitution code inside the outer single-quoted alias body
287
+ * so it fires when the alias is expanded, not when the alias is defined.
288
+ *
289
+ * The outer alias body is `'…'`. To inject `$(cmd)` and have it evaluate at
290
+ * expansion time, we close the outer `'`, switch to double quotes (which
291
+ * interpolate), emit the substitution, close the double quotes, reopen the
292
+ * outer `'`. Net source text: `'"$(cmd)"'`.
293
+ *
294
+ * Any inner double quotes in the substitution code (e.g. `"$PWD"` for
295
+ * spaces-in-paths protection) survive because POSIX command substitution
296
+ * has its own quoting context inside `$(...)`.
297
+ */
298
+ function embedPosixSubstitution(posixCode) {
299
+ return `'"${posixCode}"'`;
300
+ }
301
+ const POSIX_BARE = /^[A-Za-z0-9_\-=,:.\/!@%+]+$/;
302
+ function quoteInsidePosixAlias(token) {
303
+ if (POSIX_BARE.test(token))
304
+ return token;
305
+ // No internal single quotes guaranteed by upstream validation.
306
+ return `'\\''${token}'\\''`;
307
+ }
308
+ /**
309
+ * Tokens with embedded single quotes can't be cleanly nested inside an outer
310
+ * single-quoted alias body without compounding escape layers that make the
311
+ * file unreadable. The UI surfaces this as an error before write.
312
+ *
313
+ * Composite segments are not checked here — their literal parts are checked
314
+ * via the same recursion. A `'` in a templated value's literal portion would
315
+ * still be unrenderable, so we walk the parts.
316
+ */
317
+ export function findUnquotableTokens(segments) {
318
+ const out = [];
319
+ for (const seg of segments) {
320
+ if (seg.kind === "literal") {
321
+ if (seg.text.includes("'"))
322
+ out.push(seg.text);
323
+ continue;
324
+ }
325
+ for (const p of seg.parts) {
326
+ if (p.kind === "literal" && p.text.includes("'"))
327
+ out.push(p.text);
328
+ }
329
+ }
330
+ return out;
331
+ }
332
+ function renderFishBlock(name, segments) {
333
+ // Fish: `alias <name> 'claude --foo bar'` — outer single quotes, no `=`.
334
+ const body = segments.length === 0
335
+ ? ""
336
+ : " " + segments.map(renderFishSegment).join(" ");
337
+ const line = `alias ${name} 'claude${body}'`;
338
+ return `${BLOCK_BEGIN}\n${line}\n${BLOCK_END}\n`;
339
+ }
340
+ function renderFishSegment(seg) {
341
+ if (seg.kind === "literal")
342
+ return quoteFish(seg.text);
343
+ return seg.parts
344
+ .map((p) => p.kind === "literal"
345
+ ? quoteFish(p.text)
346
+ : embedFishSubstitution(p.fish))
347
+ .join("");
348
+ }
349
+ /**
350
+ * Fish substitution embedding mirrors POSIX. Outer `'…'` is fish's literal
351
+ * single-quote (no interpolation), so close-and-double-quote-reopen is the
352
+ * same dance: `'"(cmd)"'` becomes literal-' + `(cmd)` evaluated at expansion
353
+ * time + literal-'.
354
+ */
355
+ function embedFishSubstitution(fishCode) {
356
+ return `'"${fishCode}"'`;
357
+ }
358
+ // ─── Splicing ─────────────────────────────────────────────────────────────
359
+ /**
360
+ * Replace the existing managed block in `existing` with `block`, or append
361
+ * `block` to the end if no block is present. Pure function — no I/O.
362
+ */
363
+ export function spliceManagedBlock(existing, block) {
364
+ const beginIdx = existing.indexOf(BLOCK_BEGIN);
365
+ if (beginIdx === -1) {
366
+ if (existing.length === 0)
367
+ return block;
368
+ const sep = existing.endsWith("\n") ? "" : "\n";
369
+ return existing + sep + "\n" + block;
370
+ }
371
+ const endIdx = existing.indexOf(BLOCK_END, beginIdx);
372
+ if (endIdx === -1) {
373
+ // Malformed block — replace from BEGIN to EOF to be safe.
374
+ return existing.slice(0, beginIdx) + block;
375
+ }
376
+ // Consume the trailing newline of the END marker if present.
377
+ let cutEnd = endIdx + BLOCK_END.length;
378
+ if (existing[cutEnd] === "\n")
379
+ cutEnd += 1;
380
+ return existing.slice(0, beginIdx) + block + existing.slice(cutEnd);
381
+ }
382
+ export async function writeAliasToShell(config, target) {
383
+ const args = renderArgs(config);
384
+ const unquotable = findUnquotableTokens(args);
385
+ if (unquotable.length > 0) {
386
+ throw new Error(`Cannot render alias: ${unquotable.length} value${unquotable.length === 1 ? "" : "s"} contain a single quote, which can't be embedded in a shell alias. Edit the offending value(s) and try again: ${unquotable
387
+ .map((t) => JSON.stringify(t))
388
+ .join(", ")}`);
389
+ }
390
+ const rendered = renderAlias(config, target.kind);
391
+ const existing = target.exists ? await readFile(target.path, "utf8") : "";
392
+ const next = spliceManagedBlock(existing, rendered.block);
393
+ await writeFile(target.path, next, "utf8");
394
+ return {
395
+ shell: target.kind,
396
+ path: target.path,
397
+ action: target.exists ? "updated" : "created",
398
+ };
399
+ }
400
+ /**
401
+ * Summarize which flags are in conflict given the current config.
402
+ * The UI uses this to grey out / annotate rows. The renderer enforces the
403
+ * same rules independently — this helper is purely for display.
404
+ */
405
+ export function validateConfig(config) {
406
+ const issues = [];
407
+ // xor groups
408
+ const groups = new Map();
409
+ for (const flag of ALIAS_FLAGS) {
410
+ if (!flag.xorGroup)
411
+ continue;
412
+ if (!isFlagEnabled(config.flags[flag.id]))
413
+ continue;
414
+ const list = groups.get(flag.xorGroup) ?? [];
415
+ list.push(flag.id);
416
+ groups.set(flag.xorGroup, list);
417
+ }
418
+ for (const [group, ids] of groups) {
419
+ if (ids.length > 1) {
420
+ const winner = ids[0];
421
+ const losers = ids.slice(1);
422
+ for (const loser of losers) {
423
+ issues.push({
424
+ flagId: loser,
425
+ reason: `Mutually exclusive with --${winner}; will be dropped on write.`,
426
+ });
427
+ }
428
+ }
429
+ }
430
+ // requires
431
+ for (const flag of ALIAS_FLAGS) {
432
+ if (!flag.requires)
433
+ continue;
434
+ if (!isFlagEnabled(config.flags[flag.id]))
435
+ continue;
436
+ if (!isFlagEnabled(config.flags[flag.requires])) {
437
+ const dep = getFlagById(flag.requires);
438
+ issues.push({
439
+ flagId: flag.id,
440
+ reason: `Requires ${dep.flag}; will be dropped on write.`,
441
+ });
442
+ }
443
+ }
444
+ return issues;
445
+ }
446
+ /**
447
+ * Find the managed block in shell rc text, parse the alias line, and return
448
+ * the alias name plus the argv tokens after `claude`.
449
+ *
450
+ * Returns `null` when no managed block exists, the block is malformed, or
451
+ * the alias line can't be parsed. Callers treat null as "no managed alias
452
+ * on disk — start from defaults".
453
+ */
454
+ export function parseManagedBlock(rcText) {
455
+ const beginIdx = rcText.indexOf(BLOCK_BEGIN);
456
+ if (beginIdx === -1)
457
+ return null;
458
+ const endIdx = rcText.indexOf(BLOCK_END, beginIdx);
459
+ if (endIdx === -1)
460
+ return null;
461
+ // Lines inside the block, sans markers.
462
+ const inner = rcText.slice(beginIdx + BLOCK_BEGIN.length, endIdx);
463
+ // Find the `alias <name>=...` line (skip blank lines, comments, the markers).
464
+ const aliasLine = inner
465
+ .split("\n")
466
+ .map((l) => l.trim())
467
+ .find((l) => /^alias\s+[A-Za-z_][A-Za-z0-9_-]*\s*=/.test(l));
468
+ if (!aliasLine)
469
+ return null;
470
+ return parseAliasLine(aliasLine);
471
+ }
472
+ const ALIAS_LINE_RE = /^alias\s+([A-Za-z_][A-Za-z0-9_-]*)\s*=\s*'(.*)'\s*$/;
473
+ /**
474
+ * Parse a single `alias <name>='<body>'` line. Body is the raw text inside
475
+ * the outer single quotes; the tokenizer below undoes the writer's escape
476
+ * scheme.
477
+ */
478
+ export function parseAliasLine(line) {
479
+ const match = ALIAS_LINE_RE.exec(line);
480
+ if (!match)
481
+ return null;
482
+ const [, aliasName, body] = match;
483
+ // First token is the command itself (claude). Drop it; we only want argv.
484
+ const tokens = tokenizePosixAliasBody(body);
485
+ if (tokens.length === 0 || tokens[0] !== "claude")
486
+ return null;
487
+ return { aliasName, args: tokens.slice(1) };
488
+ }
489
+ /**
490
+ * Tokenize the body of a POSIX alias inside the outer single quotes.
491
+ *
492
+ * Our writer emits three shapes inside the outer `'…'`:
493
+ * 1. Bare tokens (matching POSIX_BARE) — emitted as-is.
494
+ * 2. Quoted literals: `'\''text without single quotes'\''` — these survive
495
+ * the outer single quotes by closing them, emitting a literal `'`,
496
+ * single-quoting the inner content, then a literal `'`, and reopening.
497
+ * 3. Substitution embeds: `'"$(cmd)"'` — close outer ', open ", embed,
498
+ * close ", reopen outer '. Result is one shell arg with the
499
+ * substitution that fires on alias expansion.
500
+ *
501
+ * Reading the body left-to-right, we expect to see:
502
+ * - bare chars (collect into the current token's value)
503
+ * - whitespace (delimits tokens)
504
+ * - `'\''<text>'\''` (collect inner text verbatim into the current token)
505
+ * - `'"<sub>"'` (record as a raw substitution and unparse it back into
506
+ * the closest matching template token via `unparseSubstitution`)
507
+ *
508
+ * Tokens emitted as `composite` segments (template values) re-tokenize into
509
+ * a single shell arg whose pieces are interleaved literal+raw. We rebuild
510
+ * those as `{token}` strings to match what the UI stores.
511
+ */
512
+ export function tokenizePosixAliasBody(body) {
513
+ const tokens = [];
514
+ let current = "";
515
+ let inToken = false;
516
+ let i = 0;
517
+ const len = body.length;
518
+ const flush = () => {
519
+ if (inToken) {
520
+ tokens.push(current);
521
+ current = "";
522
+ inToken = false;
523
+ }
524
+ };
525
+ while (i < len) {
526
+ const ch = body[i];
527
+ if (ch === " " || ch === "\t") {
528
+ flush();
529
+ i += 1;
530
+ continue;
531
+ }
532
+ // Quoted literal: '\''…'\''
533
+ if (body.startsWith(`'\\''`, i)) {
534
+ // Skip the opening sequence.
535
+ i += 4;
536
+ const end = body.indexOf(`'\\''`, i);
537
+ if (end === -1)
538
+ return tokens; // malformed — bail, return what we have
539
+ current += body.slice(i, end);
540
+ inToken = true;
541
+ i = end + 4;
542
+ continue;
543
+ }
544
+ // Substitution embed: '"<sub>"'
545
+ if (body.startsWith(`'"`, i)) {
546
+ const inner = i + 2;
547
+ const close = body.indexOf(`"'`, inner);
548
+ if (close === -1)
549
+ return tokens;
550
+ const subCode = body.slice(inner, close);
551
+ const token = unparseSubstitution(subCode);
552
+ current += token;
553
+ inToken = true;
554
+ i = close + 2;
555
+ continue;
556
+ }
557
+ // Bare character.
558
+ current += ch;
559
+ inToken = true;
560
+ i += 1;
561
+ }
562
+ flush();
563
+ return tokens;
564
+ }
565
+ /**
566
+ * Map a POSIX substitution code back to its template token, if one matches.
567
+ * Falls through to a `$(...)` literal when no token matches — that way we
568
+ * preserve hand-edited substitutions as opaque values rather than dropping
569
+ * them.
570
+ *
571
+ * Mirror of TOKEN_TO_SUB in the writer.
572
+ */
573
+ function unparseSubstitution(posixCode) {
574
+ switch (posixCode) {
575
+ case '$(basename "$PWD")':
576
+ return "{folder}";
577
+ case "$(date +%d)":
578
+ return "{day}";
579
+ case "$(date +%m)":
580
+ return "{month}";
581
+ case "$(date +%Y)":
582
+ return "{year}";
583
+ default:
584
+ // Unknown substitution: pass through the raw code so the user can see
585
+ // it in the UI and decide whether to keep it. The writer can't faithfully
586
+ // round-trip it, but at least we don't silently drop it.
587
+ return posixCode;
588
+ }
589
+ }
590
+ /**
591
+ * Apply parsed argv tokens to a FlagValue map. Walks the catalog in order,
592
+ * consuming tokens that match each flag's pattern. Tokens that don't match
593
+ * any flag are dropped silently (e.g. the user hand-added a flag we don't
594
+ * know about). Returns a fresh `Record<flagId, FlagValue>` suitable for
595
+ * splicing into an AliasConfig.
596
+ *
597
+ * Ordering: the writer emits flags in catalog order, so a left-to-right
598
+ * scan is sufficient. We pre-index by flag string for `--foo` lookups.
599
+ */
600
+ export function argsToFlagValues(args) {
601
+ // Index every flag by its primary `flag` string AND its triStateOff variant.
602
+ const byFlag = new Map();
603
+ for (const flag of ALIAS_FLAGS) {
604
+ byFlag.set(flag.flag, { flag, off: false });
605
+ if (flag.triStateOff) {
606
+ byFlag.set(flag.triStateOff, { flag, off: true });
607
+ }
608
+ }
609
+ const out = {};
610
+ let i = 0;
611
+ while (i < args.length) {
612
+ const tok = args[i];
613
+ const entry = byFlag.get(tok);
614
+ if (!entry) {
615
+ // Unknown flag — skip this token AND its likely value, if next looks
616
+ // like a value (doesn't start with --). This is a best-effort skip
617
+ // so unknown flag pairs don't cascade-corrupt the rest of the parse.
618
+ i += 1;
619
+ if (i < args.length && !args[i].startsWith("-"))
620
+ i += 1;
621
+ continue;
622
+ }
623
+ const { flag, off } = entry;
624
+ const peek = () => args[i + 1];
625
+ const takeValue = () => {
626
+ const v = args[i + 1];
627
+ i += 2;
628
+ return v;
629
+ };
630
+ switch (flag.kind) {
631
+ case "boolean":
632
+ out[flag.id] = { kind: "boolean", enabled: true };
633
+ i += 1;
634
+ break;
635
+ case "tri-state":
636
+ out[flag.id] = { kind: "tri-state", state: off ? "off" : "on" };
637
+ i += 1;
638
+ break;
639
+ case "select": {
640
+ // The writer emits `--flag` alone for "bare" empty value, or
641
+ // `--flag <value>` for a selection. We can't tell without peeking
642
+ // at whether the next token is a known value.
643
+ const opts = flag.options ?? [];
644
+ const next = peek();
645
+ const valid = next !== undefined && opts.some((o) => o.value === next);
646
+ if (valid) {
647
+ out[flag.id] = { kind: "select", enabled: true, value: takeValue() };
648
+ }
649
+ else {
650
+ out[flag.id] = { kind: "select", enabled: true, value: "" };
651
+ i += 1;
652
+ }
653
+ break;
654
+ }
655
+ case "text":
656
+ // `text` always emits a value; if there's no next token we treat as
657
+ // disabled (defensive).
658
+ if (peek() !== undefined) {
659
+ out[flag.id] = { kind: "text", enabled: true, value: takeValue() };
660
+ }
661
+ else {
662
+ out[flag.id] = { kind: "text", enabled: false, value: "" };
663
+ i += 1;
664
+ }
665
+ break;
666
+ case "optional-text": {
667
+ // Optional-text: next token is the value if it doesn't look like a
668
+ // flag. We can't perfectly distinguish a deliberately-bare emission
669
+ // from one followed by an unrelated flag, but the writer's emission
670
+ // of bare is `flag.flag` alone with the next token being another
671
+ // catalog flag — so checking the lookup tells us.
672
+ const next = peek();
673
+ if (next === undefined || byFlag.has(next)) {
674
+ out[flag.id] = { kind: "optional-text", enabled: true, value: "" };
675
+ i += 1;
676
+ }
677
+ else {
678
+ out[flag.id] = {
679
+ kind: "optional-text",
680
+ enabled: true,
681
+ value: takeValue(),
682
+ };
683
+ }
684
+ break;
685
+ }
686
+ case "text-list": {
687
+ // Each instance of the flag emits one value. Collect repeats.
688
+ const values = [];
689
+ // Consume the first pair.
690
+ if (peek() !== undefined)
691
+ values.push(takeValue());
692
+ else
693
+ i += 1;
694
+ // Consume additional pairs of the same flag.
695
+ while (i < args.length && args[i] === flag.flag) {
696
+ if (i + 1 < args.length) {
697
+ values.push(args[i + 1]);
698
+ i += 2;
699
+ }
700
+ else {
701
+ i += 1;
702
+ break;
703
+ }
704
+ }
705
+ out[flag.id] = {
706
+ kind: "text-list",
707
+ enabled: true,
708
+ values,
709
+ };
710
+ break;
711
+ }
712
+ case "multi-with-custom": {
713
+ // Either bare `--debug` (no filter) or `--debug a,b,c`.
714
+ const next = peek();
715
+ if (next === undefined || byFlag.has(next)) {
716
+ out[flag.id] = {
717
+ kind: "multi-with-custom",
718
+ enabled: true,
719
+ picked: [],
720
+ custom: [],
721
+ };
722
+ i += 1;
723
+ }
724
+ else {
725
+ const tokens = takeValue()
726
+ .split(",")
727
+ .map((s) => s.trim())
728
+ .filter((s) => s.length > 0);
729
+ const picklist = new Set(flag.picklist ?? []);
730
+ const picked = [];
731
+ const custom = [];
732
+ for (const t of tokens) {
733
+ if (picklist.has(t))
734
+ picked.push(t);
735
+ else
736
+ custom.push(t);
737
+ }
738
+ out[flag.id] = {
739
+ kind: "multi-with-custom",
740
+ enabled: true,
741
+ picked,
742
+ custom,
743
+ };
744
+ }
745
+ break;
746
+ }
747
+ }
748
+ }
749
+ return out;
750
+ }
751
+ /**
752
+ * High-level entry point: read shell rc text, parse the managed block, and
753
+ * return a populated flag map plus the parsed alias name. Returns null when
754
+ * there's no managed block to parse from.
755
+ */
756
+ export function parseAliasFromRc(rcText) {
757
+ const parsed = parseManagedBlock(rcText);
758
+ if (!parsed || !parsed.aliasName)
759
+ return null;
760
+ return {
761
+ aliasName: parsed.aliasName,
762
+ flags: argsToFlagValues(parsed.args),
763
+ };
764
+ }