diff --git a/types/globby/globby-tests.ts b/types/globby/globby-tests.ts index 1c3c1842d4..e1a57adb74 100644 --- a/types/globby/globby-tests.ts +++ b/types/globby/globby-tests.ts @@ -1,24 +1,71 @@ import { IOptions } from 'glob'; -import globby = require("globby"); +import globby = require('globby'); (async () => { let result: string[]; + + /** + * Standard `pattern` usage + */ result = await globby('*.tmp'); result = await globby(['a.tmp', '*.tmp', '!{c,d,e}.tmp']); result = globby.sync('*.tmp'); result = globby.sync(['a.tmp', '*.tmp', '!{c,d,e}.tmp']); - result = await globby('*.tmp', Object.freeze({ignore: Object.freeze([])})); - result = globby.sync('*.tmp', Object.freeze({ignore: Object.freeze([])})); + /** + * `expandDirectories` option + */ + result = await globby('*.tmp', { expandDirectories: false }); + result = globby.sync('*.tmp', { expandDirectories: false }); + result = await globby('*.tmp', { expandDirectories: ['a*', 'b*'] }); + result = globby.sync('*.tmp', { expandDirectories: ['a*', 'b*'] }); + result = await globby('*.tmp', { + expandDirectories: { + files: ['a', 'b'], + extensions: ['tmp'] + } + }); + result = globby.sync('*.tmp', { + expandDirectories: { + files: ['a', 'b'], + extensions: ['tmp'] + } + }); + + /** + * Options passed through from `fast-glob` + */ + result = await globby('*.tmp', { ignore: ['**/b.tmp'] }); + result = globby.sync('*.tmp', { ignore: ['**/b.tmp'] }); })(); const tasks: Array<{ - pattern: string, - options: IOptions -}> = globby.generateGlobTasks(['*.tmp', '!b.tmp'], {ignore: ['c.tmp']}); + pattern: string; + options: IOptions; +}> = globby.generateGlobTasks(['*.tmp', '!b.tmp'], { ignore: ['c.tmp'] }); console.log(globby.hasMagic('**')); console.log(globby.hasMagic(['**', 'path1', 'path2'])); console.log(!globby.hasMagic(['path1', 'path2'])); + +(async () => { + let result: (path: string) => boolean; + + /** + * Standard `gitignore` usage + */ + result = await globby.gitignore(); + result = globby.gitignore.sync(); + + /** With options */ + result = await globby.gitignore({ + cwd: __dirname, + ignore: ['**/b.tmp'] + }); + result = globby.gitignore.sync({ + cwd: __dirname, + ignore: ['**/b.tmp'] + }); +})(); diff --git a/types/globby/index.d.ts b/types/globby/index.d.ts index 531888f803..af49813432 100644 --- a/types/globby/index.d.ts +++ b/types/globby/index.d.ts @@ -1,37 +1,99 @@ -// Type definitions for globby 6.1 +// Type definitions for globby 8.0 // Project: https://github.com/sindresorhus/globby#readme // Definitions by: Douglas Duteil // Ika // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 -import { IOptions } from 'glob'; +import { IOptions as NodeGlobOptions } from 'glob'; +import { Options as FastGlobOptions } from 'fast-glob'; + +type ExpandDirectoriesOption = boolean | string[] | { files: string[]; extensions: string[] }; + +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 below: + * + * ```js + * (async () => { + * const paths = await globby('images', { + * expandDirectories: { + * files: ['cat', 'unicorn', '*.jpg'], + * extensions: ['png'] + * } + * }); + * console.log(paths); + * //=> ['cat.png', 'unicorn.png', 'cow.jpg', 'rainbow.jpg'] + * })(); + * ``` + * + * Note that if you set this option to `false`, you won't get back matched directories unless + * you set `onlyFiles: false`. + */ + expandDirectories?: ExpandDirectoriesOption; + /** + * Respect ignore patterns in `.gitignore` files that apply to the globbed files. + */ + gitignore?: boolean; +} /** * Returns a `Promise` of matching paths. */ -declare function globby(patterns: string | string[], options?: IOptions): Promise; +declare function globby(patterns: string | string[], options?: Options): Promise; declare namespace globby { /** * Returns an `Array` of matching paths. */ - function sync(patterns: string | string[], options?: IOptions): string[]; + function sync(patterns: string | string[], options?: Options): string[]; /** * Returns an `Array` in the format `{ pattern: string, opts: Object }`, - * which can be passed as arguments to [node-glob](https://github.com/isaacs/node-glob). + * which can be passed as arguments to [`fast-glob`](https://github.com/mrmlnc/fast-glob). * This is useful for other globbing-related packages. * * 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. */ - function generateGlobTasks(patterns: string | string[], options?: IOptions): Array<{pattern: string, options: IOptions}>; + function generateGlobTasks(patterns: string | string[], options?: Options): Array<{ pattern: string; options: Options }>; /** - * Returns a `boolean` of whether there are any special glob characters in the patterns. + * Returns a boolean of whether there are any special glob characters in the `patterns`. * - * Note that the options affect the results. If `noext: true` is set, then `+(a|b)` will not be considered a magic pattern. - * If the pattern has a brace expansion, like `a/{b/c,x/y}`, then that is considered magical, unless `nobrace: true` is set. + * Note that the options affect the results. If `noext: true` is set, then `+(a|b)` will not + * be considered a magic pattern. If the pattern has a brace expansion, like `a/{b/c,x/y}`, + * then that is considered magical, unless `nobrace: true` is set. + * + * This function is backed by [`node-glob`](https://github.com/isaacs/node-glob#globhasmagicpattern-options) */ - function hasMagic(patterns: string | string[], options?: IOptions): boolean; + function hasMagic(patterns: string | string[], options?: NodeGlobOptions): boolean; + /** + * Returns a Promise<(path: string) => boolean> indicating whether a given path is ignored + * via a `.gitignore` file. + * + * Takes `cwd?: string` and `ignore?: string[]` as options. `.gitignore` files matched by the + * ignore config are not used for the resulting filter function. + * + * ```js + * const {gitignore} = require('globby'); + * + * (async () => { + * const isIgnored = await gitignore(); + * console.log(isIgnored('some/file')); + * })(); + * ``` + */ + function gitignore(options?: { cwd?: string; ignore?: string[]; }): Promise<(path: string) => boolean>; + + namespace gitignore { + /** + * Returns a `(path: string) => boolean` indicating whether a given path is ignored via a `.gitignore` file. + * + * Takes the same options as `globby.gitignore`. + */ + function sync(options?: { cwd?: string; ignore?: string[]; }): (path: string) => boolean; + } } export = globby; diff --git a/types/globby/package.json b/types/globby/package.json new file mode 100644 index 0000000000..136d4694e9 --- /dev/null +++ b/types/globby/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "dependencies": { + "fast-glob": "^2.0.2" + } +}