租房小程序前端代码
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.

412 lines
12 KiB

2 months 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://github.com/kaelzhang/node-ignore/actions/workflows/nodejs.yml">
  12. <img
  13. src="https://github.com/kaelzhang/node-ignore/actions/workflows/nodejs.yml/badge.svg"
  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 2.22.1](http://git-scm.com/docs/gitignore).
  37. `ignore` is used by eslint, gitbook and [many others](https://www.npmjs.com/browse/depended/ignore).
  38. Pay **ATTENTION** that [`minimatch`](https://www.npmjs.org/package/minimatch) (which used by `fstream-ignore`) does not follow the gitignore spec.
  39. To filter filenames according to a .gitignore file, I recommend this npm package, `ignore`.
  40. To parse an `.npmignore` file, you should use `minimatch`, because an `.npmignore` file is parsed by npm using `minimatch` and it does not work in the .gitignore way.
  41. ### Tested on
  42. `ignore` is fully tested, and has more than **five hundreds** of unit tests.
  43. - Linux + Node: `0.8` - `7.x`
  44. - Windows + Node: `0.10` - `7.x`, node < `0.10` is not tested due to the lack of support of appveyor.
  45. Actually, `ignore` does not rely on any versions of node specially.
  46. 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).
  47. ## Table Of Main Contents
  48. - [Usage](#usage)
  49. - [`Pathname` Conventions](#pathname-conventions)
  50. - See Also:
  51. - [`glob-gitignore`](https://www.npmjs.com/package/glob-gitignore) matches files using patterns and filters them according to gitignore rules.
  52. - [Upgrade Guide](#upgrade-guide)
  53. ## Install
  54. ```sh
  55. npm i ignore
  56. ```
  57. ## Usage
  58. ```js
  59. import ignore from 'ignore'
  60. const ig = ignore().add(['.abc/*', '!.abc/d/'])
  61. ```
  62. ### Filter the given paths
  63. ```js
  64. const paths = [
  65. '.abc/a.js', // filtered out
  66. '.abc/d/e.js' // included
  67. ]
  68. ig.filter(paths) // ['.abc/d/e.js']
  69. ig.ignores('.abc/a.js') // true
  70. ```
  71. ### As the filter function
  72. ```js
  73. paths.filter(ig.createFilter()); // ['.abc/d/e.js']
  74. ```
  75. ### Win32 paths will be handled
  76. ```js
  77. ig.filter(['.abc\\a.js', '.abc\\d\\e.js'])
  78. // if the code above runs on windows, the result will be
  79. // ['.abc\\d\\e.js']
  80. ```
  81. ## Why another ignore?
  82. - `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.
  83. - `ignore` only contains utility methods to filter paths according to the specified ignore rules, so
  84. - `ignore` never try to find out ignore rules by traversing directories or fetching from git configurations.
  85. - `ignore` don't cares about sub-modules of git projects.
  86. - Exactly according to [gitignore man page](http://git-scm.com/docs/gitignore), fixes some known matching issues of fstream-ignore, such as:
  87. - '`/*.js`' should only match '`a.js`', but not '`abc/a.js`'.
  88. - '`**/foo`' should match '`foo`' anywhere.
  89. - Prevent re-including a file if a parent directory of that file is excluded.
  90. - Handle trailing whitespaces:
  91. - `'a '`(one space) should not match `'a '`(two spaces).
  92. - `'a \ '` matches `'a '`
  93. - All test cases are verified with the result of `git check-ignore`.
  94. # Methods
  95. ## .add(pattern: string | Ignore): this
  96. ## .add(patterns: Array<string | Ignore>): this
  97. - **pattern** `String | Ignore` An ignore pattern string, or the `Ignore` instance
  98. - **patterns** `Array<String | Ignore>` Array of ignore patterns.
  99. Adds a rule or several rules to the current manager.
  100. Returns `this`
  101. 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.
  102. ```js
  103. ignore().add('#abc').ignores('#abc') // false
  104. ignore().add('\\#abc').ignores('#abc') // true
  105. ```
  106. `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:
  107. ```js
  108. ignore()
  109. .add(fs.readFileSync(filenameOfGitignore).toString())
  110. .filter(filenames)
  111. ```
  112. `pattern` could also be an `ignore` instance, so that we could easily inherit the rules of another `Ignore` instance.
  113. ## <strike>.addIgnoreFile(path)</strike>
  114. REMOVED in `3.x` for now.
  115. To upgrade `ignore@2.x` up to `3.x`, use
  116. ```js
  117. import fs from 'fs'
  118. if (fs.existsSync(filename)) {
  119. ignore().add(fs.readFileSync(filename).toString())
  120. }
  121. ```
  122. instead.
  123. ## .filter(paths: Array&lt;Pathname&gt;): Array&lt;Pathname&gt;
  124. ```ts
  125. type Pathname = string
  126. ```
  127. Filters the given array of pathnames, and returns the filtered array.
  128. - **paths** `Array.<Pathname>` The array of `pathname`s to be filtered.
  129. ### `Pathname` Conventions:
  130. #### 1. `Pathname` should be a `path.relative()`d pathname
  131. `Pathname` should be a string that have been `path.join()`ed, or the return value of `path.relative()` to the current directory,
  132. ```js
  133. // WRONG, an error will be thrown
  134. ig.ignores('./abc')
  135. // WRONG, for it will never happen, and an error will be thrown
  136. // If the gitignore rule locates at the root directory,
  137. // `'/abc'` should be changed to `'abc'`.
  138. // ```
  139. // path.relative('/', '/abc') -> 'abc'
  140. // ```
  141. ig.ignores('/abc')
  142. // WRONG, that it is an absolute path on Windows, an error will be thrown
  143. ig.ignores('C:\\abc')
  144. // Right
  145. ig.ignores('abc')
  146. // Right
  147. ig.ignores(path.join('./abc')) // path.join('./abc') -> 'abc'
  148. ```
  149. In other words, each `Pathname` here should be a relative path to the directory of the gitignore rules.
  150. Suppose the dir structure is:
  151. ```
  152. /path/to/your/repo
  153. |-- a
  154. | |-- a.js
  155. |
  156. |-- .b
  157. |
  158. |-- .c
  159. |-- .DS_store
  160. ```
  161. Then the `paths` might be like this:
  162. ```js
  163. [
  164. 'a/a.js'
  165. '.b',
  166. '.c/.DS_store'
  167. ]
  168. ```
  169. #### 2. filenames and dirnames
  170. `node-ignore` does NO `fs.stat` during path matching, so for the example below:
  171. ```js
  172. // First, we add a ignore pattern to ignore a directory
  173. ig.add('config/')
  174. // `ig` does NOT know if 'config', in the real world,
  175. // is a normal file, directory or something.
  176. ig.ignores('config')
  177. // `ig` treats `config` as a file, so it returns `false`
  178. ig.ignores('config/')
  179. // returns `true`
  180. ```
  181. Specially for people who develop some library based on `node-ignore`, it is important to understand that.
  182. Usually, you could use [`glob`](http://npmjs.org/package/glob) with `option.mark = true` to fetch the structure of the current directory:
  183. ```js
  184. import glob from 'glob'
  185. glob('**', {
  186. // Adds a / character to directory matches.
  187. mark: true
  188. }, (err, files) => {
  189. if (err) {
  190. return console.error(err)
  191. }
  192. let filtered = ignore().add(patterns).filter(files)
  193. console.log(filtered)
  194. })
  195. ```
  196. ## .ignores(pathname: Pathname): boolean
  197. > new in 3.2.0
  198. Returns `Boolean` whether `pathname` should be ignored.
  199. ```js
  200. ig.ignores('.abc/a.js') // true
  201. ```
  202. ## .createFilter()
  203. Creates a filter function which could filter an array of paths with `Array.prototype.filter`.
  204. Returns `function(path)` the filter function.
  205. ## .test(pathname: Pathname) since 5.0.0
  206. Returns `TestResult`
  207. ```ts
  208. interface TestResult {
  209. ignored: boolean
  210. // true if the `pathname` is finally unignored by some negative pattern
  211. unignored: boolean
  212. }
  213. ```
  214. - `{ignored: true, unignored: false}`: the `pathname` is ignored
  215. - `{ignored: false, unignored: true}`: the `pathname` is unignored
  216. - `{ignored: false, unignored: false}`: the `pathname` is never matched by any ignore rules.
  217. ## static `ignore.isPathValid(pathname): boolean` since 5.0.0
  218. Check whether the `pathname` is an valid `path.relative()`d path according to the [convention](#1-pathname-should-be-a-pathrelatived-pathname).
  219. This method is **NOT** used to check if an ignore pattern is valid.
  220. ```js
  221. ignore.isPathValid('./foo') // false
  222. ```
  223. ## ignore(options)
  224. ### `options.ignorecase` since 4.0.0
  225. 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` (the default value), otherwise case sensitive.
  226. ```js
  227. const ig = ignore({
  228. ignorecase: false
  229. })
  230. ig.add('*.png')
  231. ig.ignores('*.PNG') // false
  232. ```
  233. ### `options.ignoreCase?: boolean` since 5.2.0
  234. Which is alternative to `options.ignoreCase`
  235. ### `options.allowRelativePaths?: boolean` since 5.2.0
  236. This option brings backward compatibility with projects which based on `ignore@4.x`. If `options.allowRelativePaths` is `true`, `ignore` will not check whether the given path to be tested is [`path.relative()`d](#pathname-conventions).
  237. However, passing a relative path, such as `'./foo'` or `'../foo'`, to test if it is ignored or not is not a good practise, which might lead to unexpected behavior
  238. ```js
  239. ignore({
  240. allowRelativePaths: true
  241. }).ignores('../foo/bar.js') // And it will not throw
  242. ```
  243. ****
  244. # Upgrade Guide
  245. ## Upgrade 4.x -> 5.x
  246. Since `5.0.0`, if an invalid `Pathname` passed into `ig.ignores()`, an error will be thrown, unless `options.allowRelative = true` is passed to the `Ignore` factory.
  247. While `ignore < 5.0.0` did not make sure what the return value was, as well as
  248. ```ts
  249. .ignores(pathname: Pathname): boolean
  250. .filter(pathnames: Array<Pathname>): Array<Pathname>
  251. .createFilter(): (pathname: Pathname) => boolean
  252. .test(pathname: Pathname): {ignored: boolean, unignored: boolean}
  253. ```
  254. See the convention [here](#1-pathname-should-be-a-pathrelatived-pathname) for details.
  255. If there are invalid pathnames, the conversion and filtration should be done by users.
  256. ```js
  257. import {isPathValid} from 'ignore' // introduced in 5.0.0
  258. const paths = [
  259. // invalid
  260. //////////////////
  261. '',
  262. false,
  263. '../foo',
  264. '.',
  265. //////////////////
  266. // valid
  267. 'foo'
  268. ]
  269. .filter(isValidPath)
  270. ig.filter(paths)
  271. ```
  272. ## Upgrade 3.x -> 4.x
  273. Since `4.0.0`, `ignore` will no longer support node < 6, to use `ignore` in node < 6:
  274. ```js
  275. var ignore = require('ignore/legacy')
  276. ```
  277. ## Upgrade 2.x -> 3.x
  278. - All `options` of 2.x are unnecessary and removed, so just remove them.
  279. - `ignore()` instance is no longer an [`EventEmitter`](nodejs.org/api/events.html), and all events are unnecessary and removed.
  280. - `.addIgnoreFile()` is removed, see the [.addIgnoreFile](#addignorefilepath) section for details.
  281. ****
  282. # Collaborators
  283. - [@whitecolor](https://github.com/whitecolor) *Alex*
  284. - [@SamyPesse](https://github.com/SamyPesse) *Samy Pessé*
  285. - [@azproduction](https://github.com/azproduction) *Mikhail Davydov*
  286. - [@TrySound](https://github.com/TrySound) *Bogdan Chadkin*
  287. - [@JanMattner](https://github.com/JanMattner) *Jan Mattner*
  288. - [@ntwb](https://github.com/ntwb) *Stephen Edgar*
  289. - [@kasperisager](https://github.com/kasperisager) *Kasper Isager*
  290. - [@sandersn](https://github.com/sandersn) *Nathan Shively-Sanders*