index.d.ts 8.8 KB

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