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.

307 lines
8.5 KiB

4 years ago
  1. <table><thead>
  2. <tr>
  3. <th>Linux</th>
  4. <th>OS X</th>
  5. <th>Windows</th>
  6. <th>Coverage</th>
  7. <th>Downloads</th>
  8. </tr>
  9. </thead><tbody><tr>
  10. <td colspan="2" align="center">
  11. <a href="https://travis-ci.org/kaelzhang/node-ignore">
  12. <img
  13. src="https://travis-ci.org/kaelzhang/node-ignore.svg?branch=master"
  14. alt="Build Status" /></a>
  15. </td>
  16. <td align="center">
  17. <a href="https://ci.appveyor.com/project/kaelzhang/node-ignore">
  18. <img
  19. src="https://ci.appveyor.com/api/projects/status/github/kaelzhang/node-ignore?branch=master&svg=true"
  20. alt="Windows Build Status" /></a>
  21. </td>
  22. <td align="center">
  23. <a href="https://codecov.io/gh/kaelzhang/node-ignore">
  24. <img
  25. src="https://codecov.io/gh/kaelzhang/node-ignore/branch/master/graph/badge.svg"
  26. alt="Coverage Status" /></a>
  27. </td>
  28. <td align="center">
  29. <a href="https://www.npmjs.org/package/ignore">
  30. <img
  31. src="http://img.shields.io/npm/dm/ignore.svg"
  32. alt="npm module downloads per month" /></a>
  33. </td>
  34. </tr></tbody></table>
  35. # ignore
  36. `ignore` is a manager, filter and parser which implemented in pure JavaScript according to the .gitignore [spec](http://git-scm.com/docs/gitignore).
  37. Pay attention that [`minimatch`](https://www.npmjs.org/package/minimatch) does not work in the gitignore way. To filter filenames according to .gitignore file, I recommend this module.
  38. ##### Tested on
  39. - Linux + Node: `0.8` - `7.x`
  40. - Windows + Node: `0.10` - `7.x`, node < `0.10` is not tested due to the lack of support of appveyor.
  41. Actually, `ignore` does not rely on any versions of node specially.
  42. Since `4.0.0`, ignore will no longer support `node < 6` by default, to use in node < 6, `require('ignore/legacy')`. For details, see [CHANGELOG](https://github.com/kaelzhang/node-ignore/blob/master/CHANGELOG.md).
  43. ## Table Of Main Contents
  44. - [Usage](#usage)
  45. - [`Pathname` Conventions](#pathname-conventions)
  46. - [Guide for 2.x -> 3.x](#upgrade-2x---3x)
  47. - [Guide for 3.x -> 4.x](#upgrade-3x---4x)
  48. - See Also:
  49. - [`glob-gitignore`](https://www.npmjs.com/package/glob-gitignore) matches files using patterns and filters them according to gitignore rules.
  50. ## Usage
  51. ```js
  52. import ignore from 'ignore'
  53. const ig = ignore().add(['.abc/*', '!.abc/d/'])
  54. ```
  55. ### Filter the given paths
  56. ```js
  57. const paths = [
  58. '.abc/a.js', // filtered out
  59. '.abc/d/e.js' // included
  60. ]
  61. ig.filter(paths) // ['.abc/d/e.js']
  62. ig.ignores('.abc/a.js') // true
  63. ```
  64. ### As the filter function
  65. ```js
  66. paths.filter(ig.createFilter()); // ['.abc/d/e.js']
  67. ```
  68. ### Win32 paths will be handled
  69. ```js
  70. ig.filter(['.abc\\a.js', '.abc\\d\\e.js'])
  71. // if the code above runs on windows, the result will be
  72. // ['.abc\\d\\e.js']
  73. ```
  74. ## Why another ignore?
  75. - `ignore` is a standalone module, and is much simpler so that it could easy work with other programs, unlike [isaacs](https://npmjs.org/~isaacs)'s [fstream-ignore](https://npmjs.org/package/fstream-ignore) which must work with the modules of the fstream family.
  76. - `ignore` only contains utility methods to filter paths according to the specified ignore rules, so
  77. - `ignore` never try to find out ignore rules by traversing directories or fetching from git configurations.
  78. - `ignore` don't cares about sub-modules of git projects.
  79. - Exactly according to [gitignore man page](http://git-scm.com/docs/gitignore), fixes some known matching issues of fstream-ignore, such as:
  80. - '`/*.js`' should only match '`a.js`', but not '`abc/a.js`'.
  81. - '`**/foo`' should match '`foo`' anywhere.
  82. - Prevent re-including a file if a parent directory of that file is excluded.
  83. - Handle trailing whitespaces:
  84. - `'a '`(one space) should not match `'a '`(two spaces).
  85. - `'a \ '` matches `'a '`
  86. - All test cases are verified with the result of `git check-ignore`.
  87. # Methods
  88. ## .add(pattern: string | Ignore): this
  89. ## .add(patterns: Array<string | Ignore>): this
  90. - **pattern** `String | Ignore` An ignore pattern string, or the `Ignore` instance
  91. - **patterns** `Array<String | Ignore>` Array of ignore patterns.
  92. Adds a rule or several rules to the current manager.
  93. Returns `this`
  94. Notice that a line starting with `'#'`(hash) is treated as a comment. Put a backslash (`'\'`) in front of the first hash for patterns that begin with a hash, if you want to ignore a file with a hash at the beginning of the filename.
  95. ```js
  96. ignore().add('#abc').ignores('#abc') // false
  97. ignore().add('\#abc').ignores('#abc') // true
  98. ```
  99. `pattern` could either be a line of ignore pattern or a string of multiple ignore patterns, which means we could just `ignore().add()` the content of a ignore file:
  100. ```js
  101. ignore()
  102. .add(fs.readFileSync(filenameOfGitignore).toString())
  103. .filter(filenames)
  104. ```
  105. `pattern` could also be an `ignore` instance, so that we could easily inherit the rules of another `Ignore` instance.
  106. ## <strike>.addIgnoreFile(path)</strike>
  107. REMOVED in `3.x` for now.
  108. To upgrade `ignore@2.x` up to `3.x`, use
  109. ```js
  110. import fs from 'fs'
  111. if (fs.existsSync(filename)) {
  112. ignore().add(fs.readFileSync(filename).toString())
  113. }
  114. ```
  115. instead.
  116. ## .filter(paths: Array<Pathname>): Array<Pathname>
  117. ```ts
  118. type Pathname = string
  119. ```
  120. Filters the given array of pathnames, and returns the filtered array.
  121. - **paths** `Array.<Pathname>` The array of `pathname`s to be filtered.
  122. ### `Pathname` Conventions:
  123. #### 1. `Pathname` should be a `path.relative()`d pathname
  124. `Pathname` should be a string that have been `path.join()`ed, or the return value of `path.relative()` to the current directory.
  125. ```js
  126. // WRONG
  127. ig.ignores('./abc')
  128. // WRONG, for it will never happen.
  129. // If the gitignore rule locates at the root directory,
  130. // `'/abc'` should be changed to `'abc'`.
  131. // ```
  132. // path.relative('/', '/abc') -> 'abc'
  133. // ```
  134. ig.ignores('/abc')
  135. // Right
  136. ig.ignores('abc')
  137. // Right
  138. ig.ignores(path.join('./abc')) // path.join('./abc') -> 'abc'
  139. ```
  140. In other words, each `Pathname` here should be a relative path to the directory of the gitignore rules.
  141. Suppose the dir structure is:
  142. ```
  143. /path/to/your/repo
  144. |-- a
  145. | |-- a.js
  146. |
  147. |-- .b
  148. |
  149. |-- .c
  150. |-- .DS_store
  151. ```
  152. Then the `paths` might be like this:
  153. ```js
  154. [
  155. 'a/a.js'
  156. '.b',
  157. '.c/.DS_store'
  158. ]
  159. ```
  160. Usually, you could use [`glob`](http://npmjs.org/package/glob) with `option.mark = true` to fetch the structure of the current directory:
  161. ```js
  162. import glob from 'glob'
  163. glob('**', {
  164. // Adds a / character to directory matches.
  165. mark: true
  166. }, (err, files) => {
  167. if (err) {
  168. return console.error(err)
  169. }
  170. let filtered = ignore().add(patterns).filter(files)
  171. console.log(filtered)
  172. })
  173. ```
  174. #### 2. filenames and dirnames
  175. `node-ignore` does NO `fs.stat` during path matching, so for the example below:
  176. ```js
  177. ig.add('config/')
  178. // `ig` does NOT know if 'config' is a normal file, directory or something
  179. ig.ignores('config') // And it returns `false`
  180. ig.ignores('config/') // returns `true`
  181. ```
  182. Specially for people who develop some library based on `node-ignore`, it is important to understand that.
  183. ## .ignores(pathname: Pathname): boolean
  184. > new in 3.2.0
  185. Returns `Boolean` whether `pathname` should be ignored.
  186. ```js
  187. ig.ignores('.abc/a.js') // true
  188. ```
  189. ## .createFilter()
  190. Creates a filter function which could filter an array of paths with `Array.prototype.filter`.
  191. Returns `function(path)` the filter function.
  192. ## `options.ignorecase` since 4.0.0
  193. Similar as the `core.ignorecase` option of [git-config](https://git-scm.com/docs/git-config), `node-ignore` will be case insensitive if `options.ignorecase` is set to `true` (default value), otherwise case sensitive.
  194. ```js
  195. const ig = ignore({
  196. ignorecase: false
  197. })
  198. ig.add('*.png')
  199. ig.ignores('*.PNG') // false
  200. ```
  201. ****
  202. # Upgrade Guide
  203. ## Upgrade 2.x -> 3.x
  204. - All `options` of 2.x are unnecessary and removed, so just remove them.
  205. - `ignore()` instance is no longer an [`EventEmitter`](nodejs.org/api/events.html), and all events are unnecessary and removed.
  206. - `.addIgnoreFile()` is removed, see the [.addIgnoreFile](#addignorefilepath) section for details.
  207. ## Upgrade 3.x -> 4.x
  208. Since `4.0.0`, `ignore` will no longer support node < 6, to use `ignore` in node < 6:
  209. ```js
  210. var ignore = require('ignore/legacy')
  211. ```
  212. ****
  213. # Collaborators
  214. - [@whitecolor](https://github.com/whitecolor) *Alex*
  215. - [@SamyPesse](https://github.com/SamyPesse) *Samy Pessé*
  216. - [@azproduction](https://github.com/azproduction) *Mikhail Davydov*
  217. - [@TrySound](https://github.com/TrySound) *Bogdan Chadkin*
  218. - [@JanMattner](https://github.com/JanMattner) *Jan Mattner*
  219. - [@ntwb](https://github.com/ntwb) *Stephen Edgar*
  220. - [@kasperisager](https://github.com/kasperisager) *Kasper Isager*
  221. - [@sandersn](https://github.com/sandersn) *Nathan Shively-Sanders*