Skip to content

Commit cc40378

Browse files
authored
Merge pull request #450 from mrmlnc/ISSUE-438_only_files
ISSUE-438: Exclude only directories with enabled the `onlyFiles` option
2 parents 0a5731a + bcfd7bb commit cc40378

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ fg.globSync('*', { onlyDirectories: true }); // ['src']
472472
* Type: `boolean`
473473
* Default: `true`
474474

475-
Return only files.
475+
Return everything (file, socket, …) except directories.
476476

477477
```js
478478
fg.globSync('*', { onlyFiles: false }); // ['index.js', 'src']

src/providers/filters/entry.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ interface FilterOptions {
1717
}
1818

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

2223
function getEntryFilterInstance(options?: Options): EntryFilter {
@@ -137,6 +138,13 @@ describe('Providers → Filters → Entry', () => {
137138
options: { onlyFiles: true },
138139
});
139140
});
141+
142+
it('should accept a socket entry', () => {
143+
accept(SOCKET_ENTRY, {
144+
positive: ['**/*'],
145+
options: { onlyFiles: true },
146+
});
147+
});
140148
});
141149

142150
describe('options.onlyDirectories', () => {
@@ -147,6 +155,13 @@ describe('Providers → Filters → Entry', () => {
147155
});
148156
});
149157

158+
it('should reject a socket entry', () => {
159+
reject(SOCKET_ENTRY, {
160+
positive: ['**/*'],
161+
options: { onlyDirectories: true },
162+
});
163+
});
164+
150165
it('should accept a directory entry', () => {
151166
accept(DIRECTORY_ENTRY, {
152167
positive: ['**/*'],

src/providers/filters/entry.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ export default class EntryFilter {
4747
return false;
4848
}
4949

50-
if (this.#onlyFileFilter(entry) || this.#onlyDirectoryFilter(entry)) {
50+
const isDirectory = entry.dirent.isDirectory();
51+
52+
if (this.#onlyFileFilter(isDirectory) || this.#onlyDirectoryFilter(isDirectory)) {
5153
return false;
5254
}
5355

54-
const isMatched = this.#isMatchToPatternsSet(filepath, pattens, entry.dirent.isDirectory());
56+
const isMatched = this.#isMatchToPatternsSet(filepath, pattens, isDirectory);
5557

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

71-
#onlyFileFilter(entry: Entry): boolean {
72-
return this.#settings.onlyFiles && !entry.dirent.isFile();
73+
#onlyFileFilter(isDirectory: boolean): boolean {
74+
return this.#settings.onlyFiles && isDirectory;
7375
}
7476

75-
#onlyDirectoryFilter(entry: Entry): boolean {
76-
return this.#settings.onlyDirectories && !entry.dirent.isDirectory();
77+
#onlyDirectoryFilter(isDirectory: boolean): boolean {
78+
return this.#settings.onlyDirectories && !isDirectory;
7779
}
7880

7981
#isMatchToPatternsSet(filepath: string, patterns: PatternsRegexSet, isDirectory: boolean): boolean {

src/tests/e2e/patterns/root.e2e.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,22 @@ function getRootEntries(root: string, withBase: boolean = false): string[] {
2222
function getRootEntriesWithFileTypes(root: string): string[] {
2323
return fs.readdirSync(root, { withFileTypes: true })
2424
.filter((item) => !item.name.startsWith('.'))
25-
.filter((item) => item.isFile())
25+
.filter((item) => !item.isDirectory())
2626
.map((item) => item.name);
2727
}
2828

2929
runner.suite('Patterns Root', {
3030
tests: [
3131
{
3232
pattern: '/*',
33+
options: { followSymbolicLinks: false },
3334
condition: () => !utils.platform.isWindows(),
3435
expected: () => getRootEntries(ROOT, /** withBase */ true),
3536
},
3637
{
37-
pattern: '/tmp/*',
38+
pattern: '/usr/*',
3839
condition: () => !utils.platform.isWindows(),
39-
expected: () => getRootEntries('/tmp', /** withBase */ true),
40+
expected: () => getRootEntries('/usr', /** withBase */ true),
4041
},
4142
{
4243
pattern: '/*',
@@ -58,6 +59,7 @@ runner.suite('Patterns Root (cwd)', {
5859
pattern: '*',
5960
options: {
6061
cwd: ROOT,
62+
followSymbolicLinks: false,
6163
},
6264
condition: () => !utils.platform.isWindows(),
6365
expected: () => getRootEntries(ROOT),

src/tests/utils/entry.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ class EntryBuilder {
3838
return this;
3939
}
4040

41+
public socket(): this {
42+
this.#entryType = DirentType.Socket;
43+
44+
return this;
45+
}
46+
4147
public stats(): this {
4248
this.#entry.stats = new Stats();
4349

0 commit comments

Comments
 (0)