10 <td colspan="2" align="center">
11 <a href="https://travis-ci.org/kaelzhang/node-ignore">
13 src="https://travis-ci.org/kaelzhang/node-ignore.svg?branch=master"
14 alt="Build Status" /></a>
17 <a href="https://ci.appveyor.com/project/kaelzhang/node-ignore">
19 src="https://ci.appveyor.com/api/projects/status/github/kaelzhang/node-ignore?branch=master&svg=true"
20 alt="Windows Build Status" /></a>
23 <a href="https://codecov.io/gh/kaelzhang/node-ignore">
25 src="https://codecov.io/gh/kaelzhang/node-ignore/branch/master/graph/badge.svg"
26 alt="Coverage Status" /></a>
29 <a href="https://www.npmjs.org/package/ignore">
31 src="http://img.shields.io/npm/dm/ignore.svg"
32 alt="npm module downloads per month" /></a>
38 `ignore` is a manager, filter and parser which implemented in pure JavaScript according to the .gitignore [spec](http://git-scm.com/docs/gitignore).
40 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.
44 - Linux + Node: `0.8` - `7.x`
45 - Windows + Node: `0.10` - `7.x`, node < `0.10` is not tested due to the lack of support of appveyor.
47 Actually, `ignore` does not rely on any versions of node specially.
49 ## Table Of Main Contents
52 - [Guide for 2.x -> 3.x](#upgrade-2x---3x)
53 - [Contributing](#contributing)
55 - [`glob-gitignore`](https://www.npmjs.com/package/glob-gitignore) matches files using patterns and filters them according to gitignore rules.
60 const ignore = require('ignore')
61 const ig = ignore().add(['.abc/*', '!.abc/d/'])
64 ### Filter the given paths
68 '.abc/a.js', // filtered out
69 '.abc/d/e.js' // included
72 ig.filter(paths) // ['.abc/d/e.js']
73 ig.ignores('.abc/a.js') // true
76 ### As the filter function
79 paths.filter(ig.createFilter()); // ['.abc/d/e.js']
82 ### Win32 paths will be handled
85 ig.filter(['.abc\\a.js', '.abc\\d\\e.js'])
86 // if the code above runs on windows, the result will be
90 ## Why another ignore?
92 - `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.
94 - `ignore` only contains utility methods to filter paths according to the specified ignore rules, so
95 - `ignore` never try to find out ignore rules by traversing directories or fetching from git configurations.
96 - `ignore` don't cares about sub-modules of git projects.
98 - Exactly according to [gitignore man page](http://git-scm.com/docs/gitignore), fixes some known matching issues of fstream-ignore, such as:
99 - '`/*.js`' should only match '`a.js`', but not '`abc/a.js`'.
100 - '`**/foo`' should match '`foo`' anywhere.
101 - Prevent re-including a file if a parent directory of that file is excluded.
102 - Handle trailing whitespaces:
103 - `'a '`(one space) should not match `'a '`(two spaces).
104 - `'a \ '` matches `'a '`
105 - All test cases are verified with the result of `git check-ignore`.
112 - **pattern** `String|Ignore` An ignore pattern string, or the `Ignore` instance
113 - **patterns** `Array.<pattern>` Array of ignore patterns.
115 Adds a rule or several rules to the current manager.
119 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.
122 ignore().add('#abc').ignores('#abc') // false
123 ignore().add('\#abc').ignores('#abc') // true
126 `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:
130 .add(fs.readFileSync(filenameOfGitignore).toString())
134 `pattern` could also be an `ignore` instance, so that we could easily inherit the rules of another `Ignore` instance.
136 ### <strike>.addIgnoreFile(path)</strike>
138 REMOVED in `3.x` for now.
140 To upgrade `ignore@2.x` up to `3.x`, use
143 const fs = require('fs')
145 if (fs.existsSync(filename)) {
146 ignore().add(fs.readFileSync(filename).toString())
153 ### .ignores(pathname)
157 Returns `Boolean` whether `pathname` should be ignored.
160 ig.ignores('.abc/a.js') // true
165 Filters the given array of pathnames, and returns the filtered array.
167 - **paths** `Array.<path>` The array of `pathname`s to be filtered.
171 - `pathname` should be a string that have been `path.join()`ed, or the return value of `path.relative()` to the current directory.
177 // WRONG, for it will never happen.
178 // If the gitignore rule locates at the root directory,
179 // `'/abc'` should be changed to `'abc'`.
181 // path.relative('/', '/abc') -> 'abc'
189 ig.ignores(path.join('./abc')) // path.join('./abc') -> 'abc'
192 - In other words, each `pathname` here should be a relative path to the directory of the git ignore rules.
194 Suppose the dir structure is:
207 Then the `paths` might be like this:
217 Usually, you could use [`glob`](http://npmjs.org/package/glob) with `option.mark = true` to fetch the structure of the current directory:
220 const glob = require('glob')
223 // Adds a / character to directory matches.
227 return console.error(err)
230 let filtered = ignore().add(patterns).filter(files)
231 console.log(filtered)
237 Creates a filter function which could filter an array of paths with `Array.prototype.filter`.
239 Returns `function(path)` the filter function.
243 ## Upgrade 2.x -> 3.x
245 - All `options` of 2.x are unnecessary and removed, so just remove them.
246 - `ignore()` instance is no longer an [`EventEmitter`](nodejs.org/api/events.html), and all events are unnecessary and removed.
247 - `.addIgnoreFile()` is removed, see the [.addIgnoreFile](#addignorefilepath) section for details.
253 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.
255 So use `bash install.sh` to install dependencies and `bash test.sh` to run test cases in your local machine.
259 - [SamyPesse](https://github.com/SamyPesse) *Samy Pessé*
260 - [azproduction](https://github.com/azproduction) *Mikhail Davydov*
261 - [TrySound](https://github.com/TrySound) *Bogdan Chadkin*
262 - [JanMattner](https://github.com/JanMattner) *Jan Mattner*