You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

830 lines
26 KiB

1 month ago
  1. # fast-glob
  2. > It's a very fast and efficient [glob][glob_definition] library for [Node.js][node_js].
  3. This package provides methods for traversing the file system and returning pathnames that matched a defined set of a specified pattern according to the rules used by the Unix Bash shell with some simplifications, meanwhile results are returned in **arbitrary order**. Quick, simple, effective.
  4. ## Table of Contents
  5. <details>
  6. <summary><strong>Details</strong></summary>
  7. * [Highlights](#highlights)
  8. * [Old and modern mode](#old-and-modern-mode)
  9. * [Pattern syntax](#pattern-syntax)
  10. * [Basic syntax](#basic-syntax)
  11. * [Advanced syntax](#advanced-syntax)
  12. * [Installation](#installation)
  13. * [API](#api)
  14. * [Asynchronous](#asynchronous)
  15. * [Synchronous](#synchronous)
  16. * [Stream](#stream)
  17. * [patterns](#patterns)
  18. * [[options]](#options)
  19. * [Helpers](#helpers)
  20. * [generateTasks](#generatetaskspatterns-options)
  21. * [isDynamicPattern](#isdynamicpatternpattern-options)
  22. * [escapePath](#escapepathpath)
  23. * [convertPathToPattern](#convertpathtopatternpath)
  24. * [Options](#options-3)
  25. * [Common](#common)
  26. * [concurrency](#concurrency)
  27. * [cwd](#cwd)
  28. * [deep](#deep)
  29. * [followSymbolicLinks](#followsymboliclinks)
  30. * [fs](#fs)
  31. * [ignore](#ignore)
  32. * [suppressErrors](#suppresserrors)
  33. * [throwErrorOnBrokenSymbolicLink](#throwerroronbrokensymboliclink)
  34. * [Output control](#output-control)
  35. * [absolute](#absolute)
  36. * [markDirectories](#markdirectories)
  37. * [objectMode](#objectmode)
  38. * [onlyDirectories](#onlydirectories)
  39. * [onlyFiles](#onlyfiles)
  40. * [stats](#stats)
  41. * [unique](#unique)
  42. * [Matching control](#matching-control)
  43. * [braceExpansion](#braceexpansion)
  44. * [caseSensitiveMatch](#casesensitivematch)
  45. * [dot](#dot)
  46. * [extglob](#extglob)
  47. * [globstar](#globstar)
  48. * [baseNameMatch](#basenamematch)
  49. * [FAQ](#faq)
  50. * [What is a static or dynamic pattern?](#what-is-a-static-or-dynamic-pattern)
  51. * [How to write patterns on Windows?](#how-to-write-patterns-on-windows)
  52. * [Why are parentheses match wrong?](#why-are-parentheses-match-wrong)
  53. * [How to exclude directory from reading?](#how-to-exclude-directory-from-reading)
  54. * [How to use UNC path?](#how-to-use-unc-path)
  55. * [Compatible with `node-glob`?](#compatible-with-node-glob)
  56. * [Benchmarks](#benchmarks)
  57. * [Server](#server)
  58. * [Nettop](#nettop)
  59. * [Changelog](#changelog)
  60. * [License](#license)
  61. </details>
  62. ## Highlights
  63. * Fast. Probably the fastest.
  64. * Supports multiple and negative patterns.
  65. * Synchronous, Promise and Stream API.
  66. * Object mode. Can return more than just strings.
  67. * Error-tolerant.
  68. ## Old and modern mode
  69. This package works in two modes, depending on the environment in which it is used.
  70. * **Old mode**. Node.js below 10.10 or when the [`stats`](#stats) option is *enabled*.
  71. * **Modern mode**. Node.js 10.10+ and the [`stats`](#stats) option is *disabled*.
  72. The modern mode is faster. Learn more about the [internal mechanism][nodelib_fs_scandir_old_and_modern_modern].
  73. ## Pattern syntax
  74. > :warning: Always use forward-slashes in glob expressions (patterns and [`ignore`](#ignore) option). Use backslashes for escaping characters.
  75. There is more than one form of syntax: basic and advanced. Below is a brief overview of the supported features. Also pay attention to our [FAQ](#faq).
  76. > :book: This package uses [`micromatch`][micromatch] as a library for pattern matching.
  77. ### Basic syntax
  78. * An asterisk (`*`) — matches everything except slashes (path separators), hidden files (names starting with `.`).
  79. * A double star or globstar (`**`) — matches zero or more directories.
  80. * Question mark (`?`) – matches any single character except slashes (path separators).
  81. * Sequence (`[seq]`) — matches any character in sequence.
  82. > :book: A few additional words about the [basic matching behavior][picomatch_matching_behavior].
  83. Some examples:
  84. * `src/**/*.js` — matches all files in the `src` directory (any level of nesting) that have the `.js` extension.
  85. * `src/*.??` — matches all files in the `src` directory (only first level of nesting) that have a two-character extension.
  86. * `file-[01].js` — matches files: `file-0.js`, `file-1.js`.
  87. ### Advanced syntax
  88. * [Escapes characters][micromatch_backslashes] (`\\`) — matching special characters (`$^*+?()[]`) as literals.
  89. * [POSIX character classes][picomatch_posix_brackets] (`[[:digit:]]`).
  90. * [Extended globs][micromatch_extglobs] (`?(pattern-list)`).
  91. * [Bash style brace expansions][micromatch_braces] (`{}`).
  92. * [Regexp character classes][micromatch_regex_character_classes] (`[1-5]`).
  93. * [Regex groups][regular_expressions_brackets] (`(a|b)`).
  94. > :book: A few additional words about the [advanced matching behavior][micromatch_extended_globbing].
  95. Some examples:
  96. * `src/**/*.{css,scss}` — matches all files in the `src` directory (any level of nesting) that have the `.css` or `.scss` extension.
  97. * `file-[[:digit:]].js` — matches files: `file-0.js`, `file-1.js`, …, `file-9.js`.
  98. * `file-{1..3}.js` — matches files: `file-1.js`, `file-2.js`, `file-3.js`.
  99. * `file-(1|2)` — matches files: `file-1.js`, `file-2.js`.
  100. ## Installation
  101. ```console
  102. npm install fast-glob
  103. ```
  104. ## API
  105. ### Asynchronous
  106. ```js
  107. fg(patterns, [options])
  108. fg.async(patterns, [options])
  109. fg.glob(patterns, [options])
  110. ```
  111. Returns a `Promise` with an array of matching entries.
  112. ```js
  113. const fg = require('fast-glob');
  114. const entries = await fg(['.editorconfig', '**/index.js'], { dot: true });
  115. // ['.editorconfig', 'services/index.js']
  116. ```
  117. ### Synchronous
  118. ```js
  119. fg.sync(patterns, [options])
  120. fg.globSync(patterns, [options])
  121. ```
  122. Returns an array of matching entries.
  123. ```js
  124. const fg = require('fast-glob');
  125. const entries = fg.sync(['.editorconfig', '**/index.js'], { dot: true });
  126. // ['.editorconfig', 'services/index.js']
  127. ```
  128. ### Stream
  129. ```js
  130. fg.stream(patterns, [options])
  131. fg.globStream(patterns, [options])
  132. ```
  133. Returns a [`ReadableStream`][node_js_stream_readable_streams] when the `data` event will be emitted with matching entry.
  134. ```js
  135. const fg = require('fast-glob');
  136. const stream = fg.stream(['.editorconfig', '**/index.js'], { dot: true });
  137. for await (const entry of stream) {
  138. // .editorconfig
  139. // services/index.js
  140. }
  141. ```
  142. #### patterns
  143. * Required: `true`
  144. * Type: `string | string[]`
  145. Any correct pattern(s).
  146. > :1234: [Pattern syntax](#pattern-syntax)
  147. >
  148. > :warning: This package does not respect the order of patterns. First, all the negative patterns are applied, and only then the positive patterns. If you want to get a certain order of records, use sorting or split calls.
  149. #### [options]
  150. * Required: `false`
  151. * Type: [`Options`](#options-3)
  152. See [Options](#options-3) section.
  153. ### Helpers
  154. #### `generateTasks(patterns, [options])`
  155. Returns the internal representation of patterns ([`Task`](./src/managers/tasks.ts) is a combining patterns by base directory).
  156. ```js
  157. fg.generateTasks('*');
  158. [{
  159. base: '.', // Parent directory for all patterns inside this task
  160. dynamic: true, // Dynamic or static patterns are in this task
  161. patterns: ['*'],
  162. positive: ['*'],
  163. negative: []
  164. }]
  165. ```
  166. ##### patterns
  167. * Required: `true`
  168. * Type: `string | string[]`
  169. Any correct pattern(s).
  170. ##### [options]
  171. * Required: `false`
  172. * Type: [`Options`](#options-3)
  173. See [Options](#options-3) section.
  174. #### `isDynamicPattern(pattern, [options])`
  175. Returns `true` if the passed pattern is a dynamic pattern.
  176. > :1234: [What is a static or dynamic pattern?](#what-is-a-static-or-dynamic-pattern)
  177. ```js
  178. fg.isDynamicPattern('*'); // true
  179. fg.isDynamicPattern('abc'); // false
  180. ```
  181. ##### pattern
  182. * Required: `true`
  183. * Type: `string`
  184. Any correct pattern.
  185. ##### [options]
  186. * Required: `false`
  187. * Type: [`Options`](#options-3)
  188. See [Options](#options-3) section.
  189. #### `escapePath(path)`
  190. Returns the path with escaped special characters depending on the platform.
  191. * Posix:
  192. * `*?|(){}[]`;
  193. * `!` at the beginning of line;
  194. * `@+!` before the opening parenthesis;
  195. * `\\` before non-special characters;
  196. * Windows:
  197. * `(){}[]`
  198. * `!` at the beginning of line;
  199. * `@+!` before the opening parenthesis;
  200. * Characters like `*?|` cannot be used in the path ([windows_naming_conventions][windows_naming_conventions]), so they will not be escaped;
  201. ```js
  202. fg.escapePath('!abc');
  203. // \\!abc
  204. fg.escapePath('[OpenSource] mrmlnc – fast-glob (Deluxe Edition) 2014') + '/*.flac'
  205. // \\[OpenSource\\] mrmlnc – fast-glob \\(Deluxe Edition\\) 2014/*.flac
  206. fg.posix.escapePath('C:\\Program Files (x86)\\**\\*');
  207. // C:\\\\Program Files \\(x86\\)\\*\\*\\*
  208. fg.win32.escapePath('C:\\Program Files (x86)\\**\\*');
  209. // Windows: C:\\Program Files \\(x86\\)\\**\\*
  210. ```
  211. #### `convertPathToPattern(path)`
  212. Converts a path to a pattern depending on the platform, including special character escaping.
  213. * Posix. Works similarly to the `fg.posix.escapePath` method.
  214. * Windows. Works similarly to the `fg.win32.escapePath` method, additionally converting backslashes to forward slashes in cases where they are not escape characters (`!()+@{}[]`).
  215. ```js
  216. fg.convertPathToPattern('[OpenSource] mrmlnc – fast-glob (Deluxe Edition) 2014') + '/*.flac';
  217. // \\[OpenSource\\] mrmlnc – fast-glob \\(Deluxe Edition\\) 2014/*.flac
  218. fg.convertPathToPattern('C:/Program Files (x86)/**/*');
  219. // Posix: C:/Program Files \\(x86\\)/\\*\\*/\\*
  220. // Windows: C:/Program Files \\(x86\\)/**/*
  221. fg.convertPathToPattern('C:\\Program Files (x86)\\**\\*');
  222. // Posix: C:\\\\Program Files \\(x86\\)\\*\\*\\*
  223. // Windows: C:/Program Files \\(x86\\)/**/*
  224. fg.posix.convertPathToPattern('\\\\?\\c:\\Program Files (x86)') + '/**/*';
  225. // Posix: \\\\\\?\\\\c:\\\\Program Files \\(x86\\)/**/* (broken pattern)
  226. fg.win32.convertPathToPattern('\\\\?\\c:\\Program Files (x86)') + '/**/*';
  227. // Windows: //?/c:/Program Files \\(x86\\)/**/*
  228. ```
  229. ## Options
  230. ### Common options
  231. #### concurrency
  232. * Type: `number`
  233. * Default: `os.cpus().length`
  234. Specifies the maximum number of concurrent requests from a reader to read directories.
  235. > :book: The higher the number, the higher the performance and load on the file system. If you want to read in quiet mode, set the value to a comfortable number or `1`.
  236. <details>
  237. <summary>More details</summary>
  238. In Node, there are [two types of threads][nodejs_thread_pool]: Event Loop (code) and a Thread Pool (fs, dns, …). The thread pool size controlled by the `UV_THREADPOOL_SIZE` environment variable. Its default size is 4 ([documentation][libuv_thread_pool]). The pool is one for all tasks within a single Node process.
  239. Any code can make 4 real concurrent accesses to the file system. The rest of the FS requests will wait in the queue.
  240. > :book: Each new instance of FG in the same Node process will use the same Thread pool.
  241. But this package also has the `concurrency` option. This option allows you to control the number of concurrent accesses to the FS at the package level. By default, this package has a value equal to the number of cores available for the current Node process. This allows you to set a value smaller than the pool size (`concurrency: 1`) or, conversely, to prepare tasks for the pool queue more quickly (`concurrency: Number.POSITIVE_INFINITY`).
  242. So, in fact, this package can **only make 4 concurrent requests to the FS**. You can increase this value by using an environment variable (`UV_THREADPOOL_SIZE`), but in practice this does not give a multiple advantage.
  243. </details>
  244. #### cwd
  245. * Type: `string`
  246. * Default: `process.cwd()`
  247. The current working directory in which to search.
  248. #### deep
  249. * Type: `number`
  250. * Default: `Infinity`
  251. Specifies the maximum depth of a read directory relative to the start directory.
  252. For example, you have the following tree:
  253. ```js
  254. dir/
  255. └── one/ // 1
  256. └── two/ // 2
  257. └── file.js // 3
  258. ```
  259. ```js
  260. // With base directory
  261. fg.sync('dir/**', { onlyFiles: false, deep: 1 }); // ['dir/one']
  262. fg.sync('dir/**', { onlyFiles: false, deep: 2 }); // ['dir/one', 'dir/one/two']
  263. // With cwd option
  264. fg.sync('**', { onlyFiles: false, cwd: 'dir', deep: 1 }); // ['one']
  265. fg.sync('**', { onlyFiles: false, cwd: 'dir', deep: 2 }); // ['one', 'one/two']
  266. ```
  267. > :book: If you specify a pattern with some base directory, this directory will not participate in the calculation of the depth of the found directories. Think of it as a [`cwd`](#cwd) option.
  268. #### followSymbolicLinks
  269. * Type: `boolean`
  270. * Default: `true`
  271. Indicates whether to traverse descendants of symbolic link directories when expanding `**` patterns.
  272. > :book: Note that this option does not affect the base directory of the pattern. For example, if `./a` is a symlink to directory `./b` and you specified `['./a**', './b/**']` patterns, then directory `./a` will still be read.
  273. > :book: If the [`stats`](#stats) option is specified, the information about the symbolic link (`fs.lstat`) will be replaced with information about the entry (`fs.stat`) behind it.
  274. #### fs
  275. * Type: `FileSystemAdapter`
  276. * Default: `fs.*`
  277. Custom implementation of methods for working with the file system. Supports objects with enumerable properties only.
  278. ```ts
  279. export interface FileSystemAdapter {
  280. lstat?: typeof fs.lstat;
  281. stat?: typeof fs.stat;
  282. lstatSync?: typeof fs.lstatSync;
  283. statSync?: typeof fs.statSync;
  284. readdir?: typeof fs.readdir;
  285. readdirSync?: typeof fs.readdirSync;
  286. }
  287. ```
  288. #### ignore
  289. * Type: `string[]`
  290. * Default: `[]`
  291. An array of glob patterns to exclude matches. This is an alternative way to use negative patterns.
  292. ```js
  293. dir/
  294. ├── package-lock.json
  295. └── package.json
  296. ```
  297. ```js
  298. fg.sync(['*.json', '!package-lock.json']); // ['package.json']
  299. fg.sync('*.json', { ignore: ['package-lock.json'] }); // ['package.json']
  300. ```
  301. #### suppressErrors
  302. * Type: `boolean`
  303. * Default: `false`
  304. By default this package suppress only `ENOENT` errors. Set to `true` to suppress any error.
  305. > :book: Can be useful when the directory has entries with a special level of access.
  306. #### throwErrorOnBrokenSymbolicLink
  307. * Type: `boolean`
  308. * Default: `false`
  309. Throw an error when symbolic link is broken if `true` or safely return `lstat` call if `false`.
  310. > :book: This option has no effect on errors when reading the symbolic link directory.
  311. ### Output control
  312. #### absolute
  313. * Type: `boolean`
  314. * Default: `false`
  315. Return the absolute path for entries.
  316. ```js
  317. fg.sync('*.js', { absolute: false }); // ['index.js']
  318. fg.sync('*.js', { absolute: true }); // ['/home/user/index.js']
  319. ```
  320. > :book: This option is required if you want to use negative patterns with absolute path, for example, `!${__dirname}/*.js`.
  321. #### markDirectories
  322. * Type: `boolean`
  323. * Default: `false`
  324. Mark the directory path with the final slash.
  325. ```js
  326. fg.sync('*', { onlyFiles: false, markDirectories: false }); // ['index.js', 'controllers']
  327. fg.sync('*', { onlyFiles: false, markDirectories: true }); // ['index.js', 'controllers/']
  328. ```
  329. #### objectMode
  330. * Type: `boolean`
  331. * Default: `false`
  332. Returns objects (instead of strings) describing entries.
  333. ```js
  334. fg.sync('*', { objectMode: false }); // ['src/index.js']
  335. fg.sync('*', { objectMode: true }); // [{ name: 'index.js', path: 'src/index.js', dirent: <fs.Dirent> }]
  336. ```
  337. The object has the following fields:
  338. * name (`string`) — the last part of the path (basename)
  339. * path (`string`) — full path relative to the pattern base directory
  340. * dirent ([`fs.Dirent`][node_js_fs_class_fs_dirent]) — instance of `fs.Dirent`
  341. > :book: An object is an internal representation of entry, so getting it does not affect performance.
  342. #### onlyDirectories
  343. * Type: `boolean`
  344. * Default: `false`
  345. Return only directories.
  346. ```js
  347. fg.sync('*', { onlyDirectories: false }); // ['index.js', 'src']
  348. fg.sync('*', { onlyDirectories: true }); // ['src']
  349. ```
  350. > :book: If `true`, the [`onlyFiles`](#onlyfiles) option is automatically `false`.
  351. #### onlyFiles
  352. * Type: `boolean`
  353. * Default: `true`
  354. Return only files.
  355. ```js
  356. fg.sync('*', { onlyFiles: false }); // ['index.js', 'src']
  357. fg.sync('*', { onlyFiles: true }); // ['index.js']
  358. ```
  359. #### stats
  360. * Type: `boolean`
  361. * Default: `false`
  362. Enables an [object mode](#objectmode) with an additional field:
  363. * stats ([`fs.Stats`][node_js_fs_class_fs_stats]) — instance of `fs.Stats`
  364. ```js
  365. fg.sync('*', { stats: false }); // ['src/index.js']
  366. fg.sync('*', { stats: true }); // [{ name: 'index.js', path: 'src/index.js', dirent: <fs.Dirent>, stats: <fs.Stats> }]
  367. ```
  368. > :book: Returns `fs.stat` instead of `fs.lstat` for symbolic links when the [`followSymbolicLinks`](#followsymboliclinks) option is specified.
  369. >
  370. > :warning: Unlike [object mode](#objectmode) this mode requires additional calls to the file system. On average, this mode is slower at least twice. See [old and modern mode](#old-and-modern-mode) for more details.
  371. #### unique
  372. * Type: `boolean`
  373. * Default: `true`
  374. Ensures that the returned entries are unique.
  375. ```js
  376. fg.sync(['*.json', 'package.json'], { unique: false }); // ['package.json', 'package.json']
  377. fg.sync(['*.json', 'package.json'], { unique: true }); // ['package.json']
  378. ```
  379. If `true` and similar entries are found, the result is the first found.
  380. ### Matching control
  381. #### braceExpansion
  382. * Type: `boolean`
  383. * Default: `true`
  384. Enables Bash-like brace expansion.
  385. > :1234: [Syntax description][bash_hackers_syntax_expansion_brace] or more [detailed description][micromatch_braces].
  386. ```js
  387. dir/
  388. ├── abd
  389. ├── acd
  390. └── a{b,c}d
  391. ```
  392. ```js
  393. fg.sync('a{b,c}d', { braceExpansion: false }); // ['a{b,c}d']
  394. fg.sync('a{b,c}d', { braceExpansion: true }); // ['abd', 'acd']
  395. ```
  396. #### caseSensitiveMatch
  397. * Type: `boolean`
  398. * Default: `true`
  399. Enables a [case-sensitive][wikipedia_case_sensitivity] mode for matching files.
  400. ```js
  401. dir/
  402. ├── file.txt
  403. └── File.txt
  404. ```
  405. ```js
  406. fg.sync('file.txt', { caseSensitiveMatch: false }); // ['file.txt', 'File.txt']
  407. fg.sync('file.txt', { caseSensitiveMatch: true }); // ['file.txt']
  408. ```
  409. #### dot
  410. * Type: `boolean`
  411. * Default: `false`
  412. Allow patterns to match entries that begin with a period (`.`).
  413. > :book: Note that an explicit dot in a portion of the pattern will always match dot files.
  414. ```js
  415. dir/
  416. ├── .editorconfig
  417. └── package.json
  418. ```
  419. ```js
  420. fg.sync('*', { dot: false }); // ['package.json']
  421. fg.sync('*', { dot: true }); // ['.editorconfig', 'package.json']
  422. ```
  423. #### extglob
  424. * Type: `boolean`
  425. * Default: `true`
  426. Enables Bash-like `extglob` functionality.
  427. > :1234: [Syntax description][micromatch_extglobs].
  428. ```js
  429. dir/
  430. ├── README.md
  431. └── package.json
  432. ```
  433. ```js
  434. fg.sync('*.+(json|md)', { extglob: false }); // []
  435. fg.sync('*.+(json|md)', { extglob: true }); // ['README.md', 'package.json']
  436. ```
  437. #### globstar
  438. * Type: `boolean`
  439. * Default: `true`
  440. Enables recursively repeats a pattern containing `**`. If `false`, `**` behaves exactly like `*`.
  441. ```js
  442. dir/
  443. └── a
  444. └── b
  445. ```
  446. ```js
  447. fg.sync('**', { onlyFiles: false, globstar: false }); // ['a']
  448. fg.sync('**', { onlyFiles: false, globstar: true }); // ['a', 'a/b']
  449. ```
  450. #### baseNameMatch
  451. * Type: `boolean`
  452. * Default: `false`
  453. If set to `true`, then patterns without slashes will be matched against the basename of the path if it contains slashes.
  454. ```js
  455. dir/
  456. └── one/
  457. └── file.md
  458. ```
  459. ```js
  460. fg.sync('*.md', { baseNameMatch: false }); // []
  461. fg.sync('*.md', { baseNameMatch: true }); // ['one/file.md']
  462. ```
  463. ## FAQ
  464. ## What is a static or dynamic pattern?
  465. All patterns can be divided into two types:
  466. * **static**. A pattern is considered static if it can be used to get an entry on the file system without using matching mechanisms. For example, the `file.js` pattern is a static pattern because we can just verify that it exists on the file system.
  467. * **dynamic**. A pattern is considered dynamic if it cannot be used directly to find occurrences without using a matching mechanisms. For example, the `*` pattern is a dynamic pattern because we cannot use this pattern directly.
  468. A pattern is considered dynamic if it contains the following characters (`…` — any characters or their absence) or options:
  469. * The [`caseSensitiveMatch`](#casesensitivematch) option is disabled
  470. * `\\` (the escape character)
  471. * `*`, `?`, `!` (at the beginning of line)
  472. * `[…]`
  473. * `(…|…)`
  474. * `@(…)`, `!(…)`, `*(…)`, `?(…)`, `+(…)` (respects the [`extglob`](#extglob) option)
  475. * `{…,…}`, `{…..…}` (respects the [`braceExpansion`](#braceexpansion) option)
  476. ## How to write patterns on Windows?
  477. Always use forward-slashes in glob expressions (patterns and [`ignore`](#ignore) option). Use backslashes for escaping characters. With the [`cwd`](#cwd) option use a convenient format.
  478. **Bad**
  479. ```ts
  480. [
  481. 'directory\\*',
  482. path.join(process.cwd(), '**')
  483. ]
  484. ```
  485. **Good**
  486. ```ts
  487. [
  488. 'directory/*',
  489. fg.convertPathToPattern(process.cwd()) + '/**'
  490. ]
  491. ```
  492. > :book: Use the [`.convertPathToPattern`](#convertpathtopatternpath) package to convert Windows-style path to a Unix-style path.
  493. Read more about [matching with backslashes][micromatch_backslashes].
  494. ## Why are parentheses match wrong?
  495. ```js
  496. dir/
  497. └── (special-*file).txt
  498. ```
  499. ```js
  500. fg.sync(['(special-*file).txt']) // []
  501. ```
  502. Refers to Bash. You need to escape special characters:
  503. ```js
  504. fg.sync(['\\(special-*file\\).txt']) // ['(special-*file).txt']
  505. ```
  506. Read more about [matching special characters as literals][picomatch_matching_special_characters_as_literals]. Or use the [`.escapePath`](#escapepathpath).
  507. ## How to exclude directory from reading?
  508. You can use a negative pattern like this: `!**/node_modules` or `!**/node_modules/**`. Also you can use [`ignore`](#ignore) option. Just look at the example below.
  509. ```js
  510. first/
  511. ├── file.md
  512. └── second/
  513. └── file.txt
  514. ```
  515. If you don't want to read the `second` directory, you must write the following pattern: `!**/second` or `!**/second/**`.
  516. ```js
  517. fg.sync(['**/*.md', '!**/second']); // ['first/file.md']
  518. fg.sync(['**/*.md'], { ignore: ['**/second/**'] }); // ['first/file.md']
  519. ```
  520. > :warning: When you write `!**/second/**/*` it means that the directory will be **read**, but all the entries will not be included in the results.
  521. You have to understand that if you write the pattern to exclude directories, then the directory will not be read under any circumstances.
  522. ## How to use UNC path?
  523. You cannot use [Uniform Naming Convention (UNC)][unc_path] paths as patterns (due to syntax) directly, but you can use them as [`cwd`](#cwd) directory or use the `fg.convertPathToPattern` method.
  524. ```ts
  525. // cwd
  526. fg.sync('*', { cwd: '\\\\?\\C:\\Python27' /* or //?/C:/Python27 */ });
  527. fg.sync('Python27/*', { cwd: '\\\\?\\C:\\' /* or //?/C:/ */ });
  528. // .convertPathToPattern
  529. fg.sync(fg.convertPathToPattern('\\\\?\\c:\\Python27') + '/*');
  530. ```
  531. ## Compatible with `node-glob`?
  532. | node-glob | fast-glob |
  533. | :----------: | :-------: |
  534. | `cwd` | [`cwd`](#cwd) |
  535. | `root` | – |
  536. | `dot` | [`dot`](#dot) |
  537. | `nomount` | – |
  538. | `mark` | [`markDirectories`](#markdirectories) |
  539. | `nosort` | – |
  540. | `nounique` | [`unique`](#unique) |
  541. | `nobrace` | [`braceExpansion`](#braceexpansion) |
  542. | `noglobstar` | [`globstar`](#globstar) |
  543. | `noext` | [`extglob`](#extglob) |
  544. | `nocase` | [`caseSensitiveMatch`](#casesensitivematch) |
  545. | `matchBase` | [`baseNameMatch`](#basenamematch) |
  546. | `nodir` | [`onlyFiles`](#onlyfiles) |
  547. | `ignore` | [`ignore`](#ignore) |
  548. | `follow` | [`followSymbolicLinks`](#followsymboliclinks) |
  549. | `realpath` | – |
  550. | `absolute` | [`absolute`](#absolute) |
  551. ## Benchmarks
  552. You can see results [here](https://github.com/mrmlnc/fast-glob/actions/workflows/benchmark.yml?query=branch%3Amaster) for every commit into the `main` branch.
  553. * **Product benchmark** – comparison with the main competitors.
  554. * **Regress benchmark** – regression between the current version and the version from the npm registry.
  555. ## Changelog
  556. See the [Releases section of our GitHub project][github_releases] for changelog for each release version.
  557. ## License
  558. This software is released under the terms of the MIT license.
  559. [bash_hackers_syntax_expansion_brace]: https://wiki.bash-hackers.org/syntax/expansion/brace
  560. [github_releases]: https://github.com/mrmlnc/fast-glob/releases
  561. [glob_definition]: https://en.wikipedia.org/wiki/Glob_(programming)
  562. [glob_linux_man]: http://man7.org/linux/man-pages/man3/glob.3.html
  563. [micromatch_backslashes]: https://github.com/micromatch/micromatch#backslashes
  564. [micromatch_braces]: https://github.com/micromatch/braces
  565. [micromatch_extended_globbing]: https://github.com/micromatch/micromatch#extended-globbing
  566. [micromatch_extglobs]: https://github.com/micromatch/micromatch#extglobs
  567. [micromatch_regex_character_classes]: https://github.com/micromatch/micromatch#regex-character-classes
  568. [micromatch]: https://github.com/micromatch/micromatch
  569. [node_js_fs_class_fs_dirent]: https://nodejs.org/api/fs.html#fs_class_fs_dirent
  570. [node_js_fs_class_fs_stats]: https://nodejs.org/api/fs.html#fs_class_fs_stats
  571. [node_js_stream_readable_streams]: https://nodejs.org/api/stream.html#stream_readable_streams
  572. [node_js]: https://nodejs.org/en
  573. [nodelib_fs_scandir_old_and_modern_modern]: https://github.com/nodelib/nodelib/blob/master/packages/fs/fs.scandir/README.md#old-and-modern-mode
  574. [npm_normalize_path]: https://www.npmjs.com/package/normalize-path
  575. [npm_unixify]: https://www.npmjs.com/package/unixify
  576. [picomatch_matching_behavior]: https://github.com/micromatch/picomatch#matching-behavior-vs-bash
  577. [picomatch_matching_special_characters_as_literals]: https://github.com/micromatch/picomatch#matching-special-characters-as-literals
  578. [picomatch_posix_brackets]: https://github.com/micromatch/picomatch#posix-brackets
  579. [regular_expressions_brackets]: https://www.regular-expressions.info/brackets.html
  580. [unc_path]: https://learn.microsoft.com/openspecs/windows_protocols/ms-dtyp/62e862f4-2a51-452e-8eeb-dc4ff5ee33cc
  581. [wikipedia_case_sensitivity]: https://en.wikipedia.org/wiki/Case_sensitivity
  582. [nodejs_thread_pool]: https://nodejs.org/en/docs/guides/dont-block-the-event-loop
  583. [libuv_thread_pool]: http://docs.libuv.org/en/v1.x/threadpool.html
  584. [windows_naming_conventions]: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions