@@ -5,7 +5,6 @@ import { AppFileSystem } from "@/filesystem"
55import { git } from "@/util/git"
66import { Effect , Layer , ServiceMap } from "effect"
77import { formatPatch , structuredPatch } from "diff"
8- import fs from "fs"
98import fuzzysort from "fuzzysort"
109import ignore from "ignore"
1110import path from "path"
@@ -359,49 +358,46 @@ export namespace File {
359358 const isGlobalHome = Instance . directory === Global . Path . home && Instance . project . id === "global"
360359 const next : Entry = { files : [ ] , dirs : [ ] }
361360
362- yield * Effect . promise ( async ( ) => {
363- if ( isGlobalHome ) {
364- const dirs = new Set < string > ( )
365- const protectedNames = Protected . names ( )
366- const ignoreNested = new Set ( [ "node_modules" , "dist" , "build" , "target" , "vendor" ] )
367- const shouldIgnoreName = ( name : string ) => name . startsWith ( "." ) || protectedNames . has ( name )
368- const shouldIgnoreNested = ( name : string ) => name . startsWith ( "." ) || ignoreNested . has ( name )
369- const top = await fs . promises
370- . readdir ( Instance . directory , { withFileTypes : true } )
371- . catch ( ( ) => [ ] as fs . Dirent [ ] )
372-
373- for ( const entry of top ) {
374- if ( ! entry . isDirectory ( ) ) continue
375- if ( shouldIgnoreName ( entry . name ) ) continue
376- dirs . add ( entry . name + "/" )
377-
378- const base = path . join ( Instance . directory , entry . name )
379- const children = await fs . promises . readdir ( base , { withFileTypes : true } ) . catch ( ( ) => [ ] as fs . Dirent [ ] )
380- for ( const child of children ) {
381- if ( ! child . isDirectory ( ) ) continue
382- if ( shouldIgnoreNested ( child . name ) ) continue
383- dirs . add ( entry . name + "/" + child . name + "/" )
384- }
361+ if ( isGlobalHome ) {
362+ const dirs = new Set < string > ( )
363+ const protectedNames = Protected . names ( )
364+ const ignoreNested = new Set ( [ "node_modules" , "dist" , "build" , "target" , "vendor" ] )
365+ const shouldIgnoreName = ( name : string ) => name . startsWith ( "." ) || protectedNames . has ( name )
366+ const shouldIgnoreNested = ( name : string ) => name . startsWith ( "." ) || ignoreNested . has ( name )
367+ const top = yield * appFs . readDirectoryEntries ( Instance . directory ) . pipe ( Effect . orElseSucceed ( ( ) => [ ] ) )
368+
369+ for ( const entry of top ) {
370+ if ( entry . type !== "directory" ) continue
371+ if ( shouldIgnoreName ( entry . name ) ) continue
372+ dirs . add ( entry . name + "/" )
373+
374+ const base = path . join ( Instance . directory , entry . name )
375+ const children = yield * appFs . readDirectoryEntries ( base ) . pipe ( Effect . orElseSucceed ( ( ) => [ ] ) )
376+ for ( const child of children ) {
377+ if ( child . type !== "directory" ) continue
378+ if ( shouldIgnoreNested ( child . name ) ) continue
379+ dirs . add ( entry . name + "/" + child . name + "/" )
385380 }
381+ }
386382
387- next . dirs = Array . from ( dirs ) . toSorted ( )
388- } else {
389- const seen = new Set < string > ( )
390- for await ( const file of Ripgrep . files ( { cwd : Instance . directory } ) ) {
391- next . files . push ( file )
392- let current = file
393- while ( true ) {
394- const dir = path . dirname ( current )
395- if ( dir === "." ) break
396- if ( dir === current ) break
397- current = dir
398- if ( seen . has ( dir ) ) continue
399- seen . add ( dir )
400- next . dirs . push ( dir + "/" )
401- }
383+ next . dirs = Array . from ( dirs ) . toSorted ( )
384+ } else {
385+ const files = yield * Effect . promise ( ( ) => Array . fromAsync ( Ripgrep . files ( { cwd : Instance . directory } ) ) )
386+ const seen = new Set < string > ( )
387+ for ( const file of files ) {
388+ next . files . push ( file )
389+ let current = file
390+ while ( true ) {
391+ const dir = path . dirname ( current )
392+ if ( dir === "." ) break
393+ if ( dir === current ) break
394+ current = dir
395+ if ( seen . has ( dir ) ) continue
396+ seen . add ( dir )
397+ next . dirs . push ( dir + "/" )
402398 }
403399 }
404- } )
400+ }
405401
406402 const s = yield * InstanceState . get ( state )
407403 s . cache = next
@@ -636,30 +632,27 @@ export namespace File {
636632 yield * ensure ( )
637633 const { cache } = yield * InstanceState . get ( state )
638634
639- return yield * Effect . promise ( async ( ) => {
640- const query = input . query . trim ( )
641- const limit = input . limit ?? 100
642- const kind = input . type ?? ( input . dirs === false ? "file" : "all" )
643- log . info ( "search" , { query, kind } )
635+ const query = input . query . trim ( )
636+ const limit = input . limit ?? 100
637+ const kind = input . type ?? ( input . dirs === false ? "file" : "all" )
638+ log . info ( "search" , { query, kind } )
644639
645- const result = cache
646- const preferHidden = query . startsWith ( "." ) || query . includes ( "/." )
640+ const preferHidden = query . startsWith ( "." ) || query . includes ( "/." )
647641
648- if ( ! query ) {
649- if ( kind === "file" ) return result . files . slice ( 0 , limit )
650- return sortHiddenLast ( result . dirs . toSorted ( ) , preferHidden ) . slice ( 0 , limit )
651- }
642+ if ( ! query ) {
643+ if ( kind === "file" ) return cache . files . slice ( 0 , limit )
644+ return sortHiddenLast ( cache . dirs . toSorted ( ) , preferHidden ) . slice ( 0 , limit )
645+ }
652646
653- const items =
654- kind === "file" ? result . files : kind === "directory" ? result . dirs : [ ...result . files , ...result . dirs ]
647+ const items =
648+ kind === "file" ? cache . files : kind === "directory" ? cache . dirs : [ ...cache . files , ...cache . dirs ]
655649
656- const searchLimit = kind === "directory" && ! preferHidden ? limit * 20 : limit
657- const sorted = fuzzysort . go ( query , items , { limit : searchLimit } ) . map ( ( item ) => item . target )
658- const output = kind === "directory" ? sortHiddenLast ( sorted , preferHidden ) . slice ( 0 , limit ) : sorted
650+ const searchLimit = kind === "directory" && ! preferHidden ? limit * 20 : limit
651+ const sorted = fuzzysort . go ( query , items , { limit : searchLimit } ) . map ( ( item ) => item . target )
652+ const output = kind === "directory" ? sortHiddenLast ( sorted , preferHidden ) . slice ( 0 , limit ) : sorted
659653
660- log . info ( "search" , { query, kind, results : output . length } )
661- return output
662- } )
654+ log . info ( "search" , { query, kind, results : output . length } )
655+ return output
663656 } )
664657
665658 log . info ( "init" )
0 commit comments