index.d.ts 5.6 KB

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