diff --git a/packages/it-glob/README.md b/packages/it-glob/README.md index 873304c9..a658a32e 100644 --- a/packages/it-glob/README.md +++ b/packages/it-glob/README.md @@ -7,6 +7,21 @@ # About + + Like [`glob`](https://npmjs.com/package/glob) but async iterable. ## Example @@ -14,20 +29,15 @@ Like [`glob`](https://npmjs.com/package/glob) but async iterable. ```javascript import glob from 'it-glob' -const options = { - cwd // defaults to process.cwd - absolute // return absolute paths, defaults to false - nodir // only yield file paths, skip directories - - // all other options are passed to minimatch -} +// All options are passed through to fast-glob +const options = {} for await (const path of glob('/path/to/file', '**/*', options)) { console.info(path) } ``` -See the [minimatch docs](https://www.npmjs.com/package/minimatch#options) for the full list of options. +See the [fast-glob docs](https://github.com/mrmlnc/fast-glob#options-3) for the full list of options. # Install diff --git a/packages/it-glob/package.json b/packages/it-glob/package.json index e195b445..5ec274f1 100644 --- a/packages/it-glob/package.json +++ b/packages/it-glob/package.json @@ -38,21 +38,17 @@ } }, "scripts": { - "build": "aegir build", + "build": "aegir build --bundle false", "lint": "aegir lint", "dep-check": "aegir dep-check", "test": "aegir test -t node", "test:node": "aegir test -t node --cov" }, "dependencies": { - "minimatch": "^9.0.4" + "fast-glob": "^3.3.2" }, "devDependencies": { "aegir": "^42.2.5", "it-all": "^3.0.0" - }, - "browser": { - "fs/promises": false, - "path": false } } diff --git a/packages/it-glob/src/index.ts b/packages/it-glob/src/index.ts index 9a06011a..e67c1e0d 100644 --- a/packages/it-glob/src/index.ts +++ b/packages/it-glob/src/index.ts @@ -8,85 +8,33 @@ * ```javascript * import glob from 'it-glob' * - * const options = { - * cwd // defaults to process.cwd - * absolute // return absolute paths, defaults to false - * nodir // only yield file paths, skip directories - * - * // all other options are passed to minimatch - * } + * // All options are passed through to fast-glob + * const options = {} * * for await (const path of glob('/path/to/file', '**\/*', options)) { * console.info(path) * } * ``` * - * See the [minimatch docs](https://www.npmjs.com/package/minimatch#options) for the full list of options. + * See the [fast-glob docs](https://github.com/mrmlnc/fast-glob#options-3) for the full list of options. */ -import fs from 'fs/promises' -import path from 'path' -import { minimatch } from 'minimatch' -import type { MinimatchOptions } from 'minimatch' - -export interface GlobOptions extends MinimatchOptions { - /** - * The current working directory - */ - cwd?: string - - /** - * If true produces absolute paths (default: false) - */ - absolute?: boolean - - /** - * If true yields file paths and skip directories (default: false) - */ - nodir?: boolean -} +import fs from 'node:fs/promises' +import path from 'node:path' +import fastGlob from 'fast-glob' +import type { Options } from 'fast-glob' /** * Async iterable filename pattern matcher */ -export default async function * glob (dir: string, pattern: string, options: GlobOptions = {}): AsyncGenerator { +export default async function * glob (dir: string, pattern: string, options: Options = {}): AsyncGenerator { const absoluteDir = path.resolve(dir) - const relativeDir = path.relative(options.cwd ?? process.cwd(), dir) - const stats = await fs.stat(absoluteDir) - if (stats.isDirectory()) { - for await (const entry of _glob(absoluteDir, '', pattern, options)) { - yield entry - } - - return - } - - if (minimatch(relativeDir, pattern, options)) { - yield options.absolute === true ? absoluteDir : relativeDir - } -} - -async function * _glob (base: string, dir: string, pattern: string, options: GlobOptions): AsyncGenerator { - for await (const entry of await fs.opendir(path.join(base, dir))) { - const relativeEntryPath = path.join(dir, entry.name) - const absoluteEntryPath = path.join(base, dir, entry.name) - - let match = minimatch(relativeEntryPath, pattern, options) - - const isDirectory = entry.isDirectory() - - if (isDirectory && options.nodir === true) { - match = false - } - - if (match) { - yield options.absolute === true ? absoluteEntryPath : relativeEntryPath - } - - if (isDirectory) { - yield * _glob(base, relativeEntryPath, pattern, options) - } + for await (const entry of fastGlob.stream(pattern, { + ...options, + cwd: stats.isDirectory() ? dir : process.cwd() + })) { + yield entry.toString() } } diff --git a/packages/it-glob/test/index.spec.ts b/packages/it-glob/test/index.spec.ts index 0583947d..18cf5deb 100644 --- a/packages/it-glob/test/index.spec.ts +++ b/packages/it-glob/test/index.spec.ts @@ -64,14 +64,16 @@ describe('it-glob', () => { }) it('should match directories', async () => { - const files = await all(glob(path.resolve(dir, '..', '..'), 'dist/*')) + const files = await all(glob(path.resolve(dir, '..', '..'), 'dist/*', { + onlyFiles: false + })) expect(files.includes(path.join('dist', 'src'))).to.be.true() }) it('should skip directories', async () => { const files = await all(glob(path.resolve(dir, '..', '..'), 'dist/**/*', { - nodir: true, + onlyFiles: true, dot: true }))