char-slop/ai-dots

ai dotfiles

git clone https://git.t4t.associates/char-slop/ai-dots

Charlotte Sompermission-gate: analyze through cwd changes6ff1139

main
1.8 KiB51 linesraw
1import * as path from "node:path";
2import {
3  resolvePath,
4  type Analysis,
5  type Invocation,
6  type ShellWord,
7  type WorkingDirectory,
8} from "bash-effect-analyzer";
9
10export function resolvesToFilesystemRoot(cwd: string, target: string): boolean {
11  const resolved = path.resolve(cwd, target);
12  return resolved === path.parse(resolved).root;
13}
14
15function wordTargetsRoot(word: ShellWord, cwd: WorkingDirectory, initialCwd: string): boolean {
16  return word.kind === "literal" && resolvePath(word, cwd, initialCwd).paths.some((target) => target === "/");
17}
18
19function findTargetsRoot(invocation: Invocation, initialCwd: string): boolean {
20  let hasStartingPoint = false;
21  for (let i = 1; i < invocation.argv.length; i++) {
22    const word = invocation.argv[i];
23    const value = word.kind === "literal" ? word.value : undefined;
24
25    if (value === "--") continue;
26    if (value === "-H" || value === "-L" || value === "-P" || value?.startsWith("-O")) {
27      continue;
28    }
29    if (value === "-D") {
30      i++;
31      continue;
32    }
33
34    if (value === undefined) return false;
35    if (value.startsWith("-") || ["!", "(", ")", ","].includes(value)) break;
36    hasStartingPoint = true;
37    if (wordTargetsRoot(word, invocation.cwd, initialCwd)) return true;
38  }
39  return !hasStartingPoint && wordTargetsRoot(
40    { kind: "literal", value: ".", text: "." }, invocation.cwd, initialCwd,
41  );
42}
43
44export function rootTargetCommand(analysis: Analysis, cwd: string): "find" | "rm" | undefined {
45  for (const invocation of analysis.invocations) {
46    if (invocation.effects.some((effect) =>
47      effect.kind === "delete" && effect.via === "rm" && wordTargetsRoot(effect.path, effect.cwd, cwd)
48    )) return "rm";
49    if (invocation.name === "find" && findTargetsRoot(invocation, cwd)) return "find";
50  }
51}