Skip to content

Commit

Permalink
add dot option
Browse files Browse the repository at this point in the history
closes #5
  • Loading branch information
SuperchupuDev committed Jul 24, 2024
1 parent b49622b commit 952953d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ globSync({ patterns: ['src/*.ts', '!**/*.d.ts'] });

## Options

- `patterns`: An array of glob patterns to search for. If not present returns every file in the cwd.
- `patterns`: An array of glob patterns to search for. Defaults to `['**/*']`.
- `ignore`: An array of glob patterns to ignore.
- `cwd`: The current working directory in which to search. Defaults to `process.cwd()`.
- `absolute`: Whether to return absolute paths. Defaults to `false`.
- `dot`: Whether to allow entries starting with a dot. 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`.
28 changes: 14 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'node:path';
import { type Options as FdirOptions, fdir } from 'fdir';
import picomatch from 'picomatch';

Expand All @@ -6,6 +7,7 @@ export interface GlobOptions {
cwd?: string;
patterns?: string[];
ignore?: string[];
dot?: boolean;
expandDirectories?: boolean;
onlyDirectories?: boolean;
}
Expand Down Expand Up @@ -44,17 +46,17 @@ function processPatterns({ patterns, ignore = [], expandDirectories = true }: Gl
return { match: matchPatterns, ignore: ignorePatterns };
}

function getFdirBuilder(options: GlobOptions) {
function getFdirBuilder(options: GlobOptions, cwd: string) {
const processed = processPatterns(options);

const fdirOptions: Partial<FdirOptions> = {
filters: [
picomatch(processed.match, {
dot: true,
ignore: processed.ignore
})
],
const matcher = picomatch(processed.match, {
dot: options.dot,
ignore: processed.ignore
});

const fdirOptions: Partial<FdirOptions> = {
// use relative paths in the matcher
filters: [options.absolute ? p => matcher(p.slice(cwd.length + 1)) : matcher],
relativePaths: true
};

Expand All @@ -73,13 +75,11 @@ function getFdirBuilder(options: GlobOptions) {
}

export async function glob(options: GlobOptions | undefined = {}): Promise<string[]> {
return getFdirBuilder(options)
.crawl(options.cwd ?? process.cwd())
.withPromise();
const cwd = options.cwd ? path.resolve(options.cwd) : process.cwd();
return getFdirBuilder(options, cwd).crawl(cwd).withPromise();
}

export function globSync(options: GlobOptions | undefined = {}): string[] {
return getFdirBuilder(options)
.crawl(options.cwd ?? process.cwd())
.sync();
const cwd = options.cwd ? path.resolve(options.cwd) : process.cwd();
return getFdirBuilder(options, cwd).crawl(cwd).sync();
}
1 change: 1 addition & 0 deletions test/fixtures/.a/a/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const h = 'h';
20 changes: 20 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ test('bracket expanding', async () => {
assert.deepEqual(files.sort(), [path.join('a', 'a.ts'), path.join('a', 'b.ts')]);
});

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

test('absolute + dot', async () => {
const files = await glob({ patterns: ['a/a.ts'], dot: true, cwd: path.join(cwd, '.a'), absolute: true });
assert.equal(files[0].slice(path.join(cwd, '.a').length + 1), path.join('a', 'a.ts'));
});

test('absolute', async () => {
const files = await glob({ patterns: ['a/a.ts'], dot: true, cwd: path.join(cwd, '.a'), absolute: true });
assert.equal(files[0].slice(path.join(cwd, '.a').length + 1), path.join('a', 'a.ts'));
});

test('works with non-absolute cwd', async () => {
const files = await glob({ patterns: ['a/*.ts'], cwd: 'test/fixtures' });
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 });
assert.deepEqual(files.sort(), [
Expand Down

0 comments on commit 952953d

Please sign in to comment.