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