From dc0a49cbf39afdd1a4919257e086454d28c9ea2e Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Mon, 1 Apr 2019 09:08:32 +0000 Subject: [PATCH] Refactor TypeScript definition to CommonJS compatible export (#115) --- index.d.ts | 239 ++++++++++++++++++++++++++---------------------- index.js | 1 + index.test-d.ts | 5 +- package.json | 8 +- 4 files changed, 139 insertions(+), 114 deletions(-) diff --git a/index.d.ts b/index.d.ts index 8715113..cae3e97 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,123 +1,146 @@ import {IOptions as NodeGlobOptions} from 'glob'; import {Options as FastGlobOptions} from 'fast-glob'; -export type ExpandDirectoriesOption = - | boolean - | ReadonlyArray - | {files: ReadonlyArray; extensions: ReadonlyArray}; +declare namespace globby { + type ExpandDirectoriesOption = + | boolean + | ReadonlyArray + | {files: ReadonlyArray; extensions: ReadonlyArray}; -export interface GlobbyOptions extends FastGlobOptions { + interface GlobbyOptions 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. + + Note that if you set this option to `false`, you won't get back matched directories unless you set `onlyFiles: false`. + + @default true + + @example + ``` + import globby = require('globby'); + + (async () => { + const paths = await globby('images', { + expandDirectories: { + files: ['cat', 'unicorn', '*.jpg'], + extensions: ['png'] + } + }); + 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. + + @default false + */ + readonly gitignore?: boolean; + } + + interface GlobTask { + readonly pattern: string; + readonly options: globby.GlobbyOptions; + } + + interface GitignoreOptions { + readonly cwd?: string; + readonly ignore?: ReadonlyArray; + } + + type FilterFunction = (path: string) => boolean; +} + +interface Gitignore { /** - * 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`. - * - * @default true - * - * @example - * - * import globby from 'globby'; - * - * (async () => { - * const paths = await globby('images', { - * expandDirectories: { - * files: ['cat', 'unicorn', '*.jpg'], - * extensions: ['png'] - * } - * }); - * console.log(paths); - * //=> ['cat.png', 'unicorn.png', 'cow.jpg', 'rainbow.jpg'] - * })(); - */ - readonly expandDirectories?: ExpandDirectoriesOption; + `.gitignore` files matched by the ignore config are not used for the resulting filter function. + + @returns A `Promise` for a filter function indicating whether a given path is ignored via a `.gitignore` file. + + @example + ``` + import {gitignore} from 'globby'; + + (async () => { + const isIgnored = await gitignore(); + console.log(isIgnored('some/file')); + })(); + ``` + */ + (options?: globby.GitignoreOptions): Promise; /** - * Respect ignore patterns in `.gitignore` files that apply to the globbed files. - * - * @default false - */ - readonly gitignore?: boolean; + @returns A filter function indicating whether a given path is ignored via a `.gitignore` file. + */ + sync(options?: globby.GitignoreOptions): globby.FilterFunction; } -/** - * @param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage). - * @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package. - * @returns A `Promise` of matching paths. - */ -export default function globby( - patterns: string | ReadonlyArray, - options?: GlobbyOptions -): Promise; - -/** - * @param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage). - * @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package. - * @returns An `Array` of matching paths. - */ -export function sync( - patterns: string | ReadonlyArray, - options?: GlobbyOptions -): string[]; - -export interface GlobTask { - readonly pattern: string; - readonly options: GlobbyOptions; -} +declare const globby: { + /** + @param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage). + @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package. + @returns A `Promise` of matching paths. -/** - * 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 supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage). - * @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package. - * @returns An `Array` 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 | ReadonlyArray, - options?: GlobbyOptions -): GlobTask[]; - -/** - * 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). - * - * @param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage). - * @param options - See the [`node-glob` options](https://github.com/isaacs/node-glob#globhasmagicpattern-options). - * @returns A boolean of whether there are any special glob characters in the `patterns`. - */ -export function hasMagic( - patterns: string | ReadonlyArray, - options?: NodeGlobOptions -): boolean; - -export interface GitignoreOptions { - readonly cwd?: string; - readonly ignore?: ReadonlyArray; -} + @example + ``` + import globby = require('globby'); -export type FilterFunction = (path: string) => boolean; + (async () => { + const paths = await globby(['*', '!cake']); -export interface Gitignore { - (options?: GitignoreOptions): Promise; + console.log(paths); + //=> ['unicorn', 'rainbow'] + })(); + ``` + */ + ( + patterns: string | ReadonlyArray, + options?: globby.GlobbyOptions + ): Promise; /** - * @returns A filter function indicating whether a given path is ignored via a `.gitignore` file. - */ - sync(options?: GitignoreOptions): FilterFunction; -} + @param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage). + @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package. + @returns An `Array` of matching paths. + */ + sync( + patterns: string | ReadonlyArray, + options?: globby.GlobbyOptions + ): string[]; + + /** + 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 supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage). + @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package. + @returns An `Array` 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. + */ + generateGlobTasks( + patterns: string | ReadonlyArray, + options?: globby.GlobbyOptions + ): globby.GlobTask[]; + + /** + 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). + + @param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage). + @param options - See the [`node-glob` options](https://github.com/isaacs/node-glob#globhasmagicpattern-options). + @returns A boolean of whether there are any special glob characters in the `patterns`. + */ + hasMagic( + patterns: string | ReadonlyArray, + options?: NodeGlobOptions + ): boolean; + + readonly gitignore: Gitignore; + + // TODO: Remove this for the next major release + default: typeof globby; +}; -/** - * `.gitignore` files matched by the ignore config are not used for the resulting filter function. - * - * @returns A `Promise` for a filter function indicating whether a given path is ignored via a `.gitignore` file. - * - * @example - * - * import {gitignore} from 'globby'; - * - * (async () => { - * const isIgnored = await gitignore(); - * console.log(isIgnored('some/file')); - * })(); - */ -export const gitignore: Gitignore; +export = globby; diff --git a/index.js b/index.js index 08c3150..dc0b225 100644 --- a/index.js +++ b/index.js @@ -115,6 +115,7 @@ const globby = (patterns, options) => { }; module.exports = globby; +// TODO: Remove this for the next major release module.exports.default = globby; module.exports.sync = (patterns, options) => { diff --git a/index.test-d.ts b/index.test-d.ts index aee3a39..4e7a3f0 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,6 @@ -import {expectType} from 'tsd-check'; -import globby, { +import {expectType} from 'tsd'; +import globby = require('.'); +import { GlobTask, FilterFunction, sync as globbySync, diff --git a/package.json b/package.json index 245435e..ce2be9d 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ }, "scripts": { "bench": "npm update glob-stream fast-glob && matcha bench.js", - "test": "xo && ava && tsd-check" + "test": "xo && ava && tsd" }, "files": [ "index.js", @@ -58,7 +58,7 @@ "dependencies": { "@types/glob": "^7.1.1", "array-union": "^1.0.2", - "dir-glob": "^2.2.1", + "dir-glob": "^2.2.2", "fast-glob": "^2.2.6", "glob": "^7.1.3", "ignore": "^4.0.3", @@ -66,12 +66,12 @@ "slash": "^2.0.0" }, "devDependencies": { - "ava": "^1.2.1", + "ava": "^1.4.1", "glob-stream": "^6.1.0", "globby": "sindresorhus/globby#master", "matcha": "^0.7.0", "rimraf": "^2.6.3", - "tsd-check": "^0.3.0", + "tsd": "^0.7.1", "xo": "^0.24.0" }, "xo": {