UNPKG

6.93 kBTypeScriptView Raw
1import {Options as FastGlobOptions, Entry} from 'fast-glob';
2
3export type GlobEntry = Entry;
4
5export interface GlobTask {
6 readonly patterns: string[];
7 readonly options: Options;
8}
9
10export type ExpandDirectoriesOption =
11 | boolean
12 | readonly string[]
13 | {files?: readonly string[]; extensions?: readonly string[]};
14
15type FastGlobOptionsWithoutCwd = Omit<FastGlobOptions, 'cwd'>;
16
17export interface Options extends FastGlobOptionsWithoutCwd {
18 /**
19 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.
20
21 Note that if you set this option to `false`, you won't get back matched directories unless you set `onlyFiles: false`.
22
23 @default true
24
25 @example
26 ```
27 import {globby} from 'globby';
28
29 const paths = await globby('images', {
30 expandDirectories: {
31 files: ['cat', 'unicorn', '*.jpg'],
32 extensions: ['png']
33 }
34 });
35
36 console.log(paths);
37 //=> ['cat.png', 'unicorn.png', 'cow.jpg', 'rainbow.jpg']
38 ```
39 */
40 readonly expandDirectories?: ExpandDirectoriesOption;
41
42 /**
43 Respect ignore patterns in `.gitignore` files that apply to the globbed files.
44
45 @default false
46 */
47 readonly gitignore?: boolean;
48
49 /**
50 Glob patterns to look for ignore files, which are then used to ignore globbed files.
51
52 This is a more generic form of the `gitignore` option, allowing you to find ignore files with a [compatible syntax](http://git-scm.com/docs/gitignore). For instance, this works with Babel's `.babelignore`, Prettier's `.prettierignore`, or ESLint's `.eslintignore` files.
53
54 @default undefined
55 */
56 readonly ignoreFiles?: string | readonly string[];
57
58 /**
59 The current working directory in which to search.
60
61 @default process.cwd()
62 */
63 readonly cwd?: URL | string;
64}
65
66export interface GitignoreOptions {
67 readonly cwd?: URL | string;
68}
69
70export type GlobbyFilterFunction = (path: URL | string) => boolean;
71
72/**
73Find files and directories using glob patterns.
74
75Note 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()`.
76
77@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
78@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones in this package.
79@returns The matching paths.
80
81@example
82```
83import {globby} from 'globby';
84
85const paths = await globby(['*', '!cake']);
86
87console.log(paths);
88//=> ['unicorn', 'rainbow']
89```
90*/
91export function globby(
92 patterns: string | readonly string[],
93 options: Options & {objectMode: true}
94): Promise<GlobEntry[]>;
95export function globby(
96 patterns: string | readonly string[],
97 options?: Options
98): Promise<string[]>;
99
100/**
101Find files and directories using glob patterns.
102
103Note 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()`.
104
105@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
106@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones in this package.
107@returns The matching paths.
108*/
109export function globbySync(
110 patterns: string | readonly string[],
111 options: Options & {objectMode: true}
112): GlobEntry[];
113export function globbySync(
114 patterns: string | readonly string[],
115 options?: Options
116): string[];
117
118/**
119Find files and directories using glob patterns.
120
121Note 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()`.
122
123@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
124@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones in this package.
125@returns The stream of matching paths.
126
127@example
128```
129import {globbyStream} from 'globby';
130
131for await (const path of globbyStream('*.tmp')) {
132 console.log(path);
133}
134```
135*/
136export function globbyStream(
137 patterns: string | readonly string[],
138 options?: Options
139): NodeJS.ReadableStream;
140
141/**
142Note 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.
143
144@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
145@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones in this package.
146@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.
147*/
148export function generateGlobTasks(
149 patterns: string | readonly string[],
150 options?: Options
151): Promise<GlobTask[]>;
152
153/**
154@see generateGlobTasks
155
156@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.
157*/
158export function generateGlobTasksSync(
159 patterns: string | readonly string[],
160 options?: Options
161): GlobTask[];
162
163/**
164Note that the options affect the results.
165
166This function is backed by [`fast-glob`](https://github.com/mrmlnc/fast-glob#isdynamicpatternpattern-options).
167
168@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
169@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3).
170@returns Whether there are any special glob characters in the `patterns`.
171*/
172export function isDynamicPattern(
173 patterns: string | readonly string[],
174 options?: FastGlobOptionsWithoutCwd & {
175 /**
176 The current working directory in which to search.
177
178 @default process.cwd()
179 */
180 readonly cwd?: URL | string;
181 }
182): boolean;
183
184/**
185`.gitignore` files matched by the ignore config are not used for the resulting filter function.
186
187@returns A filter function indicating whether a given path is ignored via a `.gitignore` file.
188
189@example
190```
191import {isGitIgnored} from 'globby';
192
193const isIgnored = await isGitIgnored();
194
195console.log(isIgnored('some/file'));
196```
197*/
198export function isGitIgnored(options?: GitignoreOptions): Promise<GlobbyFilterFunction>;
199
200/**
201@see isGitIgnored
202
203@returns A filter function indicating whether a given path is ignored via a `.gitignore` file.
204*/
205export function isGitIgnoredSync(options?: GitignoreOptions): GlobbyFilterFunction;