@cmodjs/core 6.1.1 → 6.2.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/docs/mod.md CHANGED
@@ -594,7 +594,7 @@ export const myMod = defineMod({
594
594
  parseShell(line: string): ParsedShell
595
595
 
596
596
  type ParsedShell = { commands: ShellCommand[]; reads: string[]; writes: string[]; fetches: string[]; isFullyParsed: boolean }
597
- type ShellCommand = { argv: [string, ...string[]]; folder: string; input?: number }
597
+ type ShellCommand = { argv: [string, ...string[]]; folder: string; input?: number; runner?: number }
598
598
 
599
599
  resolve(...paths): string join(...paths): string dirname(path): string basename(path, suffix?): string
600
600
  extname(path): string relative(from, to): string isAbsolute(path): boolean normalize(path): string
@@ -603,7 +603,8 @@ extname(path): string relative(from, to): string isAbsolute(path): bool
603
603
  - `commands` lists every command the line runs, those inside `sudo`, `bash -c`, `$(…)`, and heredocs included, with flags such as `-rf` split into `-r` and `-f`.
604
604
  - `folder` is where a command runs, as the line wrote it: `''` for the folder the line starts in, `src` after `cd src`, `/tmp` after `cd /tmp`, and `~/p` after `cd ~/p`. `resolve(call.cwd, folder)` makes it absolute.
605
605
  - `input` is the index in `commands` of the command whose output this one reads through `|` or `|&`, and is absent for a command that reads no pipe. `trace read a.ts | head -5` gives `head` the `input` `0`. A wrapper such as `timeout 5 trace read a.ts` writes the output of the command it runs, so `input` names `trace`, not `timeout`.
606
- - `reads`, `writes`, and `fetches` are the paths and URLs the commands name, as written. A path that holds a shell expansion makes `isFullyParsed` false, and so does a line the parser cannot read.
606
+ - `runner` is the index in `commands` of the command that runs this one, such as `sudo`, `xargs`, `find -exec`, or `bash -c`, and is absent for a command the line's own shell runs, `$(…)` and `( … )` included. `sudo rm x` gives `rm` the `runner` `0`, and in `sudo bash -c 'rm x'` the runners chain from `rm` to `bash` to `sudo`.
607
+ - `reads`, `writes`, and `fetches` are the paths and URLs the commands name, as written. A `{}` in a `find -exec` action stands for each start path of the `find`, so `find build -exec rm {} +` writes `build`, as `find build -delete` does, while its `argv` keeps `{}`. A path that holds a shell expansion makes `isFullyParsed` false, and so does a line the parser cannot read.
607
608
  - `resolve` has no working folder to start from in Claude Code, so pass an absolute folder first, such as `resolve(sessions.current.cwd, path)`.
608
609
 
609
610
  ```ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmodjs/core",
3
- "version": "6.1.1",
3
+ "version": "6.2.0",
4
4
  "description": "Build Claude Code mods: hooks, panes, checks, and an install that just works.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/records.d.ts CHANGED
@@ -174,7 +174,7 @@ export declare function newestLog(entries: readonly LogEntry[]): LogEntry[];
174
174
  export declare function versionsLine({ mod, core, cmod, program, claudeCode }: LogVersions): string;
175
175
  export declare function logReport(entry: LogEntry): string;
176
176
  export declare function parseConsent(value: unknown, path: string): Record<string, string[]>;
177
- export declare const coreVersion = "6.1.1";
177
+ export declare const coreVersion = "6.2.0";
178
178
  export declare const oldestCmodVersion = "6.1.0";
179
179
  export declare function hotkeyCommand(mod: string, hotkey: string): string;
180
180
  export declare function hotkeyBindings(mod: string, hotkeys: Hotkeys | undefined): Record<string, string>;
package/records.js CHANGED
@@ -378,7 +378,7 @@ function readHotkeys(hotkeys) {
378
378
  throw new Error(`package.json "cmod.hotkeys" is not a map of hotkeys to their title and optional default combo. ${fix}`);
379
379
  return { hotkeys: checkHotkeys('package.json "cmod"', hotkeys) };
380
380
  }
381
- export const coreVersion = '6.1.1';
381
+ export const coreVersion = '6.2.0';
382
382
  export const oldestCmodVersion = '6.1.0';
383
383
  export function hotkeyCommand(mod, hotkey) {
384
384
  return `${mod}--${hotkey}`;
package/src/records.ts CHANGED
@@ -458,7 +458,7 @@ function readHotkeys(hotkeys: unknown): Steps {
458
458
  return { hotkeys: checkHotkeys('package.json "cmod"', hotkeys as Record<string, HotkeyInit>) }
459
459
  }
460
460
 
461
- export const coreVersion = '6.1.1'
461
+ export const coreVersion = '6.2.0'
462
462
 
463
463
  export const oldestCmodVersion = '6.1.0'
464
464
 
@@ -1,6 +1,6 @@
1
1
  import { basename, parse, type GlobPattern, type ParseEntry } from '../vendor.js'
2
2
 
3
- export type ShellCommand = { argv: [string, ...string[]]; folder: string; input?: number }
3
+ export type ShellCommand = { argv: [string, ...string[]]; folder: string; input?: number; runner?: number }
4
4
 
5
5
  export type ParsedShell = {
6
6
  commands: ShellCommand[]
@@ -582,8 +582,9 @@ function addCommand(first: Word, args: Word[], folder: string, stdin: Stdin | un
582
582
  else if (wrappers.has(program)) addWrapper(first, program, args, folder, stdin, result)
583
583
  else {
584
584
  const flags = expandFlags(program, args)
585
- result.commands.push({ argv: [first.text, ...flags], folder })
585
+ const runner = result.commands.push({ argv: [first.text, ...flags], folder }) - 1
586
586
  addEffects(program, args, flags, folder, stdin, result)
587
+ runBy(runner, result)
587
588
  if (program === 'cd') return changedFolder(args, folder)
588
589
  }
589
590
  return undefined
@@ -613,13 +614,35 @@ function addFind(first: Word, args: Word[], folder: string, result: ParsedShell)
613
614
  actions.push(args.slice(index + 1, end < 0 ? args.length : end))
614
615
  index = end < 0 ? args.length : end
615
616
  }
616
- result.commands.push({ argv: [first.text, ...own], folder })
617
- if (own.includes('-delete')) for (const start of findStarts(args)) addPath(result, 'writes', start, folder)
618
- for (const [actionFirst, ...actionArgs] of actions) {
619
- if (actionFirst !== undefined) addCommand(actionFirst, actionArgs, folder, undefined, result)
617
+ const runner = result.commands.push({ argv: [first.text, ...own], folder }) - 1
618
+ const starts = findStarts(args)
619
+ if (own.includes('-delete')) for (const start of starts) addPath(result, 'writes', start, folder)
620
+ for (const action of actions) addFindAction(action, starts, folder, result)
621
+ runBy(runner, result)
622
+ }
623
+
624
+ function addFindAction(action: Word[], starts: Word[], folder: string, result: ParsedShell): void {
625
+ const [first, ...args] = action
626
+ if (first === undefined) return
627
+ const reads = result.reads.length
628
+ const writes = result.writes.length
629
+ addCommand(first, args, folder, undefined, result)
630
+ if (!action.some((word) => word.text.includes('{}'))) return
631
+ result.reads.splice(reads)
632
+ result.writes.splice(writes)
633
+ for (const start of starts) {
634
+ const found: ParsedShell = { commands: [], reads: result.reads, writes: result.writes, fetches: [], isFullyParsed: true }
635
+ const [foundFirst, ...foundArgs] = action.map((word) => foundWord(word, start))
636
+ addCommand(foundFirst!, foundArgs, folder, undefined, found)
637
+ if (!found.isFullyParsed) result.isFullyParsed = false
620
638
  }
621
639
  }
622
640
 
641
+ function foundWord(word: Word, start: Word): Word {
642
+ if (!word.text.includes('{}')) return word
643
+ return { text: word.text.replaceAll('{}', start.text), isGlob: word.isGlob || start.isGlob, isDynamic: word.isDynamic || start.isDynamic }
644
+ }
645
+
623
646
  function findStarts(args: Word[]): Word[] {
624
647
  let index = 0
625
648
  for (let text = args[0]?.text; text === '-D' || /^-([HLP]|O\d*)$/.test(text ?? ''); text = args[index]?.text) index += text === '-D' ? 2 : 1
@@ -637,18 +660,20 @@ function addWrapper(first: Word, program: string, args: Word[], folder: string,
637
660
  start += wrapper.leadingOperands ?? 0
638
661
  while (wrapper.takesAssignments === true && assignmentPattern.test(args[start]?.text ?? '')) start += 1
639
662
  const hasTrailingCode = wrapper.codeOptions?.includes(args[start]?.text ?? '') === true
640
- result.commands.push({ argv: [first.text, ...expandFlags(program, args.slice(0, start))], folder })
663
+ const runner = result.commands.push({ argv: [first.text, ...expandFlags(program, args.slice(0, start))], folder }) - 1
641
664
  const innerFolder = options.findLast((option) => wrapper.folderOptions?.includes(option.name) === true)?.value
642
665
  const wrappedFolder = innerFolder === undefined ? folder : joinFolder(folder, innerFolder.text)
643
666
  const code = hasTrailingCode ? args[start + 1] : options.findLast((option) => wrapper.codeOptions?.includes(option.name) === true)?.value
644
667
  const rest = args.slice(hasTrailingCode ? start + 2 : start)
645
668
  const isShell = wrapper.shellUnless !== undefined && !options.some((option) => wrapper.shellUnless?.includes(option.name) === true)
646
- if (code !== undefined || isShell) {
647
- parseLine([...(code === undefined ? [] : [code]), ...rest].map((word) => word.text).join(' '), wrappedFolder, result)
648
- return
649
- }
650
669
  const [innerFirst, ...innerArgs] = rest
651
- if (innerFirst !== undefined) addCommand(innerFirst, innerArgs, wrappedFolder, stdin, result)
670
+ if (code !== undefined || isShell) parseLine([...(code === undefined ? [] : [code]), ...rest].map((word) => word.text).join(' '), wrappedFolder, result)
671
+ else if (innerFirst !== undefined) addCommand(innerFirst, innerArgs, wrappedFolder, stdin, result)
672
+ runBy(runner, result)
673
+ }
674
+
675
+ function runBy(runner: number, result: ParsedShell): void {
676
+ for (const command of result.commands.slice(runner + 1)) command.runner ??= runner
652
677
  }
653
678
 
654
679
  function addEffects(program: string, args: Word[], flags: string[], folder: string, stdin: Stdin | undefined, result: ParsedShell): void {
@@ -2,6 +2,7 @@ export type ShellCommand = {
2
2
  argv: [string, ...string[]];
3
3
  folder: string;
4
4
  input?: number;
5
+ runner?: number;
5
6
  };
6
7
  export type ParsedShell = {
7
8
  commands: ShellCommand[];
@@ -1 +1 @@
1
- {"version":3,"file":"parse-shell.d.ts","sourceRoot":"","sources":["../src/utils/parse-shell.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,YAAY,GAAG;IAAE,IAAI,EAAE,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAAA;AAE1F,MAAM,MAAM,WAAW,GAAG;IACxB,QAAQ,EAAE,YAAY,EAAE,CAAA;IACxB,MAAM,EAAE,MAAM,EAAE,CAAA;IAChB,KAAK,EAAE,MAAM,EAAE,CAAA;IACf,OAAO,EAAE,MAAM,EAAE,CAAA;IACjB,aAAa,EAAE,OAAO,CAAA;CACvB,CAAA;AA4BD,eAAO,MAAM,cAAc,QAAS,CAAA;AAiKpC,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,CAIpD"}
1
+ {"version":3,"file":"parse-shell.d.ts","sourceRoot":"","sources":["../src/utils/parse-shell.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,YAAY,GAAG;IAAE,IAAI,EAAE,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,CAAA;AAE3G,MAAM,MAAM,WAAW,GAAG;IACxB,QAAQ,EAAE,YAAY,EAAE,CAAA;IACxB,MAAM,EAAE,MAAM,EAAE,CAAA;IAChB,KAAK,EAAE,MAAM,EAAE,CAAA;IACf,OAAO,EAAE,MAAM,EAAE,CAAA;IACjB,aAAa,EAAE,OAAO,CAAA;CACvB,CAAA;AA4BD,eAAO,MAAM,cAAc,QAAS,CAAA;AAiKpC,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,CAIpD"}
@@ -586,8 +586,9 @@ function addCommand(first, args, folder, stdin, result) {
586
586
  addWrapper(first, program, args, folder, stdin, result);
587
587
  else {
588
588
  const flags = expandFlags(program, args);
589
- result.commands.push({ argv: [first.text, ...flags], folder });
589
+ const runner = result.commands.push({ argv: [first.text, ...flags], folder }) - 1;
590
590
  addEffects(program, args, flags, folder, stdin, result);
591
+ runBy(runner, result);
591
592
  if (program === 'cd')
592
593
  return changedFolder(args, folder);
593
594
  }
@@ -618,15 +619,39 @@ function addFind(first, args, folder, result) {
618
619
  actions.push(args.slice(index + 1, end < 0 ? args.length : end));
619
620
  index = end < 0 ? args.length : end;
620
621
  }
621
- result.commands.push({ argv: [first.text, ...own], folder });
622
+ const runner = result.commands.push({ argv: [first.text, ...own], folder }) - 1;
623
+ const starts = findStarts(args);
622
624
  if (own.includes('-delete'))
623
- for (const start of findStarts(args))
625
+ for (const start of starts)
624
626
  addPath(result, 'writes', start, folder);
625
- for (const [actionFirst, ...actionArgs] of actions) {
626
- if (actionFirst !== undefined)
627
- addCommand(actionFirst, actionArgs, folder, undefined, result);
627
+ for (const action of actions)
628
+ addFindAction(action, starts, folder, result);
629
+ runBy(runner, result);
630
+ }
631
+ function addFindAction(action, starts, folder, result) {
632
+ const [first, ...args] = action;
633
+ if (first === undefined)
634
+ return;
635
+ const reads = result.reads.length;
636
+ const writes = result.writes.length;
637
+ addCommand(first, args, folder, undefined, result);
638
+ if (!action.some((word) => word.text.includes('{}')))
639
+ return;
640
+ result.reads.splice(reads);
641
+ result.writes.splice(writes);
642
+ for (const start of starts) {
643
+ const found = { commands: [], reads: result.reads, writes: result.writes, fetches: [], isFullyParsed: true };
644
+ const [foundFirst, ...foundArgs] = action.map((word) => foundWord(word, start));
645
+ addCommand(foundFirst, foundArgs, folder, undefined, found);
646
+ if (!found.isFullyParsed)
647
+ result.isFullyParsed = false;
628
648
  }
629
649
  }
650
+ function foundWord(word, start) {
651
+ if (!word.text.includes('{}'))
652
+ return word;
653
+ return { text: word.text.replaceAll('{}', start.text), isGlob: word.isGlob || start.isGlob, isDynamic: word.isDynamic || start.isDynamic };
654
+ }
630
655
  function findStarts(args) {
631
656
  let index = 0;
632
657
  for (let text = args[0]?.text; text === '-D' || /^-([HLP]|O\d*)$/.test(text ?? ''); text = args[index]?.text)
@@ -647,19 +672,22 @@ function addWrapper(first, program, args, folder, stdin, result) {
647
672
  while (wrapper.takesAssignments === true && assignmentPattern.test(args[start]?.text ?? ''))
648
673
  start += 1;
649
674
  const hasTrailingCode = wrapper.codeOptions?.includes(args[start]?.text ?? '') === true;
650
- result.commands.push({ argv: [first.text, ...expandFlags(program, args.slice(0, start))], folder });
675
+ const runner = result.commands.push({ argv: [first.text, ...expandFlags(program, args.slice(0, start))], folder }) - 1;
651
676
  const innerFolder = options.findLast((option) => wrapper.folderOptions?.includes(option.name) === true)?.value;
652
677
  const wrappedFolder = innerFolder === undefined ? folder : joinFolder(folder, innerFolder.text);
653
678
  const code = hasTrailingCode ? args[start + 1] : options.findLast((option) => wrapper.codeOptions?.includes(option.name) === true)?.value;
654
679
  const rest = args.slice(hasTrailingCode ? start + 2 : start);
655
680
  const isShell = wrapper.shellUnless !== undefined && !options.some((option) => wrapper.shellUnless?.includes(option.name) === true);
656
- if (code !== undefined || isShell) {
657
- parseLine([...(code === undefined ? [] : [code]), ...rest].map((word) => word.text).join(' '), wrappedFolder, result);
658
- return;
659
- }
660
681
  const [innerFirst, ...innerArgs] = rest;
661
- if (innerFirst !== undefined)
682
+ if (code !== undefined || isShell)
683
+ parseLine([...(code === undefined ? [] : [code]), ...rest].map((word) => word.text).join(' '), wrappedFolder, result);
684
+ else if (innerFirst !== undefined)
662
685
  addCommand(innerFirst, innerArgs, wrappedFolder, stdin, result);
686
+ runBy(runner, result);
687
+ }
688
+ function runBy(runner, result) {
689
+ for (const command of result.commands.slice(runner + 1))
690
+ command.runner ??= runner;
663
691
  }
664
692
  function addEffects(program, args, flags, folder, stdin, result) {
665
693
  const fileProgram = fileReaders.get(program);