Skip to content

Commit c8e7b35

Browse files
authored
Merge pull request #12 from Avivbens/feat/support-search-contains
2 parents 4de2ab6 + ac1d5a7 commit c8e7b35

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { SearchOptions } from '../models/search-options.model';
2+
3+
export const FILE_TYPE_TO_QUERY_STRING: Record<SearchOptions['type'], string> = {
4+
folder: 'kMDItemContentType == "public.folder"',
5+
file: 'kMDItemContentType != "public.folder"',
6+
all: '',
7+
};

projects/packages/advance-fs-search/src/services/search.service.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { glob } from 'glob';
22
import { readdir, stat } from 'node:fs/promises';
33
import { homedir } from 'node:os';
44
import { basename } from 'node:path';
5-
import { $ } from 'zurk';
5+
import { $, quote } from 'zurk';
66
import { FileSearch } from '../models/file-search.model';
77
import { SearchOptions } from '../models/search-options.model';
8+
import { FILE_TYPE_TO_QUERY_STRING } from './search.config';
89

910
export 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

Comments
 (0)