Skip to content

Commit

Permalink
add expandDirectories option
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperchupuDev committed Jul 22, 2024
1 parent 6f27832 commit 3fe5a2b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ globSync({ patterns: ['src/*.ts', '!**/*.d.ts'] });
- `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`.
- `expandDirectories`: Whether to expand directories. Disable to best match `fast-glob`. Defaults to `true`.
48 changes: 27 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
import { fdir } from 'fdir';
import picomatch from 'picomatch';

function processPatterns(patterns?: string[], ignore?: string[]) {
export interface GlobOptions {
absolute?: boolean;
cwd?: string;
patterns?: string[];
ignore?: string[];
expandDirectories?: boolean;
}

// using a directory as entry should match all files inside it
function expandDir(pattern: string) {
if (pattern.endsWith('/')) {
return `${pattern}**`;
}
if (pattern.endsWith('\\')) {
return `${pattern.slice(0, -1)}/**`;
}
return `${pattern}/**`;
}

function processPatterns({ patterns, ignore = [], expandDirectories = true }: GlobOptions) {
if (!patterns) {
return null;
}
const matchPatterns: string[] = [];
const ignorePatterns: string[] = ignore ?? [];
const ignorePatterns: string[] = ignore.map(p => (!p.endsWith('*') && expandDirectories ? expandDir(p) : p));
for (let pattern of patterns) {
// using a directory as entry should match all files inside it
if (!pattern.endsWith('*')) {
if (pattern.endsWith('/')) {
pattern += '**';
} else if (pattern.endsWith('\\')) {
pattern = `${pattern.slice(0, -1)}/**`;
} else {
pattern += '/**';
}
if (!pattern.endsWith('*') && expandDirectories) {
pattern = expandDir(pattern);
}
if (pattern.startsWith('!') && pattern[1] !== '(') {
ignorePatterns.push(pattern.slice(1));
Expand All @@ -28,17 +41,10 @@ function processPatterns(patterns?: string[], ignore?: string[]) {
return { match: matchPatterns, ignore: ignorePatterns };
}

export interface GlobOptions {
absolute?: boolean;
cwd?: string;
patterns?: string[];
ignore?: string[];
}

function getFdirBuilder({ absolute = false, ignore, patterns }: GlobOptions | undefined = {}) {
const processed = processPatterns(patterns, ignore);
function getFdirBuilder(options: GlobOptions) {
const processed = processPatterns(options);

const options = processed
const fdirOptions = processed
? {
filters: [
picomatch(processed.match, {
Expand All @@ -50,7 +56,7 @@ function getFdirBuilder({ absolute = false, ignore, patterns }: GlobOptions | un
}
: undefined;

return absolute ? new fdir(options).withFullPaths() : new fdir(options).withRelativePaths();
return options.absolute ? new fdir(fdirOptions).withFullPaths() : new fdir(fdirOptions).withRelativePaths();
}

export async function glob(options: GlobOptions | undefined = {}): Promise<string[]> {
Expand Down
7 changes: 6 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import path from 'node:path';
import { test } from 'node:test';
import { glob, globSync } from '../src';

test('path expansion', async () => {
test('directory expansion', async () => {
const files = await glob({ patterns: ['a'], cwd: path.join(__dirname, 'fixtures') });
assert.deepEqual(files.sort(), [path.join('a', 'a.ts'), path.join('a', 'b.ts')]);
});

test('no directory expansion if expandDirectories is set to false', async () => {
const files = await glob({ patterns: ['a'], expandDirectories: false, cwd: path.join(__dirname, 'fixtures') });
assert.deepEqual(files.sort(), []);
});

test('negative patterns', async () => {
const files = await glob({ patterns: ['**/a.ts', '!b/a.ts'], cwd: path.join(__dirname, 'fixtures') });
assert.deepEqual(files.sort(), [path.join('a', 'a.ts')]);
Expand Down

0 comments on commit 3fe5a2b

Please sign in to comment.