Skip to content

Commit

Permalink
add deep option
Browse files Browse the repository at this point in the history
closes #2
  • Loading branch information
SuperchupuDev committed Jul 24, 2024
1 parent 952953d commit 00ea3d9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ 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`.
- `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`.
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface GlobOptions {
patterns?: string[];
ignore?: string[];
dot?: boolean;
deep?: number;
expandDirectories?: boolean;
onlyDirectories?: boolean;
}
Expand Down Expand Up @@ -60,6 +61,10 @@ function getFdirBuilder(options: GlobOptions, cwd: string) {
relativePaths: true
};

if (options.deep) {
fdirOptions.maxDepth = options.deep;
}

if (options.absolute) {
fdirOptions.relativePaths = false;
fdirOptions.resolvePaths = true;
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/.deep/a/a/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const h = 'h';
12 changes: 10 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,22 @@ test('dot', async () => {
assert.deepEqual(files.sort(), [path.join('a', 'a.ts')]);
});

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

const files2 = await glob({ patterns: ['.deep/a/a/*.ts'], deep: 2, cwd });
assert.deepEqual(files2.sort(), []);
});

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'));
assert.deepEqual(files.sort(), [path.resolve(cwd, '.a', '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'));
assert.deepEqual(files.sort(), [path.resolve(cwd, '.a', 'a', 'a.ts')]);
});

test('works with non-absolute cwd', async () => {
Expand Down

0 comments on commit 00ea3d9

Please sign in to comment.