1+ import * as OS from "node:os" ;
12import fsPromises from "node:fs/promises" ;
23import type { Dirent } from "node:fs" ;
34
45import { Cache , Duration , Effect , Exit , Layer , Option , Path } from "effect" ;
56
6- import { type ProjectEntry } from "@t3tools/contracts" ;
7+ import { type FilesystemBrowseInput , type ProjectEntry } from "@t3tools/contracts" ;
8+ import { isExplicitRelativePath , isWindowsAbsolutePath } from "@t3tools/shared/path" ;
79import {
810 insertRankedSearchResult ,
911 normalizeSearchQuery ,
@@ -14,6 +16,7 @@ import {
1416import { GitCore } from "../../git/Services/GitCore.ts" ;
1517import {
1618 WorkspaceEntries ,
19+ WorkspaceEntriesBrowseError ,
1720 WorkspaceEntriesError ,
1821 type WorkspaceEntriesShape ,
1922} from "../Services/WorkspaceEntries.ts" ;
@@ -52,6 +55,16 @@ function toPosixPath(input: string): string {
5255 return input . replaceAll ( "\\" , "/" ) ;
5356}
5457
58+ function expandHomePath ( input : string , path : Path . Path ) : string {
59+ if ( input === "~" ) {
60+ return OS . homedir ( ) ;
61+ }
62+ if ( input . startsWith ( "~/" ) || input . startsWith ( "~\\" ) ) {
63+ return path . join ( OS . homedir ( ) , input . slice ( 2 ) ) ;
64+ }
65+ return input ;
66+ }
67+
5568function parentPathOf ( input : string ) : string | undefined {
5669 const separatorIndex = input . lastIndexOf ( "/" ) ;
5770 if ( separatorIndex === - 1 ) {
@@ -129,6 +142,36 @@ function directoryAncestorsOf(relativePath: string): string[] {
129142 return directories ;
130143}
131144
145+ const resolveBrowseTarget = (
146+ input : FilesystemBrowseInput ,
147+ pathService : Path . Path ,
148+ ) : Effect . Effect < string , WorkspaceEntriesBrowseError > =>
149+ Effect . gen ( function * ( ) {
150+ if ( process . platform !== "win32" && isWindowsAbsolutePath ( input . partialPath ) ) {
151+ return yield * new WorkspaceEntriesBrowseError ( {
152+ cwd : input . cwd ,
153+ partialPath : input . partialPath ,
154+ operation : "workspaceEntries.resolveBrowseTarget" ,
155+ detail : "Windows-style paths are only supported on Windows." ,
156+ } ) ;
157+ }
158+
159+ if ( ! isExplicitRelativePath ( input . partialPath ) ) {
160+ return pathService . resolve ( expandHomePath ( input . partialPath , pathService ) ) ;
161+ }
162+
163+ if ( ! input . cwd ) {
164+ return yield * new WorkspaceEntriesBrowseError ( {
165+ cwd : input . cwd ,
166+ partialPath : input . partialPath ,
167+ operation : "workspaceEntries.resolveBrowseTarget" ,
168+ detail : "Relative filesystem browse paths require a current project." ,
169+ } ) ;
170+ }
171+
172+ return pathService . resolve ( expandHomePath ( input . cwd , pathService ) , input . partialPath ) ;
173+ } ) ;
174+
132175export const makeWorkspaceEntries = Effect . gen ( function * ( ) {
133176 const path = yield * Path . Path ;
134177 const gitOption = yield * Effect . serviceOption ( GitCore ) ;
@@ -379,6 +422,46 @@ export const makeWorkspaceEntries = Effect.gen(function* () {
379422 } ,
380423 ) ;
381424
425+ const browse : WorkspaceEntriesShape [ "browse" ] = Effect . fn ( "WorkspaceEntries.browse" ) (
426+ function * ( input ) {
427+ const resolvedInputPath = yield * resolveBrowseTarget ( input , path ) ;
428+ const endsWithSeparator = / [ \\ / ] $ / . test ( input . partialPath ) || input . partialPath === "~" ;
429+ const parentPath = endsWithSeparator ? resolvedInputPath : path . dirname ( resolvedInputPath ) ;
430+ const prefix = endsWithSeparator ? "" : path . basename ( resolvedInputPath ) ;
431+
432+ const dirents = yield * Effect . tryPromise ( {
433+ try : ( ) => fsPromises . readdir ( parentPath , { withFileTypes : true } ) ,
434+ catch : ( cause ) =>
435+ new WorkspaceEntriesBrowseError ( {
436+ cwd : input . cwd ,
437+ partialPath : input . partialPath ,
438+ operation : "workspaceEntries.browse.readDirectory" ,
439+ detail : `Unable to browse '${ parentPath } ': ${ cause instanceof Error ? cause . message : String ( cause ) } ` ,
440+ cause,
441+ } ) ,
442+ } ) ;
443+
444+ const showHidden = endsWithSeparator || prefix . startsWith ( "." ) ;
445+ const lowerPrefix = prefix . toLowerCase ( ) ;
446+
447+ return {
448+ parentPath,
449+ entries : dirents
450+ . filter (
451+ ( dirent ) =>
452+ dirent . isDirectory ( ) &&
453+ dirent . name . toLowerCase ( ) . startsWith ( lowerPrefix ) &&
454+ ( showHidden || ! dirent . name . startsWith ( "." ) ) ,
455+ )
456+ . map ( ( dirent ) => ( {
457+ name : dirent . name ,
458+ fullPath : path . join ( parentPath , dirent . name ) ,
459+ } ) )
460+ . toSorted ( ( left , right ) => left . name . localeCompare ( right . name ) ) ,
461+ } ;
462+ } ,
463+ ) ;
464+
382465 const search : WorkspaceEntriesShape [ "search" ] = Effect . fn ( "WorkspaceEntries.search" ) (
383466 function * ( input ) {
384467 const normalizedCwd = yield * normalizeWorkspaceRoot ( input . cwd ) ;
@@ -415,6 +498,7 @@ export const makeWorkspaceEntries = Effect.gen(function* () {
415498 ) ;
416499
417500 return {
501+ browse,
418502 invalidate,
419503 search,
420504 } satisfies WorkspaceEntriesShape ;
0 commit comments