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.

262 lines
7.3 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. ## Table Of Main Contents
  43. - [Usage](#usage)
  44. - [Guide for 2.x -> 3.x](#upgrade-2x---3x)
  45. - [Contributing](#contributing)
  46. - Related Packages
  47. - [`glob-gitignore`](https://www.npmjs.com/package/glob-gitignore) matches files using patterns and filters them according to gitignore rules.
  48. ## Usage
  49. ```js
  50. const ignore = require('ignore')
  51. const ig = ignore().add(['.abc/*', '!.abc/d/'])
  52. ```
  53. ### Filter the given paths
  54. ```js
  55. const paths = [
  56. '.abc/a.js', // filtered out
  57. '.abc/d/e.js' // included
  58. ]
  59. ig.filter(paths) // ['.abc/d/e.js']
  60. ig.ignores('.abc/a.js') // true
  61. ```
  62. ### As the filter function
  63. ```js
  64. paths.filter(ig.createFilter()); // ['.abc/d/e.js']
  65. ```
  66. ### Win32 paths will be handled
  67. ```js
  68. ig.filter(['.abc\\a.js', '.abc\\d\\e.js'])
  69. // if the code above runs on windows, the result will be
  70. // ['.abc\\d\\e.js']
  71. ```
  72. ## Why another ignore?
  73. - `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.
  74. - `ignore` only contains utility methods to filter paths according to the specified ignore rules, so
  75. - `ignore` never try to find out ignore rules by traversing directories or fetching from git configurations.
  76. - `ignore` don't cares about sub-modules of git projects.
  77. - Exactly according to [gitignore man page](http://git-scm.com/docs/gitignore), fixes some known matching issues of fstream-ignore, such as:
  78. - '`/*.js`' should only match '`a.js`', but not '`abc/a.js`'.
  79. - '`**/foo`' should match '`foo`' anywhere.
  80. - Prevent re-including a file if a parent directory of that file is excluded.
  81. - Handle trailing whitespaces:
  82. - `'a '`(one space) should not match `'a '`(two spaces).
  83. - `'a \ '` matches `'a '`
  84. - All test cases are verified with the result of `git check-ignore`.
  85. ## Methods
  86. ### .add(pattern)
  87. ### .add(patterns)
  88. - **pattern** `String|Ignore` An ignore pattern string, or the `Ignore` instance
  89. - **patterns** `Array.<pattern>` Array of ignore patterns.
  90. Adds a rule or several rules to the current manager.
  91. Returns `this`
  92. 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.
  93. ```js
  94. ignore().add('#abc').ignores('#abc') // false
  95. ignore().add('\#abc').ignores('#abc') // true
  96. ```
  97. `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:
  98. ```js
  99. ignore()
  100. .add(fs.readFileSync(filenameOfGitignore).toString())
  101. .filter(filenames)
  102. ```
  103. `pattern` could also be an `ignore` instance, so that we could easily inherit the rules of another `Ignore` instance.
  104. ### <strike>.addIgnoreFile(path)</strike>
  105. REMOVED in `3.x` for now.
  106. To upgrade `ignore@2.x` up to `3.x`, use
  107. ```js
  108. const fs = require('fs')
  109. if (fs.existsSync(filename)) {
  110. ignore().add(fs.readFileSync(filename).toString())
  111. }
  112. ```
  113. instead.
  114. ### .ignores(pathname)
  115. > new in 3.2.0
  116. Returns `Boolean` whether `pathname` should be ignored.
  117. ```js
  118. ig.ignores('.abc/a.js') // true
  119. ```
  120. ### .filter(paths)
  121. Filters the given array of pathnames, and returns the filtered array.
  122. - **paths** `Array.<path>` The array of `pathname`s to be filtered.
  123. **NOTICE** that:
  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 git ignore 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. const glob = require('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. ### .createFilter()
  175. Creates a filter function which could filter an array of paths with `Array.prototype.filter`.
  176. Returns `function(path)` the filter function.
  177. ****
  178. ## Upgrade 2.x -> 3.x
  179. - All `options` of 2.x are unnecessary and removed, so just remove them.
  180. - `ignore()` instance is no longer an [`EventEmitter`](nodejs.org/api/events.html), and all events are unnecessary and removed.
  181. - `.addIgnoreFile()` is removed, see the [.addIgnoreFile](#addignorefilepath) section for details.
  182. ****
  183. ## Contributing
  184. The code of `node-ignore` is based on es6 and babel, but babel and its preset is not included in the `dependencies` field of package.json, so that the installation process of test cases will not fail in older versions of node.
  185. So use `bash install.sh` to install dependencies and `bash test.sh` to run test cases in your local machine.
  186. #### Collaborators
  187. - [SamyPesse](https://github.com/SamyPesse) *Samy Pessé*
  188. - [azproduction](https://github.com/azproduction) *Mikhail Davydov*
  189. - [TrySound](https://github.com/TrySound) *Bogdan Chadkin*
  190. - [JanMattner](https://github.com/JanMattner) *Jan Mattner*