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.

156 lines
4.4 KiB

4 years ago
  1. # globby [![Build Status](https://travis-ci.org/sindresorhus/globby.svg?branch=master)](https://travis-ci.org/sindresorhus/globby)
  2. > User-friendly glob matching
  3. Based on [`fast-glob`](https://github.com/mrmlnc/fast-glob), but adds a bunch of useful features and a nicer API.
  4. ## Features
  5. - Promise API
  6. - Multiple patterns
  7. - Negated patterns: `['foo*', '!foobar']`
  8. - Expands directories: `dir``dir/**/*`
  9. - Supports `.gitignore`
  10. ## Install
  11. ```
  12. $ npm install globby
  13. ```
  14. ## Usage
  15. ```
  16. ├── unicorn
  17. ├── cake
  18. └── rainbow
  19. ```
  20. ```js
  21. const globby = require('globby');
  22. (async () => {
  23. const paths = await globby(['*', '!cake']);
  24. console.log(paths);
  25. //=> ['unicorn', 'rainbow']
  26. })();
  27. ```
  28. ## API
  29. ### globby(patterns, [options])
  30. Returns a `Promise<Array>` of matching paths.
  31. #### patterns
  32. Type: `string` `Array`
  33. See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage).
  34. #### options
  35. Type: `Object`
  36. See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options) in addition to the ones below.
  37. ##### expandDirectories
  38. Type: `boolean` `Array` `Object`<br>
  39. Default: `true`
  40. 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 below:
  41. ```js
  42. (async () => {
  43. const paths = await globby('images', {
  44. expandDirectories: {
  45. files: ['cat', 'unicorn', '*.jpg'],
  46. extensions: ['png']
  47. }
  48. });
  49. console.log(paths);
  50. //=> ['cat.png', 'unicorn.png', 'cow.jpg', 'rainbow.jpg']
  51. })();
  52. ```
  53. Note that if you set this option to `false`, you won't get back matched directories unless you set `nodir: false`.
  54. ##### gitignore
  55. Type: `boolean`<br>
  56. Default: `false`
  57. Respect ignore patterns in `.gitignore` files that apply to the globbed files.
  58. ### globby.sync(patterns, [options])
  59. Returns an `Array` of matching paths.
  60. ### globby.generateGlobTasks(patterns, [options])
  61. Returns an `Array<Object>` in the format `{pattern: string, opts: Object}`, which can be passed as arguments to [`fast-glob`](https://github.com/mrmlnc/fast-glob). This is useful for other globbing-related packages.
  62. 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.
  63. ### globby.hasMagic(patterns, [options])
  64. Returns a `boolean` of whether there are any special glob characters in the `patterns`.
  65. 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.
  66. This function is backed by [`node-glob`](https://github.com/isaacs/node-glob#globhasmagicpattern-options)
  67. ### globby.gitignore([options])
  68. Returns a `Promise<(path: string) => boolean>` indicating whether a given path is ignored via a `.gitignore` file.
  69. Takes `cwd?: string` and `ignore?: string[]` as options. `.gitignore` files matched by the ignore config are not
  70. used for the resulting filter function.
  71. ```js
  72. const {gitignore} = require('globby');
  73. (async () => {
  74. const isIgnored = await gitignore();
  75. console.log(isIgnored('some/file'));
  76. })();
  77. ```
  78. ### globby.gitignore.sync([options])
  79. Returns a `(path: string) => boolean` indicating whether a given path is ignored via a `.gitignore` file.
  80. Takes the same options as `globby.gitignore`.
  81. ## Globbing patterns
  82. Just a quick overview.
  83. - `*` matches any number of characters, but not `/`
  84. - `?` matches a single character, but not `/`
  85. - `**` matches any number of characters, including `/`, as long as it's the only thing in a path part
  86. - `{}` allows for a comma-separated list of "or" expressions
  87. - `!` at the beginning of a pattern will negate the match
  88. [Various patterns and expected matches.](https://github.com/sindresorhus/multimatch/blob/master/test/test.js)
  89. ## Related
  90. - [multimatch](https://github.com/sindresorhus/multimatch) - Match against a list instead of the filesystem
  91. - [matcher](https://github.com/sindresorhus/matcher) - Simple wildcard matching
  92. - [del](https://github.com/sindresorhus/del) - Delete files and directories
  93. - [make-dir](https://github.com/sindresorhus/make-dir) - Make a directory and its parents if needed
  94. ## License
  95. MIT © [Sindre Sorhus](https://sindresorhus.com)