index.d.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. | ReadonlyArray<string>
  7. | {files: ReadonlyArray<string>; extensions: ReadonlyArray<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?: ReadonlyArray<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 `Promise` for 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. @param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage).
  67. @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package.
  68. @returns A `Promise<Array>` of matching paths.
  69. @example
  70. ```
  71. import globby = require('globby');
  72. (async () => {
  73. const paths = await globby(['*', '!cake']);
  74. console.log(paths);
  75. //=> ['unicorn', 'rainbow']
  76. })();
  77. ```
  78. */
  79. (
  80. patterns: string | ReadonlyArray<string>,
  81. options?: globby.GlobbyOptions
  82. ): Promise<string[]>;
  83. /**
  84. @param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage).
  85. @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package.
  86. @returns An `Array` of matching paths.
  87. */
  88. sync(
  89. patterns: string | ReadonlyArray<string>,
  90. options?: globby.GlobbyOptions
  91. ): string[];
  92. /**
  93. 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.
  94. @param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage).
  95. @param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package.
  96. @returns An `Array<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.
  97. */
  98. generateGlobTasks(
  99. patterns: string | ReadonlyArray<string>,
  100. options?: globby.GlobbyOptions
  101. ): globby.GlobTask[];
  102. /**
  103. 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.
  104. This function is backed by [`node-glob`](https://github.com/isaacs/node-glob#globhasmagicpattern-options).
  105. @param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage).
  106. @param options - See the [`node-glob` options](https://github.com/isaacs/node-glob#globhasmagicpattern-options).
  107. @returns A boolean of whether there are any special glob characters in the `patterns`.
  108. */
  109. hasMagic(
  110. patterns: string | ReadonlyArray<string>,
  111. options?: NodeGlobOptions
  112. ): boolean;
  113. readonly gitignore: Gitignore;
  114. // TODO: Remove this for the next major release
  115. default: typeof globby;
  116. };
  117. export = globby;