From bcfd7bb6c955c906e9b0c460ee64aa73a07c7ccb Mon Sep 17 00:00:00 2001 From: mrmlnc Date: Wed, 3 Jul 2024 00:35:52 +0300 Subject: [PATCH] feat: exclude only directories with enabled the `onlyFiles` option --- README.md | 2 +- src/providers/filters/entry.spec.ts | 15 +++++++++++++++ src/providers/filters/entry.ts | 14 ++++++++------ src/tests/e2e/patterns/root.e2e.ts | 8 +++++--- src/tests/utils/entry.ts | 6 ++++++ 5 files changed, 35 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 14855db4..b34874d4 100644 --- a/README.md +++ b/README.md @@ -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'] diff --git a/src/providers/filters/entry.spec.ts b/src/providers/filters/entry.spec.ts index c504a0e1..c7b6d99c 100644 --- a/src/providers/filters/entry.spec.ts +++ b/src/providers/filters/entry.spec.ts @@ -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 { @@ -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', () => { @@ -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: ['**/*'], diff --git a/src/providers/filters/entry.ts b/src/providers/filters/entry.ts index d596e0d8..d7b09f1c 100644 --- a/src/providers/filters/entry.ts +++ b/src/providers/filters/entry.ts @@ -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); @@ -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 { diff --git a/src/tests/e2e/patterns/root.e2e.ts b/src/tests/e2e/patterns/root.e2e.ts index c30200fa..82853214 100644 --- a/src/tests/e2e/patterns/root.e2e.ts +++ b/src/tests/e2e/patterns/root.e2e.ts @@ -22,7 +22,7 @@ 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); } @@ -30,13 +30,14 @@ 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: '/*', @@ -58,6 +59,7 @@ runner.suite('Patterns Root (cwd)', { pattern: '*', options: { cwd: ROOT, + followSymbolicLinks: false, }, condition: () => !utils.platform.isWindows(), expected: () => getRootEntries(ROOT), diff --git a/src/tests/utils/entry.ts b/src/tests/utils/entry.ts index 095fdbeb..57ca0855 100644 --- a/src/tests/utils/entry.ts +++ b/src/tests/utils/entry.ts @@ -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();