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 Nov 23, 2024
1 parent 3e7a83b commit bcfd7bb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 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 @@ -17,6 +17,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 @@ -137,6 +138,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 @@ -147,6 +155,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
8 changes: 5 additions & 3 deletions src/tests/e2e/patterns/root.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@ function getRootEntries(root: string, withBase: boolean = false): string[] {
function getRootEntriesWithFileTypes(root: string): string[] {
return fs.readdirSync(root, { withFileTypes: true })
.filter((item) => !item.name.startsWith('.'))
.filter((item) => item.isFile())
.filter((item) => !item.isDirectory())
.map((item) => item.name);
}

runner.suite('Patterns Root', {
tests: [
{
pattern: '/*',
options: { followSymbolicLinks: false },
condition: () => !utils.platform.isWindows(),
expected: () => getRootEntries(ROOT, /** withBase */ true),
},
{
pattern: '/tmp/*',
pattern: '/usr/*',
condition: () => !utils.platform.isWindows(),
expected: () => getRootEntries('/tmp', /** withBase */ true),
expected: () => getRootEntries('/usr', /** withBase */ true),
},
{
pattern: '/*',
Expand All @@ -58,6 +59,7 @@ runner.suite('Patterns Root (cwd)', {
pattern: '*',
options: {
cwd: ROOT,
followSymbolicLinks: false,
},
condition: () => !utils.platform.isWindows(),
expected: () => getRootEntries(ROOT),
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 bcfd7bb

Please sign in to comment.