char-slop/berry
multipurpose self-modifiable agentic harness
git clone https://git.t4t.associates/char-slop/berry
eda338f
main
1import type { Context } from "@earendil-works/chord" ; 2import { getOrThrow } from "@earendil-works/pi-durable/env" ; 3import { posix , win32 } from "node:path" ; 4import type { Environment } from "@berry/api/environment.ts" ; 5 6export async function folders ( 7env :Environment , 8prefix :string , 9context :Context , 10) :Promise < string []> { 11if ( prefix . includes ( "\0" )) throw new Error ( "Invalid directory prefix" ); 12const paths = env . platform === "win32" ?win32 :posix ; 13const expanded = prefix === "~" || / ^~[/\\] / u . test ( prefix ) ?env . home + prefix . slice ( 1 ) :prefix ; 14const trailing = expanded === "" || 15( env . platform === "win32" ?/ [/\\]$ / u :/ \/$ / u ). test ( expanded ); 16const parent = trailing ?expanded || "." :paths . dirname ( expanded ); 17const query = trailing ?"" :paths . basename ( expanded ); 18const absolute = getOrThrow ( await env . absolutePath ( parent , context )); 19const entries = getOrThrow ( await env . listDir ( absolute , context )); 20const matches = []; 21for ( const entry of entries . sort (( a , b ) => a . name . localeCompare ( b . name ))) { 22const matchesPrefix = env . platform === "win32" 23 ?entry . name . toLowerCase (). startsWith ( query . toLowerCase ()) 24 :entry . name . startsWith ( query ); 25if ( ! matchesPrefix || ( entry . kind !== "directory" && entry . kind !== "symlink" )) continue ; 26if ( entry . kind === "symlink" ) { 27const canonical = await env . canonicalPath ( entry . path , context ); 28if ( ! canonical . ok ) continue ; 29const info = await env . fileInfo ( canonical . value , context ); 30if ( ! info . ok || info . value . kind !== "directory" ) continue ; 31} 32matches . push ( paths . join ( parent === "." ?"" :parent , entry . name ) + paths . sep ); 33if ( matches . length === 50 ) break ; 34} 35return matches ; 36}