Skip to content

Commit

Permalink
feat: exclude only directories with enabled the onlyFiles option
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmlnc committed Jul 2, 2024
1 parent da64807 commit aeccecf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ fg.globSync('*', { onlyDirectories: true }); // ['src']
* Type: `boolean`
* Default: `true`

Return only files.
Return everything (file, socket, …) except directories.

```js
fg.globSync('*', { onlyFiles: false }); // ['index.js', 'src']
Expand Down
15 changes: 15 additions & 0 deletions src/providers/filters/entry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface FilterOptions {
}

const FILE_ENTRY = tests.entry.builder().path('root/file.txt').file().build();
const SOCKET_ENTRY = tests.entry.builder().path('/tmp/test.sock').socket().build();
const DIRECTORY_ENTRY = tests.entry.builder().path('root/directory').directory().build();

function getEntryFilterInstance(options?: Options): EntryFilter {
Expand Down Expand Up @@ -135,6 +136,13 @@ describe('Providers → Filters → Entry', () => {
options: { onlyFiles: true },
});
});

it('should accept a socket entry', () => {
accept(SOCKET_ENTRY, {
positive: ['**/*'],
options: { onlyFiles: true },
});
});
});

describe('options.onlyDirectories', () => {
Expand All @@ -145,6 +153,13 @@ describe('Providers → Filters → Entry', () => {
});
});

it('should reject a socket entry', () => {
reject(SOCKET_ENTRY, {
positive: ['**/*'],
options: { onlyDirectories: true },
});
});

it('should accept a directory entry', () => {
accept(DIRECTORY_ENTRY, {
positive: ['**/*'],
Expand Down
14 changes: 8 additions & 6 deletions src/providers/filters/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ export default class EntryFilter {
return false;
}

if (this.#onlyFileFilter(entry) || this.#onlyDirectoryFilter(entry)) {
const isDirectory = entry.dirent.isDirectory();

if (this.#onlyFileFilter(isDirectory) || this.#onlyDirectoryFilter(isDirectory)) {
return false;
}

const isMatched = this.#isMatchToPatternsSet(filepath, pattens, entry.dirent.isDirectory());
const isMatched = this.#isMatchToPatternsSet(filepath, pattens, isDirectory);

if (this.#settings.unique && isMatched) {
this.#createIndexRecord(filepath);
Expand All @@ -68,12 +70,12 @@ export default class EntryFilter {
this.index.set(filepath, undefined);
}

#onlyFileFilter(entry: Entry): boolean {
return this.#settings.onlyFiles && !entry.dirent.isFile();
#onlyFileFilter(isDirectory: boolean): boolean {
return this.#settings.onlyFiles && isDirectory;
}

#onlyDirectoryFilter(entry: Entry): boolean {
return this.#settings.onlyDirectories && !entry.dirent.isDirectory();
#onlyDirectoryFilter(isDirectory: boolean): boolean {
return this.#settings.onlyDirectories && !isDirectory;
}

#isMatchToPatternsSet(filepath: string, patterns: PatternsRegexSet, isDirectory: boolean): boolean {
Expand Down
6 changes: 6 additions & 0 deletions src/tests/utils/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class EntryBuilder {
return this;
}

public socket(): this {
this.#entryType = DirentType.Socket;

return this;
}

public stats(): this {
this.#entry.stats = new Stats();

Expand Down

0 comments on commit aeccecf

Please sign in to comment.