Skip to content

Commit

Permalink
add onlyFiles option
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperchupuDev committed Jul 24, 2024
1 parent 00ea3d9 commit c5976cc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ globSync({ patterns: ['src/*.ts', '!**/*.d.ts'] });
- `dot`: Whether to allow entries starting with a dot. Defaults to `false`.
- `deep`: Maximum depth of a directory. Defaults to `Infinity`.
- `expandDirectories`: Whether to expand directories. Disable to best match `fast-glob`. Defaults to `true`.
- `onlyDirectories`: Enable to only return directories. Defaults to `false`.
- `onlyDirectories`: Enable to only return directories. Disables `onlyFiles` if set. Defaults to `false`.
- `onlyFiles`: Enable to only return files. Defaults to `true`.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface GlobOptions {
deep?: number;
expandDirectories?: boolean;
onlyDirectories?: boolean;
onlyFiles?: boolean;
}

// using a directory as entry should match all files inside it
Expand Down Expand Up @@ -74,6 +75,8 @@ function getFdirBuilder(options: GlobOptions, cwd: string) {
if (options.onlyDirectories) {
fdirOptions.excludeFiles = true;
fdirOptions.includeDirs = true;
} else if (options.onlyFiles === false) {
fdirOptions.includeDirs = true;
}

return new fdir(fdirOptions);
Expand Down
10 changes: 10 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ test('onlyDirectories option', async () => {
assert.deepEqual(files.sort(), [`a${path.sep}`]);
});

test('onlyFiles option', async () => {
const files = await glob({ patterns: ['a'], onlyFiles: false, cwd });
assert.deepEqual(files.sort(), [`a${path.sep}`, path.join('a', 'a.ts'), path.join('a', 'b.ts')]);
});

test('onlyDirectories has preference over onlyFiles', async () => {
const files = await glob({ patterns: ['a'], onlyDirectories: true, onlyFiles: true, cwd });
assert.deepEqual(files.sort(), [`a${path.sep}`]);
});

test('bracket expanding', async () => {
const files = await glob({ patterns: ['a/{a,b}.ts'], cwd });
assert.deepEqual(files.sort(), [path.join('a', 'a.ts'), path.join('a', 'b.ts')]);
Expand Down

0 comments on commit c5976cc

Please sign in to comment.