Skip to content

Commit dc0a49c

Browse files
BendingBendersindresorhus
authored andcommitted
Refactor TypeScript definition to CommonJS compatible export (#115)
1 parent 89cee24 commit dc0a49c

File tree

4 files changed

+139
-114
lines changed

4 files changed

+139
-114
lines changed

index.d.ts

+131-108
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,146 @@
11
import {IOptions as NodeGlobOptions} from 'glob';
22
import {Options as FastGlobOptions} from 'fast-glob';
33

4-
export type ExpandDirectoriesOption =
5-
| boolean
6-
| ReadonlyArray<string>
7-
| {files: ReadonlyArray<string>; extensions: ReadonlyArray<string>};
4+
declare namespace globby {
5+
type ExpandDirectoriesOption =
6+
| boolean
7+
| ReadonlyArray<string>
8+
| {files: ReadonlyArray<string>; extensions: ReadonlyArray<string>};
89

9-
export interface GlobbyOptions extends FastGlobOptions {
10+
interface GlobbyOptions extends FastGlobOptions {
11+
/**
12+
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.
13+
14+
Note that if you set this option to `false`, you won't get back matched directories unless you set `onlyFiles: false`.
15+
16+
@default true
17+
18+
@example
19+
```
20+
import globby = require('globby');
21+
22+
(async () => {
23+
const paths = await globby('images', {
24+
expandDirectories: {
25+
files: ['cat', 'unicorn', '*.jpg'],
26+
extensions: ['png']
27+
}
28+
});
29+
console.log(paths);
30+
//=> ['cat.png', 'unicorn.png', 'cow.jpg', 'rainbow.jpg']
31+
})();
32+
```
33+
*/
34+
readonly expandDirectories?: ExpandDirectoriesOption;
35+
36+
/**
37+
Respect ignore patterns in `.gitignore` files that apply to the globbed files.
38+
39+
@default false
40+
*/
41+
readonly gitignore?: boolean;
42+
}
43+
44+
interface GlobTask {
45+
readonly pattern: string;
46+
readonly options: globby.GlobbyOptions;
47+
}
48+
49+
interface GitignoreOptions {
50+
readonly cwd?: string;
51+
readonly ignore?: ReadonlyArray<string>;
52+
}
53+
54+
type FilterFunction = (path: string) => boolean;
55+
}
56+
57+
interface Gitignore {
1058
/**
11-
* 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.
12-
*
13-
* Note that if you set this option to `false`, you won't get back matched directories unless you set `onlyFiles: false`.
14-
*
15-
* @default true
16-
*
17-
* @example
18-
*
19-
* import globby from 'globby';
20-
*
21-
* (async () => {
22-
* const paths = await globby('images', {
23-
* expandDirectories: {
24-
* files: ['cat', 'unicorn', '*.jpg'],
25-
* extensions: ['png']
26-
* }
27-
* });
28-
* console.log(paths);
29-
* //=> ['cat.png', 'unicorn.png', 'cow.jpg', 'rainbow.jpg']
30-
* })();
31-
*/
32-
readonly expandDirectories?: ExpandDirectoriesOption;
59+
`.gitignore` files matched by the ignore config are not used for the resulting filter function.
60+
61+
@returns A `Promise` for a filter function indicating whether a given path is ignored via a `.gitignore` file.
62+
63+
@example
64+
```
65+
import {gitignore} from 'globby';
66+
67+
(async () => {
68+
const isIgnored = await gitignore();
69+
console.log(isIgnored('some/file'));
70+
})();
71+
```
72+
*/
73+
(options?: globby.GitignoreOptions): Promise<globby.FilterFunction>;
3374

3475
/**
35-
* Respect ignore patterns in `.gitignore` files that apply to the globbed files.
36-
*
37-
* @default false
38-
*/
39-
readonly gitignore?: boolean;
76+
@returns A filter function indicating whether a given path is ignored via a `.gitignore` file.
77+
*/
78+
sync(options?: globby.GitignoreOptions): globby.FilterFunction;
4079
}
4180

42-
/**
43-
* @param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage).
44-
* @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package.
45-
* @returns A `Promise<Array>` of matching paths.
46-
*/
47-
export default function globby(
48-
patterns: string | ReadonlyArray<string>,
49-
options?: GlobbyOptions
50-
): Promise<string[]>;
51-
52-
/**
53-
* @param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage).
54-
* @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package.
55-
* @returns An `Array` of matching paths.
56-
*/
57-
export function sync(
58-
patterns: string | ReadonlyArray<string>,
59-
options?: GlobbyOptions
60-
): string[];
61-
62-
export interface GlobTask {
63-
readonly pattern: string;
64-
readonly options: GlobbyOptions;
65-
}
81+
declare const globby: {
82+
/**
83+
@param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage).
84+
@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package.
85+
@returns A `Promise<Array>` of matching paths.
6686
67-
/**
68-
* 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.
69-
*
70-
* @param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage).
71-
* @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package.
72-
* @returns An `Array<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.
73-
*/
74-
export function generateGlobTasks(
75-
patterns: string | ReadonlyArray<string>,
76-
options?: GlobbyOptions
77-
): GlobTask[];
78-
79-
/**
80-
* 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.
81-
*
82-
* This function is backed by [`node-glob`](https://github.com/isaacs/node-glob#globhasmagicpattern-options).
83-
*
84-
* @param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage).
85-
* @param options - See the [`node-glob` options](https://github.com/isaacs/node-glob#globhasmagicpattern-options).
86-
* @returns A boolean of whether there are any special glob characters in the `patterns`.
87-
*/
88-
export function hasMagic(
89-
patterns: string | ReadonlyArray<string>,
90-
options?: NodeGlobOptions
91-
): boolean;
92-
93-
export interface GitignoreOptions {
94-
readonly cwd?: string;
95-
readonly ignore?: ReadonlyArray<string>;
96-
}
87+
@example
88+
```
89+
import globby = require('globby');
9790
98-
export type FilterFunction = (path: string) => boolean;
91+
(async () => {
92+
const paths = await globby(['*', '!cake']);
9993
100-
export interface Gitignore {
101-
(options?: GitignoreOptions): Promise<FilterFunction>;
94+
console.log(paths);
95+
//=> ['unicorn', 'rainbow']
96+
})();
97+
```
98+
*/
99+
(
100+
patterns: string | ReadonlyArray<string>,
101+
options?: globby.GlobbyOptions
102+
): Promise<string[]>;
102103

103104
/**
104-
* @returns A filter function indicating whether a given path is ignored via a `.gitignore` file.
105-
*/
106-
sync(options?: GitignoreOptions): FilterFunction;
107-
}
105+
@param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage).
106+
@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package.
107+
@returns An `Array` of matching paths.
108+
*/
109+
sync(
110+
patterns: string | ReadonlyArray<string>,
111+
options?: globby.GlobbyOptions
112+
): string[];
113+
114+
/**
115+
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.
116+
117+
@param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage).
118+
@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package.
119+
@returns An `Array<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.
120+
*/
121+
generateGlobTasks(
122+
patterns: string | ReadonlyArray<string>,
123+
options?: globby.GlobbyOptions
124+
): globby.GlobTask[];
125+
126+
/**
127+
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.
128+
129+
This function is backed by [`node-glob`](https://github.com/isaacs/node-glob#globhasmagicpattern-options).
130+
131+
@param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage).
132+
@param options - See the [`node-glob` options](https://github.com/isaacs/node-glob#globhasmagicpattern-options).
133+
@returns A boolean of whether there are any special glob characters in the `patterns`.
134+
*/
135+
hasMagic(
136+
patterns: string | ReadonlyArray<string>,
137+
options?: NodeGlobOptions
138+
): boolean;
139+
140+
readonly gitignore: Gitignore;
141+
142+
// TODO: Remove this for the next major release
143+
default: typeof globby;
144+
};
108145

109-
/**
110-
* `.gitignore` files matched by the ignore config are not used for the resulting filter function.
111-
*
112-
* @returns A `Promise` for a filter function indicating whether a given path is ignored via a `.gitignore` file.
113-
*
114-
* @example
115-
*
116-
* import {gitignore} from 'globby';
117-
*
118-
* (async () => {
119-
* const isIgnored = await gitignore();
120-
* console.log(isIgnored('some/file'));
121-
* })();
122-
*/
123-
export const gitignore: Gitignore;
146+
export = globby;

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ const globby = (patterns, options) => {
115115
};
116116

117117
module.exports = globby;
118+
// TODO: Remove this for the next major release
118119
module.exports.default = globby;
119120

120121
module.exports.sync = (patterns, options) => {

index.test-d.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import {expectType} from 'tsd-check';
2-
import globby, {
1+
import {expectType} from 'tsd';
2+
import globby = require('.');
3+
import {
34
GlobTask,
45
FilterFunction,
56
sync as globbySync,

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
"scripts": {
1616
"bench": "npm update glob-stream fast-glob && matcha bench.js",
17-
"test": "xo && ava && tsd-check"
17+
"test": "xo && ava && tsd"
1818
},
1919
"files": [
2020
"index.js",
@@ -58,20 +58,20 @@
5858
"dependencies": {
5959
"@types/glob": "^7.1.1",
6060
"array-union": "^1.0.2",
61-
"dir-glob": "^2.2.1",
61+
"dir-glob": "^2.2.2",
6262
"fast-glob": "^2.2.6",
6363
"glob": "^7.1.3",
6464
"ignore": "^4.0.3",
6565
"pify": "^4.0.1",
6666
"slash": "^2.0.0"
6767
},
6868
"devDependencies": {
69-
"ava": "^1.2.1",
69+
"ava": "^1.4.1",
7070
"glob-stream": "^6.1.0",
7171
"globby": "sindresorhus/globby#master",
7272
"matcha": "^0.7.0",
7373
"rimraf": "^2.6.3",
74-
"tsd-check": "^0.3.0",
74+
"tsd": "^0.7.1",
7575
"xo": "^0.24.0"
7676
},
7777
"xo": {

0 commit comments

Comments
 (0)