Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor to use miniglob / minimatch combo, working on windows \o/
- Loading branch information
Showing
10 changed files
with
196 additions
and
479 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
language: node_js | ||
|
||
node_js: | ||
- 0.4 | ||
- 0.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,134 +1,65 @@ | ||
(function() { | ||
var EventEmitter, findit, fs, glob, globs, path, rsplit; | ||
glob = require('glob'); | ||
|
||
|
||
|
||
var util = require('util'), | ||
minimatch = require('minimatch'), | ||
Miniglob = require('miniglob').Miniglob, | ||
EventEmitter = require('events').EventEmitter; | ||
path = require('path'); | ||
fs = require('fs'); | ||
findit = require('findit'); | ||
rsplit = /\s|,\s/; | ||
module.exports = function(include, exclude, callback) { | ||
var em, excludes, exludesLength, includes, includesLength, match; | ||
em = new EventEmitter; | ||
match = { | ||
includes: [], | ||
excludes: [] | ||
}; | ||
if (!callback && typeof exclude === 'function') { | ||
callback = exclude; | ||
exclude = ""; | ||
} | ||
if (!callback && !exclude) { | ||
exclude = ""; | ||
} | ||
includes = include.split(rsplit).filter(function(it) { | ||
return it; | ||
}) || []; | ||
excludes = exclude.split(rsplit).filter(function(it) { | ||
return it; | ||
}) || []; | ||
exludesLength = excludes.length; | ||
includesLength = includes.length; | ||
globs(includes, em, function(err, results) { | ||
if (err) { | ||
if (callback) { | ||
callback(err); | ||
} | ||
return em.emit('error', err); | ||
} | ||
match.includes = results; | ||
em.emit('include', results); | ||
if (!excludes.length) { | ||
if (callback) { | ||
callback(null, results); | ||
} | ||
return em.emit('end', results); | ||
} | ||
return globs(excludes, em, function(err, results) { | ||
if (err) { | ||
if (callback) { | ||
callback(err); | ||
} | ||
return em.emit('error', err); | ||
} | ||
match.excludes = results; | ||
em.emit('exclude', results); | ||
match.includes = match.includes.filter(function(it) { | ||
var dotbegin, excluded; | ||
dotbegin = /^\./.test(it.split('/').reverse()[0]); | ||
excluded = !!~match.excludes.indexOf(it); | ||
return !excluded; | ||
}); | ||
em.emit('end', match.includes); | ||
if (callback) { | ||
return callback(null, match.includes); | ||
} | ||
|
||
module.exports = fileset; | ||
|
||
function fileset(include, exclude, cb) { | ||
if (typeof exclude === 'function') cb = exclude, exclude = ''; | ||
|
||
var em = new EventEmitter, | ||
includes = include.split(' '), | ||
excludes = exclude.split(' '), | ||
remaining = includes.length, | ||
results = []; | ||
|
||
if(!includes.length) return cb(new Error('Must provide an include pattern')); | ||
|
||
em.includes = includes.map(function(pattern) { | ||
return new fileset.Fileset(pattern) | ||
.on('error', cb ? cb : em.emit.bind(em, 'error')) | ||
.on('match', em.emit.bind(em, 'match')) | ||
.on('match', em.emit.bind(em, 'include')) | ||
.on('end', next.bind({}, pattern)) | ||
}); | ||
|
||
function next(pattern, matches) { | ||
results = results.concat(matches); | ||
|
||
if(!(--remaining)) { | ||
results = results.filter(function(file) { | ||
return !excludes.filter(function(glob) { | ||
var match = minimatch(file, glob, { matchBase: true }); | ||
if(match) em.emit('exclude', file); | ||
return match; | ||
}).length; | ||
}); | ||
}); | ||
return em; | ||
}; | ||
globs = function(patterns, em, callback) { | ||
var index, matches, pattern, remaining, _len, _results; | ||
if (!patterns.length) { | ||
return callback(new Error('patterns is empty')); | ||
} | ||
matches = []; | ||
patterns = patterns.filter(function(it) { | ||
return it; | ||
}); | ||
remaining = patterns.length; | ||
_results = []; | ||
for (index = 0, _len = patterns.length; index < _len; index++) { | ||
pattern = patterns[index]; | ||
_results.push((function(pattern, index) { | ||
var absolute, errors, files, isGlob; | ||
isGlob = /\*/.test(pattern); | ||
if (isGlob) { | ||
return glob(pattern, function(err, results) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
matches = matches.concat(results.map(function(it) { | ||
return path.resolve(it); | ||
})); | ||
if (--remaining === 0) { | ||
return callback(null, matches); | ||
} | ||
}); | ||
} | ||
files = []; | ||
errors = []; | ||
absolute = path.resolve(pattern); | ||
return fs.stat(absolute, function(err, stat) { | ||
if (err) { | ||
if (--remaining === 0) { | ||
callback(null, matches); | ||
} | ||
return; | ||
} | ||
if (stat && stat.isFile()) { | ||
matches = matches.concat([absolute]); | ||
if (--remaining === 0) { | ||
callback(null, matches); | ||
} | ||
return; | ||
} | ||
return findit(absolute).on('file', function(file) { | ||
em.emit('match', file); | ||
return files.push(file); | ||
}).on('error', function(err) { | ||
return errors.push(err); | ||
}).on('end', function() { | ||
if (errors.length) { | ||
return callback(new Error('findit returned with errors'), errors); | ||
} | ||
matches = matches.concat(files); | ||
if (--remaining === 0) { | ||
return callback(null, matches); | ||
} | ||
}); | ||
}); | ||
})(pattern, index)); | ||
|
||
if(cb) cb(null, results); | ||
em.emit('end', results); | ||
} | ||
return _results; | ||
}; | ||
}).call(this); | ||
} | ||
|
||
return em; | ||
} | ||
|
||
fileset.Fileset = function Fileset(pattern, options, cb) { | ||
|
||
if (typeof options === 'function') cb = options, options = {}; | ||
if (!options) options = {}; | ||
|
||
Miniglob.call(this, pattern, options); | ||
|
||
if(typeof cb === 'function') { | ||
this.on('error', cb); | ||
this.on('end', function(matches) { cb(null, matches); }); | ||
} | ||
}; | ||
|
||
util.inherits(fileset.Fileset, Miniglob); | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.