@@ -2,9 +2,10 @@ import { glob } from 'glob';
22import { readdir , stat } from 'node:fs/promises' ;
33import { homedir } from 'node:os' ;
44import { basename } from 'node:path' ;
5- import { $ } from 'zurk' ;
5+ import { $ , quote } from 'zurk' ;
66import { FileSearch } from '../models/file-search.model' ;
77import { SearchOptions } from '../models/search-options.model' ;
8+ import { FILE_TYPE_TO_QUERY_STRING } from './search.config' ;
89
910export async function searchInFileSystem ( options : SearchOptions ) : Promise < FileSearch [ ] > {
1011 const {
@@ -34,20 +35,32 @@ export async function searchInFileSystem(options: SearchOptions): Promise<FileSe
3435 const filteredRootPaths = onlyIn ?. filter ( filterFunction ) ?? [ ] ;
3536
3637 /**
37- * -------------------------------
38+ * Handle search locations
3839 */
40+ const parsedOnlyIn = ( onlyIn ?? [ ] ) . map ( ( path ) => path . replace ( '~' , homedir ( ) ) ) ;
41+ const onlyInFlags = parsedOnlyIn . map ( ( path ) => `-onlyin '${ path } '` ) ;
42+ const onlyInLocationsQuery = onlyInFlags . join ( ' ' ) ;
3943
40- const kind = type === 'file' ? 'kind:file' : type === 'folder' ? 'kind:folder' : '' ;
41- const onlyInStr = ( onlyIn ?? [ ] ) ?. map ( ( path ) => `-onlyin ${ path } ` ) . map ( ( path ) => path . replace ( '~' , homedir ( ) ) ) ;
44+ /**
45+ * Handle file types
46+ */
47+ const fileTypeQuery = FILE_TYPE_TO_QUERY_STRING [ type ] ;
4248
49+ const nameFilterQuery = `'kMDItemFSName == "*${ quote ( name ) } *" ${ fileTypeQuery ? `&& ${ fileTypeQuery } ` : `` } '` ;
4350 const { stdout } = await $ ( {
44- args : onlyInStr ,
4551 spawnOpts : { maxBuffer : 10_000_000 } ,
46- } ) `mdfind ${ kind } -name "${ name } "` ;
52+ args : [ onlyInLocationsQuery , nameFilterQuery ] ,
53+ } ) `mdfind` ;
4754
55+ /**
56+ * Parse the output of mdfind command
57+ */
4858 const parsedQueryRes = stdout . split ( '\n' ) . filter ( Boolean ) ;
4959 const uniqueParsedQueryRes = Array . from ( new Set ( [ ...filteredForced , ...filteredRootPaths , ...parsedQueryRes ] ) ) ;
5060
61+ /**
62+ * Apply smart filtering
63+ */
5164 const filtered = uniqueParsedQueryRes . filter ( ( path ) => {
5265 /**
5366 * Force include the path even if it's bypassing exclude settings
0 commit comments