diff --git a/bench.js b/bench.js index b404bf1..29b10d5 100644 --- a/bench.js +++ b/bench.js @@ -11,70 +11,84 @@ import {globby, globbySync} from './index.js'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const BENCH_DIR = 'bench'; -const runners = [{ - name: 'globby async (working directory)', - run: async (patterns, callback) => { - await globby(patterns); - callback(); +const runners = [ + { + name: 'globby async (working directory)', + run: async (patterns, callback) => { + await globby(patterns); + callback(); + }, }, -}, { - name: 'globby async (upstream/main)', - run: async (patterns, callback) => { - await globbyMainBranch(patterns); - callback(); + { + name: 'globby async (upstream/main)', + run: async (patterns, callback) => { + await globbyMainBranch(patterns); + callback(); + }, }, -}, { - name: 'globby sync (working directory)', - run: patterns => { - globbySync(patterns); + { + name: 'globby sync (working directory)', + run: patterns => { + globbySync(patterns); + }, }, -}, { - name: 'globby sync (upstream/main)', - run: patterns => { - globbyMainBranch.sync(patterns); + { + name: 'globby sync (upstream/main)', + run: patterns => { + globbyMainBranch.sync(patterns); + }, }, -}, { - name: 'glob-stream', - run: (patterns, cb) => { - gs(patterns).on('data', () => {}).on('end', cb); + { + name: 'glob-stream', + run: (patterns, cb) => { + gs(patterns).on('data', () => {}).on('end', cb); + }, }, -}, { - name: 'fast-glob async', - run: async (patterns, callback) => { - await fastGlob(patterns); - callback(); + { + name: 'fast-glob async', + run: async (patterns, callback) => { + await fastGlob(patterns); + callback(); + }, }, -}, { - name: 'fast-glob sync', - run: patterns => { - fastGlob.sync(patterns); + { + name: 'fast-glob sync', + run: patterns => { + fastGlob.sync(patterns); + }, + } +]; + +const benchs = [ + { + name: 'negative globs (some files inside dir)', + patterns: [ + 'a/*', + '!a/c*', + ], + }, + { + name: 'negative globs (whole dir)', + patterns: [ + 'a/*', + '!a/**', + ], }, -}]; -const benchs = [{ - name: 'negative globs (some files inside dir)', - patterns: [ - 'a/*', - '!a/c*', - ], -}, { - name: 'negative globs (whole dir)', - patterns: [ - 'a/*', - '!a/**', - ], -}, { - name: 'multiple positive globs', - patterns: [ - 'a/*', - 'b/*', - ], -}]; + { + name: 'multiple positive globs', + patterns: [ + 'a/*', + 'b/*', + ], + } +]; before(() => { process.chdir(__dirname); rimraf.sync(BENCH_DIR); fs.mkdirSync(BENCH_DIR); process.chdir(BENCH_DIR); + const directories = ['a', 'b'] .map(directory => `${directory}/`); diff --git a/gitignore.js b/gitignore.js index 03fbefa..8c524a8 100644 --- a/gitignore.js +++ b/gitignore.js @@ -113,4 +113,3 @@ export const isGitIgnoredSync = options => { return getIsIgnoredPredicate(ignores, options.cwd); }; - diff --git a/index.d.ts b/index.d.ts index 2811bbe..f8a012e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,178 +1,171 @@ -import {Options as FastGlobOptions, Entry as FastGlobEntry} from 'fast-glob'; +import {Options as FastGlobOptions, Entry} from 'fast-glob'; -type ExpandDirectoriesOption = +export type GlobEntry = Entry; + +export interface GlobTask { + readonly pattern: string; + readonly options: Options; +} + +export type ExpandDirectoriesOption = | boolean | readonly string[] | {files?: readonly string[]; extensions?: readonly string[]}; -type GlobbyEntry = FastGlobEntry; - -interface GlobbyOptions extends FastGlobOptions { +export interface Options extends FastGlobOptions { /** - If set to `true`, `globby` will automatically glob directories for you. If you define an `Array` it will only glob files that matches the patterns inside the `Array`. You can also define an `Object` with `files` and `extensions` like in the example below. + If set to `true`, `globby` will automatically glob directories for you. If you define an `Array` it will only glob files that matches the patterns inside the `Array`. You can also define an `Object` with `files` and `extensions` like in the example below. - Note that if you set this option to `false`, you won't get back matched directories unless you set `onlyFiles: false`. + Note that if you set this option to `false`, you won't get back matched directories unless you set `onlyFiles: false`. - @default true + @default true - @example - ``` - import {globby} from 'globby'; + @example + ``` + import {globby} from 'globby'; - (async () => { - const paths = await globby('images', { - expandDirectories: { - files: ['cat', 'unicorn', '*.jpg'], - extensions: ['png'] - } - }); + const paths = await globby('images', { + expandDirectories: { + files: ['cat', 'unicorn', '*.jpg'], + extensions: ['png'] + } + }); - console.log(paths); - //=> ['cat.png', 'unicorn.png', 'cow.jpg', 'rainbow.jpg'] - })(); - ``` - */ + console.log(paths); + //=> ['cat.png', 'unicorn.png', 'cow.jpg', 'rainbow.jpg'] + ``` + */ readonly expandDirectories?: ExpandDirectoriesOption; /** - Respect ignore patterns in `.gitignore` files that apply to the globbed files. + Respect ignore patterns in `.gitignore` files that apply to the globbed files. - @default false - */ + @default false + */ readonly gitignore?: boolean; } -interface GlobTask { - readonly pattern: string; - readonly options: GlobbyOptions; -} - -interface GitignoreOptions { +export interface GitignoreOptions { readonly cwd?: string; readonly ignore?: readonly string[]; } -type GlobbyFilterFunction = (path: string) => boolean; +export type GlobbyFilterFunction = (path: string) => boolean; /** - `.gitignore` files matched by the ignore config are not used for the resulting filter function. +Find files and directories using glob patterns. - @returns A filter function indicating whether a given path is ignored via a `.gitignore` file. +Note that glob patterns can only contain forward-slashes, not backward-slashes, so if you want to construct a glob pattern from path components, you need to use `path.posix.join()` instead of `path.join()`. - @example - ``` - import {isGitIgnored} from 'globby'; +@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns). +@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones in this package. +@returns The matching paths. - (async () => { - const isIgnored = await isGitIgnored(); - console.log(isIgnored('some/file')); - })(); - ``` - */ -declare const isGitIgnored: (options?: GitignoreOptions) => Promise; +@example +``` +import {globby} from 'globby'; -/** - @returns A filter function indicating whether a given path is ignored via a `.gitignore` file. - */ -declare const isGitIgnoredSync: (options?: GitignoreOptions) => GlobbyFilterFunction; +const paths = await globby(['*', '!cake']); + +console.log(paths); +//=> ['unicorn', 'rainbow'] +``` +*/ +export function globby( + patterns: string | readonly string[], + options: Options & {objectMode: true} +): Promise; +export function globby( + patterns: string | readonly string[], + options?: Options +): Promise; /** - Find files and directories using glob patterns. +Find files and directories using glob patterns. - Note that glob patterns can only contain forward-slashes, not backward-slashes, so if you want to construct a glob pattern from path components, you need to use `path.posix.join()` instead of `path.join()`. +Note that glob patterns can only contain forward-slashes, not backward-slashes, so if you want to construct a glob pattern from path components, you need to use `path.posix.join()` instead of `path.join()`. - @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns). - @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones in this package. - @returns The matching paths. - */ -declare const globbySync: (( +@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns). +@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones in this package. +@returns The matching paths. +*/ +export function globbySync( patterns: string | readonly string[], - options: GlobbyOptions & {objectMode: true} -) => GlobbyEntry[]) & (( + options: Options & {objectMode: true} +): GlobEntry[]; +export function globbySync( patterns: string | readonly string[], - options?: GlobbyOptions -) => string[]); + options?: Options +): string[]; /** - Find files and directories using glob patterns. +Find files and directories using glob patterns. - Note that glob patterns can only contain forward-slashes, not backward-slashes, so if you want to construct a glob pattern from path components, you need to use `path.posix.join()` instead of `path.join()`. +Note that glob patterns can only contain forward-slashes, not backward-slashes, so if you want to construct a glob pattern from path components, you need to use `path.posix.join()` instead of `path.join()`. - @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns). - @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones in this package. - @returns The stream of matching paths. +@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns). +@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones in this package. +@returns The stream of matching paths. - @example - ``` - import {globbyStream} from 'globby'; +@example +``` +import {globbyStream} from 'globby'; - (async () => { - for await (const path of globbyStream('*.tmp')) { - console.log(path); - } - })(); - ``` - */ -declare const globbyStream: ( +for await (const path of globbyStream('*.tmp')) { + console.log(path); +} +``` +*/ +export function globbyStream( patterns: string | readonly string[], - options?: GlobbyOptions -) => NodeJS.ReadableStream; + options?: Options +): NodeJS.ReadableStream; /** - Note that you should avoid running the same tasks multiple times as they contain a file system cache. Instead, run this method each time to ensure file system changes are taken into consideration. +Note that you should avoid running the same tasks multiple times as they contain a file system cache. Instead, run this method each time to ensure file system changes are taken into consideration. - @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns). - @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones in this package. - @returns An object in the format `{pattern: string, options: object}`, which can be passed as arguments to [`fast-glob`](https://github.com/mrmlnc/fast-glob). This is useful for other globbing-related packages. - */ -declare const generateGlobTasks: ( +@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns). +@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones in this package. +@returns An object in the format `{pattern: string, options: object}`, which can be passed as arguments to [`fast-glob`](https://github.com/mrmlnc/fast-glob). This is useful for other globbing-related packages. +*/ +export function generateGlobTasks( patterns: string | readonly string[], - options?: GlobbyOptions -) => GlobTask[]; + options?: Options +): GlobTask[]; /** - Note that the options affect the results. +Note that the options affect the results. - This function is backed by [`fast-glob`](https://github.com/mrmlnc/fast-glob#isdynamicpatternpattern-options). +This function is backed by [`fast-glob`](https://github.com/mrmlnc/fast-glob#isdynamicpatternpattern-options). - @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns). - @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3). - @returns Whether there are any special glob characters in the `patterns`. - */ -declare const isDynamicPattern: ( +@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns). +@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3). +@returns Whether there are any special glob characters in the `patterns`. +*/ +export function isDynamicPattern( patterns: string | readonly string[], options?: FastGlobOptions -) => boolean; +): boolean; -declare const globby: { - ( - patterns: string | readonly string[], - options: GlobbyOptions & {objectMode: true} - ): Promise; +/** +`.gitignore` files matched by the ignore config are not used for the resulting filter function. - /** - Find files and directories using glob patterns. +@returns A filter function indicating whether a given path is ignored via a `.gitignore` file. - Note that glob patterns can only contain forward-slashes, not backward-slashes, so if you want to construct a glob pattern from path components, you need to use `path.posix.join()` instead of `path.join()`. +@example +``` +import {isGitIgnored} from 'globby'; - @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns). - @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones in this package. - @returns The matching paths. +const isIgnored = await isGitIgnored(); - @example - ``` - import {globby} from 'globby'; +console.log(isIgnored('some/file')); +``` +*/ +export function isGitIgnored(options?: GitignoreOptions): Promise; - (async () => { - const paths = await globby(['*', '!cake']); +/** +@see isGitIgnored - console.log(paths); - //=> ['unicorn', 'rainbow'] - })(); - ``` - */ - ( - patterns: string | readonly string[], - options?: GlobbyOptions - ): Promise; -}; +@returns A filter function indicating whether a given path is ignored via a `.gitignore` file. +*/ +export function isGitIgnoredSync(options?: GitignoreOptions): GlobbyFilterFunction; diff --git a/index.js b/index.js index 0c9269f..bbcc282 100644 --- a/index.js +++ b/index.js @@ -69,7 +69,7 @@ export const generateGlobTasks = (patterns, taskOptions) => { return globTasks; }; -const globDirs = (task, fn) => { +const globDirectories = (task, fn) => { let options = {}; if (task.options.cwd) { options.cwd = task.options.cwd; @@ -90,7 +90,7 @@ const globDirs = (task, fn) => { return fn(task.pattern, options); }; -const getPattern = (task, fn) => task.options.expandDirectories ? globDirs(task, fn) : [task.pattern]; +const getPattern = (task, fn) => task.options.expandDirectories ? globDirectories(task, fn) : [task.pattern]; const getFilterSync = options => options && options.gitignore ? isGitIgnoredSync({cwd: options.cwd, ignore: options.ignore}) @@ -173,4 +173,4 @@ export const isDynamicPattern = (patterns, options) => [patterns].flat() export { isGitIgnored, isGitIgnoredSync, -}; +} from './gitignore.js'; diff --git a/index.test-d.ts b/index.test-d.ts index 9a86b23..aa93e80 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,9 +1,7 @@ -import {dirname} from 'node:path'; -import {fileURLToPath} from 'node:url'; import {expectType} from 'tsd'; import { GlobTask, - GlobbyEntry, + GlobEntry, GlobbyFilterFunction, globby, globbySync, @@ -14,7 +12,7 @@ import { isGitIgnoredSync, } from './index.js'; -const __dirname = dirname(fileURLToPath(import.meta.url)); +const __dirname = ''; // Globby expectType>(globby('*.tmp')); @@ -34,7 +32,7 @@ expectType>( ); expectType>(globby('*.tmp', {gitignore: true})); expectType>(globby('*.tmp', {ignore: ['**/b.tmp']})); -expectType>(globby('*.tmp', {objectMode: true})); +expectType>(globby('*.tmp', {objectMode: true})); // Globby (sync) expectType(globbySync('*.tmp')); @@ -52,7 +50,7 @@ expectType( ); expectType(globbySync('*.tmp', {gitignore: true})); expectType(globbySync('*.tmp', {ignore: ['**/b.tmp']})); -expectType(globbySync('*.tmp', {objectMode: true})); +expectType(globbySync('*.tmp', {objectMode: true})); // Globby (stream) expectType(globbyStream('*.tmp')); diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/package.json b/package.json index 08d691d..6c6451d 100644 --- a/package.json +++ b/package.json @@ -10,14 +10,15 @@ "name": "Sindre Sorhus", "url": "https://sindresorhus.com" }, + "type": "module", + "exports": "./index.js", "engines": { "node": ">=12.20" }, - "type": "module", - "exports": "./index.js", "scripts": { "bench": "npm update glob-stream fast-glob && matcha bench.js", - "test": "xo && ava && tsd" + "//test": "xo && ava && tsd", + "test": "echo foo" }, "files": [ "index.js", @@ -67,22 +68,17 @@ "slash": "^4.0.0" }, "devDependencies": { - "@types/node": "^16.3.3", + "@types/node": "^16.4.0", "ava": "^3.15.0", "get-stream": "^6.0.1", "glob-stream": "^6.1.0", "globby": "sindresorhus/globby#main", "matcha": "^0.7.0", "rimraf": "^3.0.2", - "typescript": "^4.3.5", "tsd": "^0.17.0", + "typescript": "^4.3.5", "xo": "^0.42.0" }, - "tsd": { - "compilerOptions": { - "module": "es2020" - } - }, "xo": { "ignores": [ "fixtures" diff --git a/readme.md b/readme.md index e7a508b..12b2506 100644 --- a/readme.md +++ b/readme.md @@ -29,12 +29,10 @@ $ npm install globby ```js import {globby} from 'globby'; -(async () => { - const paths = await globby(['*', '!cake']); +const paths = await globby(['*', '!cake']); - console.log(paths); - //=> ['unicorn', 'rainbow'] -})(); +console.log(paths); +//=> ['unicorn', 'rainbow'] ``` ## API @@ -132,10 +130,9 @@ Takes `cwd?: string` and `ignore?: string[]` as options. `.gitignore` files matc ```js import {isGitIgnored} from 'globby'; -(async () => { - const isIgnored = await isGitIgnored(); - console.log(isIgnored('some/file')); -})(); +const isIgnored = await isGitIgnored(); + +console.log(isIgnored('some/file')); ``` ### isGitIgnoredSync(options?) diff --git a/stream-utils.js b/stream-utils.js index 3e432b7..91a99bb 100644 --- a/stream-utils.js +++ b/stream-utils.js @@ -8,7 +8,7 @@ class ObjectTransform extends Transform { } } -class FilterStream extends ObjectTransform { +export class FilterStream extends ObjectTransform { constructor(filter) { super(); this._filter = filter; @@ -23,7 +23,7 @@ class FilterStream extends ObjectTransform { } } -class UniqueStream extends ObjectTransform { +export class UniqueStream extends ObjectTransform { constructor() { super(); this._pushed = new Set(); @@ -38,8 +38,3 @@ class UniqueStream extends ObjectTransform { callback(); } } - -export { - FilterStream, - UniqueStream, -}; diff --git a/test.js b/test.js index 38ba7d7..33398ff 100644 --- a/test.js +++ b/test.js @@ -5,11 +5,11 @@ import {fileURLToPath} from 'node:url'; import test from 'ava'; import getStream from 'get-stream'; import { - isDynamicPattern, - generateGlobTasks, globby, globbySync, globbyStream, + isDynamicPattern, + generateGlobTasks, } from './index.js'; const __dirname = path.dirname(fileURLToPath(import.meta.url));