Skip to content

Commit

Permalink
add "classic" way of using patterns as first argument
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperchupuDev committed Jul 24, 2024
1 parent c5976cc commit 609e2a7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ This library uses only two subdependencies, unlike globby's [24](https://npmgrap
## Usage

```js
import { glob } from 'tinyglobby';
import { glob, globSync } from 'tinyglobby';

await glob({ patterns: ['src/*.ts', '!**/*.d.ts'] });
```

```js
import { globSync } from 'tinyglobby';
await glob(['files/*.ts', '!**/*.d.ts'], { cwd: 'src' });
globSync(['src/**/*.ts'], { ignore: ['**/*.d.ts'] });

globSync({ patterns: ['src/*.ts', '!**/*.d.ts'] });
// you can also specify patterns inside the options object (exclusive to tinyglobby)
await glob({ patterns: ['src/*.ts', '!**/*.d.ts'], dot: true });
globSync({ patterns: ['src/**/*.ts', '!**/*.d.ts'], deep: 3 });
```

## Options
Expand Down
28 changes: 22 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,28 @@ function getFdirBuilder(options: GlobOptions, cwd: string) {
return new fdir(fdirOptions);
}

export async function glob(options: GlobOptions | undefined = {}): Promise<string[]> {
const cwd = options.cwd ? path.resolve(options.cwd) : process.cwd();
return getFdirBuilder(options, cwd).crawl(cwd).withPromise();
export function glob(patterns: string[], options?: Omit<GlobOptions, 'patterns'>): Promise<string[]>;
export function glob(options: GlobOptions): Promise<string[]>;
export async function glob(patternsOrOptions: string[] | GlobOptions, options?: GlobOptions): Promise<string[]> {
if (patternsOrOptions && options?.patterns) {
throw new Error('Cannot pass patterns as both an argument and an option');
}

const opts = Array.isArray(patternsOrOptions) ? { ...options, patterns: patternsOrOptions } : patternsOrOptions;
const cwd = opts.cwd ? path.resolve(opts.cwd) : process.cwd();

return getFdirBuilder(opts, cwd).crawl(cwd).withPromise();
}

export function globSync(options: GlobOptions | undefined = {}): string[] {
const cwd = options.cwd ? path.resolve(options.cwd) : process.cwd();
return getFdirBuilder(options, cwd).crawl(cwd).sync();
export function globSync(patterns: string[], options?: Omit<GlobOptions, 'patterns'>): string[];
export function globSync(options: GlobOptions): string[];
export function globSync(patternsOrOptions: string[] | GlobOptions, options?: GlobOptions): string[] {
if (patternsOrOptions && options?.patterns) {
throw new Error('Cannot pass patterns as both an argument and an option');
}

const opts = Array.isArray(patternsOrOptions) ? { ...options, patterns: patternsOrOptions } : patternsOrOptions;
const cwd = opts.cwd ? path.resolve(opts.cwd) : process.cwd();

return getFdirBuilder(opts, cwd).crawl(cwd).sync();
}
10 changes: 10 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ test('no directory expansion if expandDirectories is set to false', async () =>
assert.deepEqual(files.sort(), []);
});

test('classic patterns as first argument', async () => {
const files = await glob(['a/*.ts'], { cwd });
assert.deepEqual(files.sort(), [path.join('a', 'a.ts'), path.join('a', 'b.ts')]);
});

test("cant have both classic patterns and options' patterns", async () => {
// @ts-expect-error
assert.rejects(glob(['a/*.ts'], { patterns: ['whoops!'], cwd }));
});

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

0 comments on commit 609e2a7

Please sign in to comment.