From 6b4497b61bd2ead9819e39a3277f520392efd38f Mon Sep 17 00:00:00 2001 From: Superchupu <53496941+SuperchupuDev@users.noreply.github.com> Date: Tue, 6 Aug 2024 14:00:44 +0200 Subject: [PATCH] fix matching only a directory without `expandDirectories` --- src/index.ts | 10 ++++++++-- test/index.test.ts | 5 +++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3ce365c..c8b8ea2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); @@ -60,8 +66,8 @@ function getFdirBuilder(options: GlobOptions, cwd: string) { const fdirOptions: Partial = { // 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 }; diff --git a/test/index.test.ts b/test/index.test.ts index 587271c..0877c25 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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']);