Skip to content

Commit

Permalink
add onlyDirectories option
Browse files Browse the repository at this point in the history
closes #1
  • Loading branch information
SuperchupuDev committed Jul 22, 2024
1 parent e8dd9c6 commit ef284a1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ globSync({ patterns: ['src/*.ts', '!**/*.d.ts'] });
- `cwd`: The current working directory in which to search. Defaults to `process.cwd()`.
- `absolute`: Whether to return absolute paths. Defaults to `false`.
- `expandDirectories`: Whether to expand directories. Disable to best match `fast-glob`. Defaults to `true`.
- `onlyDirectories`: Enable to only return directories. Defaults to `false`.
15 changes: 14 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface GlobOptions {
patterns?: string[];
ignore?: string[];
expandDirectories?: boolean;
onlyDirectories?: boolean;
}

// using a directory as entry should match all files inside it
Expand Down Expand Up @@ -56,7 +57,19 @@ function getFdirBuilder(options: GlobOptions) {
}
: undefined;

return options.absolute ? new fdir(fdirOptions).withFullPaths() : new fdir(fdirOptions).withRelativePaths();
let builder = new fdir(fdirOptions);

if (options.absolute) {
builder = builder.withFullPaths();
} else {
builder = builder.withRelativePaths();
}

if (options.onlyDirectories) {
builder = builder.onlyDirs();
}

return builder;
}

export async function glob(options: GlobOptions | undefined = {}): Promise<string[]> {
Expand Down
25 changes: 16 additions & 9 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
import assert from 'node:assert/strict';
import path from 'node:path';
import path, { posix } from 'node:path';
import { test } from 'node:test';
import { glob, globSync } from '../src';

const cwd = path.join(__dirname, 'fixtures');

test('directory expansion', async () => {
const files = await glob({ patterns: ['a'], cwd: path.join(__dirname, 'fixtures') });
const files = await glob({ patterns: ['a'], cwd });
assert.deepEqual(files.sort(), [path.join('a', 'a.ts'), path.join('a', 'b.ts')]);
});

test('no directory expansion if expandDirectories is set to false', async () => {
const files = await glob({ patterns: ['a'], expandDirectories: false, cwd: path.join(__dirname, 'fixtures') });
const files = await glob({ patterns: ['a'], expandDirectories: false, cwd });
assert.deepEqual(files.sort(), []);
});

test('negative patterns', async () => {
const files = await glob({ patterns: ['**/a.ts', '!b/a.ts'], cwd: path.join(__dirname, 'fixtures') });
const files = await glob({ patterns: ['**/a.ts', '!b/a.ts'], cwd });
assert.deepEqual(files.sort(), [path.join('a', 'a.ts')]);
});

test('ignore option', async () => {
const files = await glob({ patterns: ['**/a.ts'], ignore: ['b/a.ts'], cwd: path.join(__dirname, 'fixtures') });
const files = await glob({ patterns: ['**/a.ts'], ignore: ['b/a.ts'], cwd });
assert.deepEqual(files.sort(), [path.join('a', 'a.ts')]);
});

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

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

test('no patterns returns everything in cwd', async () => {
const files = await glob({ cwd: path.join(__dirname, 'fixtures') });
const files = await glob({ cwd });
assert.deepEqual(files.sort(), [
path.join('a', 'a.ts'),
path.join('a', 'b.ts'),
Expand All @@ -39,7 +46,7 @@ test('no patterns returns everything in cwd', async () => {
});

test('**/* works', async () => {
const files = await glob({ patterns: ['**/*'], cwd: path.join(__dirname, 'fixtures') });
const files = await glob({ patterns: ['**/*'], cwd });
assert.deepEqual(files.sort(), [
path.join('a', 'a.ts'),
path.join('a', 'b.ts'),
Expand All @@ -49,6 +56,6 @@ test('**/* works', async () => {
});

test('sync version', () => {
const files = globSync({ patterns: ['a/*.ts'], cwd: path.join(__dirname, 'fixtures') });
const files = globSync({ patterns: ['a/*.ts'], cwd });
assert.deepEqual(files.sort(), [path.join('a', 'a.ts'), path.join('a', 'b.ts')]);
});

0 comments on commit ef284a1

Please sign in to comment.