From c5976cc91d084753dca8f7d97f2e3b409af06d19 Mon Sep 17 00:00:00 2001 From: Superchupu <53496941+SuperchupuDev@users.noreply.github.com> Date: Wed, 24 Jul 2024 13:42:56 +0100 Subject: [PATCH] add `onlyFiles` option --- README.md | 3 ++- src/index.ts | 3 +++ test/index.test.ts | 10 ++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 42ef755..2349e44 100644 --- a/README.md +++ b/README.md @@ -32,4 +32,5 @@ globSync({ patterns: ['src/*.ts', '!**/*.d.ts'] }); - `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`. +- `onlyDirectories`: Enable to only return directories. Disables `onlyFiles` if set. Defaults to `false`. +- `onlyFiles`: Enable to only return files. Defaults to `true`. diff --git a/src/index.ts b/src/index.ts index 3f21899..524c8b8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,7 @@ export interface GlobOptions { deep?: number; expandDirectories?: boolean; onlyDirectories?: boolean; + onlyFiles?: boolean; } // using a directory as entry should match all files inside it @@ -74,6 +75,8 @@ function getFdirBuilder(options: GlobOptions, cwd: string) { if (options.onlyDirectories) { fdirOptions.excludeFiles = true; fdirOptions.includeDirs = true; + } else if (options.onlyFiles === false) { + fdirOptions.includeDirs = true; } return new fdir(fdirOptions); diff --git a/test/index.test.ts b/test/index.test.ts index 7445aae..09c1594 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -30,6 +30,16 @@ test('onlyDirectories option', async () => { assert.deepEqual(files.sort(), [`a${path.sep}`]); }); +test('onlyFiles option', async () => { + const files = await glob({ patterns: ['a'], onlyFiles: false, cwd }); + assert.deepEqual(files.sort(), [`a${path.sep}`, path.join('a', 'a.ts'), path.join('a', 'b.ts')]); +}); + +test('onlyDirectories has preference over onlyFiles', async () => { + const files = await glob({ patterns: ['a'], onlyDirectories: true, onlyFiles: true, cwd }); + assert.deepEqual(files.sort(), [`a${path.sep}`]); +}); + test('bracket expanding', async () => { const files = await glob({ patterns: ['a/{a,b}.ts'], cwd }); assert.deepEqual(files.sort(), [path.join('a', 'a.ts'), path.join('a', 'b.ts')]);