glob.d.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /// <reference types="node" />
  2. import { Minimatch } from 'minimatch';
  3. import Minipass from 'minipass';
  4. import { Path, PathScurry } from 'path-scurry';
  5. import { Ignore } from './ignore.js';
  6. import { Pattern } from './pattern.js';
  7. export type MatchSet = Minimatch['set'];
  8. export type GlobParts = Exclude<Minimatch['globParts'], undefined>;
  9. /**
  10. * A `GlobOptions` object may be provided to any of the exported methods, and
  11. * must be provided to the `Glob` constructor.
  12. *
  13. * All options are optional, boolean, and false by default, unless otherwise
  14. * noted.
  15. *
  16. * All resolved options are added to the Glob object as properties.
  17. *
  18. * If you are running many `glob` operations, you can pass a Glob object as the
  19. * `options` argument to a subsequent operation to share the previously loaded
  20. * cache.
  21. */
  22. export interface GlobOptions {
  23. /**
  24. * Set to true to always receive absolute paths for
  25. * matched files. This does _not_ make an extra system call to get
  26. * the realpath, it only does string path resolution.
  27. *
  28. * By default, when this option is not set, absolute paths are
  29. * returned for patterns that are absolute, and otherwise paths
  30. * are returned that are relative to the `cwd` setting.
  31. *
  32. * Conflicts with {@link withFileTypes}
  33. */
  34. absolute?: boolean;
  35. /**
  36. * Set to false to enable {@link windowsPathsNoEscape}
  37. *
  38. * @deprecated
  39. */
  40. allowWindowsEscape?: boolean;
  41. /**
  42. * The current working directory in which to search. Defaults to
  43. * `process.cwd()`.
  44. *
  45. * May be eiher a string path or a `file://` URL object or string.
  46. */
  47. cwd?: string | URL;
  48. /**
  49. * Include `.dot` files in normal matches and `globstar`
  50. * matches. Note that an explicit dot in a portion of the pattern
  51. * will always match dot files.
  52. */
  53. dot?: boolean;
  54. /**
  55. * Follow symlinked directories when expanding `**`
  56. * patterns. This can result in a lot of duplicate references in
  57. * the presence of cyclic links, and make performance quite bad.
  58. *
  59. * By default, a `**` in a pattern will follow 1 symbolic link if
  60. * it is not the first item in the pattern, or none if it is the
  61. * first item in the pattern, following the same behavior as Bash.
  62. */
  63. follow?: boolean;
  64. /**
  65. * A glob pattern or array of glob patterns to exclude from matches. To
  66. * ignore all children within a directory, as well as the entry itself,
  67. * append `/**'` to the ignore pattern.
  68. */
  69. ignore?: string | string[] | Ignore;
  70. /**
  71. * Add a `/` character to directory matches. Note that this requires
  72. * additional stat calls in some cases.
  73. */
  74. mark?: boolean;
  75. /**
  76. * Perform a basename-only match if the pattern does not contain any slash
  77. * characters. That is, `*.js` would be treated as equivalent to
  78. * `**\/*.js`, matching all js files in all directories.
  79. */
  80. matchBase?: boolean;
  81. /**
  82. * Do not expand `{a,b}` and `{1..3}` brace sets.
  83. */
  84. nobrace?: boolean;
  85. /**
  86. * Perform a case-insensitive match. This defaults to `true` on macOS and
  87. * Windows systems, and `false` on all others.
  88. *
  89. * **Note** `nocase` should only be explicitly set when it is
  90. * known that the filesystem's case sensitivity differs from the
  91. * platform default. If set `true` on case-sensitive file
  92. * systems, or `false` on case-insensitive file systems, then the
  93. * walk may return more or less results than expected.
  94. */
  95. nocase?: boolean;
  96. /**
  97. * Do not match directories, only files. (Note: to match
  98. * _only_ directories, put a `/` at the end of the pattern.)
  99. */
  100. nodir?: boolean;
  101. /**
  102. * Do not match "extglob" patterns such as `+(a|b)`.
  103. */
  104. noext?: boolean;
  105. /**
  106. * Do not match `**` against multiple filenames. (Ie, treat it as a normal
  107. * `*` instead.)
  108. *
  109. * Conflicts with {@link matchBase}
  110. */
  111. noglobstar?: boolean;
  112. /**
  113. * Defaults to value of `process.platform` if available, or `'linux'` if
  114. * not. Setting `platform:'win32'` on non-Windows systems may cause strange
  115. * behavior.
  116. */
  117. platform?: NodeJS.Platform;
  118. /**
  119. * Set to true to call `fs.realpath` on all of the
  120. * results. In the case of an entry that cannot be resolved, the
  121. * entry is omitted. This incurs a slight performance penalty, of
  122. * course, because of the added system calls.
  123. */
  124. realpath?: boolean;
  125. /**
  126. * A [PathScurry](http://npm.im/path-scurry) object used
  127. * to traverse the file system. If the `nocase` option is set
  128. * explicitly, then any provided `scurry` object must match this
  129. * setting.
  130. */
  131. scurry?: PathScurry;
  132. /**
  133. * An AbortSignal which will cancel the Glob walk when
  134. * triggered.
  135. */
  136. signal?: AbortSignal;
  137. /**
  138. * Use `\\` as a path separator _only_, and
  139. * _never_ as an escape character. If set, all `\\` characters are
  140. * replaced with `/` in the pattern.
  141. *
  142. * Note that this makes it **impossible** to match against paths
  143. * containing literal glob pattern characters, but allows matching
  144. * with patterns constructed using `path.join()` and
  145. * `path.resolve()` on Windows platforms, mimicking the (buggy!)
  146. * behavior of Glob v7 and before on Windows. Please use with
  147. * caution, and be mindful of [the caveat below about Windows
  148. * paths](#windows). (For legacy reasons, this is also set if
  149. * `allowWindowsEscape` is set to the exact value `false`.)
  150. */
  151. windowsPathsNoEscape?: boolean;
  152. /**
  153. * Return [PathScurry](http://npm.im/path-scurry)
  154. * `Path` objects instead of strings. These are similar to a
  155. * NodeJS `Dirent` object, but with additional methods and
  156. * properties.
  157. *
  158. * Conflicts with {@link absolute}
  159. */
  160. withFileTypes?: boolean;
  161. }
  162. export type GlobOptionsWithFileTypesTrue = GlobOptions & {
  163. withFileTypes: true;
  164. absolute?: false;
  165. };
  166. export type GlobOptionsWithFileTypesFalse = GlobOptions & {
  167. withFileTypes?: false;
  168. };
  169. export type GlobOptionsWithFileTypesUnset = GlobOptions & {
  170. withFileTypes?: undefined;
  171. };
  172. export type Result<Opts> = Opts extends GlobOptionsWithFileTypesTrue ? Path : Opts extends GlobOptionsWithFileTypesFalse ? string : Opts extends GlobOptionsWithFileTypesUnset ? string : string | Path;
  173. export type Results<Opts> = Result<Opts>[];
  174. export type FileTypes<Opts> = Opts extends GlobOptionsWithFileTypesTrue ? true : Opts extends GlobOptionsWithFileTypesFalse ? false : Opts extends GlobOptionsWithFileTypesUnset ? false : boolean;
  175. /**
  176. * An object that can perform glob pattern traversals.
  177. */
  178. export declare class Glob<Opts extends GlobOptions> implements GlobOptions {
  179. absolute: boolean;
  180. cwd: string;
  181. dot: boolean;
  182. follow: boolean;
  183. ignore?: Ignore;
  184. mark: boolean;
  185. matchBase: boolean;
  186. nobrace: boolean;
  187. nocase: boolean;
  188. nodir: boolean;
  189. noext: boolean;
  190. noglobstar: boolean;
  191. pattern: string[];
  192. platform: NodeJS.Platform;
  193. realpath: boolean;
  194. scurry: PathScurry;
  195. signal?: AbortSignal;
  196. windowsPathsNoEscape: boolean;
  197. withFileTypes: FileTypes<Opts>;
  198. /**
  199. * The options provided to the constructor.
  200. */
  201. opts: Opts;
  202. /**
  203. * An array of parsed immutable {@link Pattern} objects.
  204. */
  205. patterns: Pattern[];
  206. /**
  207. * All options are stored as properties on the `Glob` object.
  208. *
  209. * See {@link GlobOptions} for full options descriptions.
  210. *
  211. * Note that a previous `Glob` object can be passed as the
  212. * `GlobOptions` to another `Glob` instantiation to re-use settings
  213. * and caches with a new pattern.
  214. *
  215. * Traversal functions can be called multiple times to run the walk
  216. * again.
  217. */
  218. constructor(pattern: string | string[], opts: Opts);
  219. /**
  220. * Returns a Promise that resolves to the results array.
  221. */
  222. walk(): Promise<Results<Opts>>;
  223. /**
  224. * synchronous {@link Glob.walk}
  225. */
  226. walkSync(): Results<Opts>;
  227. /**
  228. * Stream results asynchronously.
  229. */
  230. stream(): Minipass<Result<Opts>, Result<Opts>>;
  231. /**
  232. * Stream results synchronously.
  233. */
  234. streamSync(): Minipass<Result<Opts>, Result<Opts>>;
  235. /**
  236. * Default sync iteration function. Returns a Generator that
  237. * iterates over the results.
  238. */
  239. iterateSync(): Generator<Result<Opts>, void, void>;
  240. [Symbol.iterator](): Generator<Result<Opts>, void, void>;
  241. /**
  242. * Default async iteration function. Returns an AsyncGenerator that
  243. * iterates over the results.
  244. */
  245. iterate(): AsyncGenerator<Result<Opts>, void, void>;
  246. [Symbol.asyncIterator](): AsyncGenerator<Result<Opts>, void, void>;
  247. }