index.d.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import {Options as FastGlobOptions, Entry as FastGlobEntry} from 'fast-glob';
  2. declare namespace globby {
  3. type ExpandDirectoriesOption =
  4. | boolean
  5. | readonly string[]
  6. | {files?: readonly string[]; extensions?: readonly string[]};
  7. type Entry = FastGlobEntry;
  8. interface GlobbyOptions extends FastGlobOptions {
  9. /**
  10. 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.
  11. Note that if you set this option to `false`, you won't get back matched directories unless you set `onlyFiles: false`.
  12. @default true
  13. @example
  14. ```
  15. import globby = require('globby');
  16. (async () => {
  17. const paths = await globby('images', {
  18. expandDirectories: {
  19. files: ['cat', 'unicorn', '*.jpg'],
  20. extensions: ['png']
  21. }
  22. });
  23. console.log(paths);
  24. //=> ['cat.png', 'unicorn.png', 'cow.jpg', 'rainbow.jpg']
  25. })();
  26. ```
  27. */
  28. readonly expandDirectories?: ExpandDirectoriesOption;
  29. /**
  30. Respect ignore patterns in `.gitignore` files that apply to the globbed files.
  31. @default false
  32. */
  33. readonly gitignore?: boolean;
  34. }
  35. interface GlobTask {
  36. readonly pattern: string;
  37. readonly options: GlobbyOptions;
  38. }
  39. interface GitignoreOptions {
  40. readonly cwd?: string;
  41. readonly ignore?: readonly string[];
  42. }
  43. type FilterFunction = (path: string) => boolean;
  44. }
  45. interface Gitignore {
  46. /**
  47. @returns A filter function indicating whether a given path is ignored via a `.gitignore` file.
  48. */
  49. sync: (options?: globby.GitignoreOptions) => globby.FilterFunction;
  50. /**
  51. `.gitignore` files matched by the ignore config are not used for the resulting filter function.
  52. @returns A filter function indicating whether a given path is ignored via a `.gitignore` file.
  53. @example
  54. ```
  55. import {gitignore} from 'globby';
  56. (async () => {
  57. const isIgnored = await gitignore();
  58. console.log(isIgnored('some/file'));
  59. })();
  60. ```
  61. */
  62. (options?: globby.GitignoreOptions): Promise<globby.FilterFunction>;
  63. }
  64. declare const globby: {
  65. /**
  66. Find files and directories using glob patterns.
  67. 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()`.
  68. @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
  69. @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones in this package.
  70. @returns The matching paths.
  71. */
  72. sync: ((
  73. patterns: string | readonly string[],
  74. options: globby.GlobbyOptions & {objectMode: true}
  75. ) => globby.Entry[]) & ((
  76. patterns: string | readonly string[],
  77. options?: globby.GlobbyOptions
  78. ) => string[]);
  79. /**
  80. Find files and directories using glob patterns.
  81. 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()`.
  82. @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
  83. @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones in this package.
  84. @returns The stream of matching paths.
  85. @example
  86. ```
  87. import globby = require('globby');
  88. (async () => {
  89. for await (const path of globby.stream('*.tmp')) {
  90. console.log(path);
  91. }
  92. })();
  93. ```
  94. */
  95. stream: (
  96. patterns: string | readonly string[],
  97. options?: globby.GlobbyOptions
  98. ) => NodeJS.ReadableStream;
  99. /**
  100. 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.
  101. @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
  102. @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones in this package.
  103. @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.
  104. */
  105. generateGlobTasks: (
  106. patterns: string | readonly string[],
  107. options?: globby.GlobbyOptions
  108. ) => globby.GlobTask[];
  109. /**
  110. Note that the options affect the results.
  111. This function is backed by [`fast-glob`](https://github.com/mrmlnc/fast-glob#isdynamicpatternpattern-options).
  112. @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
  113. @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3).
  114. @returns Whether there are any special glob characters in the `patterns`.
  115. */
  116. hasMagic: (
  117. patterns: string | readonly string[],
  118. options?: FastGlobOptions
  119. ) => boolean;
  120. readonly gitignore: Gitignore;
  121. (
  122. patterns: string | readonly string[],
  123. options: globby.GlobbyOptions & {objectMode: true}
  124. ): Promise<globby.Entry[]>;
  125. /**
  126. Find files and directories using glob patterns.
  127. 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()`.
  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) in addition to the ones in this package.
  130. @returns The matching paths.
  131. @example
  132. ```
  133. import globby = require('globby');
  134. (async () => {
  135. const paths = await globby(['*', '!cake']);
  136. console.log(paths);
  137. //=> ['unicorn', 'rainbow']
  138. })();
  139. ```
  140. */
  141. (
  142. patterns: string | readonly string[],
  143. options?: globby.GlobbyOptions
  144. ): Promise<string[]>;
  145. };
  146. export = globby;