1 // Essentially, this is a fstream.DirReader class, but with a
2 // bit of special logic to read the specified sort of ignore files,
3 // and a filter that prevents it from picking up anything excluded
6 var Minimatch = require("minimatch").Minimatch
7 , fstream = require("fstream")
8 , DirReader = fstream.DirReader
9 , inherits = require("inherits")
10 , path = require("path")
13 module.exports = IgnoreReader
15 inherits(IgnoreReader, DirReader)
17 function IgnoreReader (props) {
18 if (!(this instanceof IgnoreReader)) {
19 return new IgnoreReader(props)
22 // must be a Directory type
23 if (typeof props === "string") {
24 props = { path: path.resolve(props) }
27 props.type = "Directory"
28 props.Directory = true
30 if (!props.ignoreFiles) props.ignoreFiles = [".ignore"]
31 this.ignoreFiles = props.ignoreFiles
33 this.ignoreRules = null
35 // ensure that .ignore files always show up at the top of the list
36 // that way, they can be read before proceeding to handle other
37 // entries in that same folder
39 this._sort = props.sort === "alpha" ? alphasort : props.sort
43 this.on("entries", function () {
44 // if there are any ignore files in the list, then
45 // pause and add them.
46 // then, filter the list based on our ignoreRules
48 var hasIg = this.entries.some(this.isIgnoreFile, this)
50 if (!hasIg) return this.filterEntries()
55 // we filter entries before we know what they are.
56 // however, directories have to be re-tested against
57 // rules with a "/" appended, because "a/b/" will only
58 // match if "a/b" is a dir, and not otherwise.
59 this.on("_entryStat", function (entry, props) {
60 var t = entry.basename
61 if (!this.applyIgnores(entry.basename,
62 entry.type === "Directory",
68 DirReader.call(this, props)
72 IgnoreReader.prototype.addIgnoreFiles = function () {
74 this.once("resume", this.addIgnoreFiles)
77 if (this._ignoreFilesAdded) return
78 this._ignoreFilesAdded = true
80 var newIg = this.entries.filter(this.isIgnoreFile, this)
81 , count = newIg.length
88 var then = function (er) {
90 if (er) return this.emit("error", errState = er)
95 this.addIgnoreFile(newIg[newIg.length - count], then)
99 this.addIgnoreFile(newIg[0], then)
103 IgnoreReader.prototype.isIgnoreFile = function (e) {
106 -1 !== this.ignoreFiles.indexOf(e)
110 IgnoreReader.prototype.getChildProps = function (stat) {
111 var props = DirReader.prototype.getChildProps.call(this, stat)
112 props.ignoreFiles = this.ignoreFiles
114 // Directories have to be read as IgnoreReaders
115 // otherwise fstream.Reader will create a DirReader instead.
116 if (stat.isDirectory()) {
117 props.type = this.constructor
123 IgnoreReader.prototype.addIgnoreFile = function (e, cb) {
124 // read the file, and then call addIgnoreRules
125 // if there's an error, then tell the cb about it.
127 var ig = path.resolve(this.path, e)
128 fs.readFile(ig, function (er, data) {
129 if (er) return cb(er)
131 this.emit("ignoreFile", e, data)
132 var rules = this.readRules(data, e)
133 this.addIgnoreRules(rules, e)
139 IgnoreReader.prototype.readRules = function (buf, e) {
140 return buf.toString().split(/\r?\n/)
144 // Override this to do fancier things, like read the
145 // "files" array from a package.json file or something.
146 IgnoreReader.prototype.addIgnoreRules = function (set, e) {
147 // filter out anything obvious
148 set = set.filter(function (s) {
150 return s && !s.match(/^#/)
154 if (!set.length) return
156 // now get a minimatch object for each one of these.
157 // Note that we need to allow dot files by default, and
158 // not switch the meaning of their exclusion
159 var mmopt = { matchBase: true, dot: true, flipNegate: true }
160 , mm = set.map(function (s) {
161 var m = new Minimatch(s, mmopt)
166 if (!this.ignoreRules) this.ignoreRules = []
167 this.ignoreRules.push.apply(this.ignoreRules, mm)
171 IgnoreReader.prototype.filterEntries = function () {
172 // this exclusion is at the point where we know the list of
173 // entries in the dir, but don't know what they are. since
174 // some of them *might* be directories, we have to run the
175 // match in dir-mode as well, so that we'll pick up partials
176 // of files that will be included later. Anything included
177 // at this point will be checked again later once we know
179 this.entries = this.entries.filter(function (entry) {
180 // at this point, we don't know if it's a dir or not.
181 return this.applyIgnores(entry) || this.applyIgnores(entry, true)
186 IgnoreReader.prototype.applyIgnores = function (entry, partial, obj) {
191 // parent /a/b sees c/d
192 if (this.parent && this.parent.applyIgnores) {
193 var pt = this.basename + "/" + entry
194 included = this.parent.applyIgnores(pt, partial)
198 // Since we're *ignoring* things here, negating means that a file
199 // is re-included, if it would have been excluded by a previous
200 // rule. So, negated rules are only relevant if the file
201 // has been excluded.
203 // Similarly, if a file has been excluded, then there's no point
204 // trying it against rules that have already been applied
206 // We're using the "flipnegate" flag here, which tells minimatch
207 // to set the "negate" for our information, but still report
208 // whether the core pattern was a hit or a miss.
210 if (!this.ignoreRules) {
214 this.ignoreRules.forEach(function (rule) {
215 // negation means inclusion
216 if (rule.negate && included ||
217 !rule.negate && !included) {
222 // first, match against /foo/bar
223 var match = rule.match("/" + entry)
226 // try with the leading / trimmed off the test
227 // eg: foo/bar instead of /foo/bar
228 match = rule.match(entry)
231 // if the entry is a directory, then it will match
232 // with a trailing slash. eg: /foo/bar/ or foo/bar/
233 if (!match && partial) {
234 match = rule.match("/" + entry + "/") ||
235 rule.match(entry + "/")
238 // When including a file with a negated rule, it's
239 // relevant if a directory partially matches, since
240 // it may then match a file within it.
241 // Eg, if you ignore /a, but !/a/b/c
242 if (!match && rule.negate && partial) {
243 match = rule.match("/" + entry, true) ||
244 rule.match(entry, true)
248 included = rule.negate
256 IgnoreReader.prototype.sort = function (a, b) {
257 var aig = this.ignoreFiles.indexOf(a) !== -1
258 , big = this.ignoreFiles.indexOf(b) !== -1
260 if (aig && !big) return -1
261 if (big && !aig) return 1
262 return this._sort(a, b)
265 IgnoreReader.prototype._sort = function (a, b) {
269 function alphasort (a, b) {
271 : a.toLowerCase() > b.toLowerCase() ? 1
272 : a.toLowerCase() < b.toLowerCase() ? -1