index.d.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import {IOptions as NodeGlobOptions} from 'glob';
  2. import {Options as FastGlobOptions} from 'fast-glob';
  3. declare namespace globby {
  4. type ExpandDirectoriesOption =
  5. | boolean
  6. | readonly string[]
  7. | {files?: readonly string[]; extensions?: readonly string[]};
  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: globby.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. `.gitignore` files matched by the ignore config are not used for the resulting filter function.
  48. @returns A filter function indicating whether a given path is ignored via a `.gitignore` file.
  49. @example
  50. ```
  51. import {gitignore} from 'globby';
  52. (async () => {
  53. const isIgnored = await gitignore();
  54. console.log(isIgnored('some/file'));
  55. })();
  56. ```
  57. */
  58. (options?: globby.GitignoreOptions): Promise<globby.FilterFunction>;
  59. /**
  60. @returns A filter function indicating whether a given path is ignored via a `.gitignore` file.
  61. */
  62. sync(options?: globby.GitignoreOptions): 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-1) in addition to the ones in this package.
  70. @returns The matching paths.
  71. @example
  72. ```
  73. import globby = require('globby');
  74. (async () => {
  75. const paths = await globby(['*', '!cake']);
  76. console.log(paths);
  77. //=> ['unicorn', 'rainbow']
  78. })();
  79. ```
  80. */
  81. (
  82. patterns: string | readonly string[],
  83. options?: globby.GlobbyOptions
  84. ): Promise<string[]>;
  85. /**
  86. Find files and directories using glob patterns.
  87. 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()`.
  88. @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
  89. @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package.
  90. @returns The matching paths.
  91. */
  92. sync(
  93. patterns: string | readonly string[],
  94. options?: globby.GlobbyOptions
  95. ): string[];
  96. /**
  97. Find files and directories using glob patterns.
  98. 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()`.
  99. @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
  100. @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package.
  101. @returns The stream of matching paths.
  102. @example
  103. ```
  104. import globby = require('globby');
  105. (async () => {
  106. for await (const path of globby.stream('*.tmp')) {
  107. console.log(path);
  108. }
  109. })();
  110. ```
  111. */
  112. stream(
  113. patterns: string | readonly string[],
  114. options?: globby.GlobbyOptions
  115. ): NodeJS.ReadableStream;
  116. /**
  117. 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.
  118. @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
  119. @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package.
  120. @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.
  121. */
  122. generateGlobTasks(
  123. patterns: string | readonly string[],
  124. options?: globby.GlobbyOptions
  125. ): globby.GlobTask[];
  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. This function is backed by [`node-glob`](https://github.com/isaacs/node-glob#globhasmagicpattern-options).
  129. @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
  130. @param options - See the [`node-glob` options](https://github.com/isaacs/node-glob#globhasmagicpattern-options).
  131. @returns Whether there are any special glob characters in the `patterns`.
  132. */
  133. hasMagic(
  134. patterns: string | readonly string[],
  135. options?: NodeGlobOptions
  136. ): boolean;
  137. readonly gitignore: Gitignore;
  138. };
  139. export = globby;