Skip to content

Commit

Permalink
fix matching only a directory without expandDirectories
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperchupuDev committed Aug 6, 2024
1 parent f67f173 commit 6b4497b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ function processPatterns({ patterns, ignore = [], expandDirectories = true }: Gl
return { match: matchPatterns, ignore: ignorePatterns };
}

function processPath(path: string, cwd: string, absolute?: boolean) {
const pathWithoutTrailingSlash = path.endsWith('/') ? path.slice(0, -1) : path;

return absolute ? pathWithoutTrailingSlash.slice(cwd.length + 1) : pathWithoutTrailingSlash;
}

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

Expand All @@ -60,8 +66,8 @@ function getFdirBuilder(options: GlobOptions, cwd: string) {

const fdirOptions: Partial<FdirOptions> = {
// use relative paths in the matcher
filters: [p => matcher(options.absolute ? p.slice(cwd.length + 1) : p)],
exclude: (_, p) => exclude(p.slice(cwd.length + 1)),
filters: [p => matcher(processPath(p, cwd, options.absolute))],
exclude: (_, p) => exclude(p.slice(cwd.length + 1).slice(0, -1)),
pathSeparator: '/',
relativePaths: true
};
Expand Down
5 changes: 5 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ test('onlyDirectories has preference over onlyFiles', async () => {
assert.deepEqual(files.sort(), ['a/']);
});

test('matching only a directory works', async () => {
const files = await glob({ patterns: ['a'], onlyFiles: false, expandDirectories: false, cwd });
assert.deepEqual(files.sort(), ['a/']);
});

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

0 comments on commit 6b4497b

Please sign in to comment.