| 1 |
/* |
| 2 |
Copyright (c) 2012, Yahoo! Inc. All rights reserved. |
| 3 |
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. |
| 4 |
*/ |
| 5 |
|
| 6 |
var async = require('async'), |
| 7 |
glob = require('glob'), |
| 8 |
fs = require('fs'), |
| 9 |
path = require('path'), |
| 10 |
seq = 0; |
| 11 |
|
| 12 |
function filesFor(options, callback) { |
| 13 |
if (!callback && typeof options === 'function') { |
| 14 |
callback = options; |
| 15 |
options = null; |
| 16 |
} |
| 17 |
options = options || {}; |
| 18 |
|
| 19 |
var root = options.root, |
| 20 |
includes = options.includes, |
| 21 |
excludes = options.excludes, |
| 22 |
realpath = options.realpath, |
| 23 |
relative = options.relative, |
| 24 |
opts; |
| 25 |
|
| 26 |
root = root || process.cwd(); |
| 27 |
includes = includes && Array.isArray(includes) ? includes : [ '**/*.js' ]; |
| 28 |
excludes = excludes && Array.isArray(excludes) ? excludes : [ '**/node_modules/**' ]; |
| 29 |
|
| 30 |
opts = { cwd: root, nodir: true, ignore: excludes }; |
| 31 |
seq += 1; |
| 32 |
opts['x' + seq + new Date().getTime()] = true; //cache buster for minimatch cache bug |
| 33 |
glob(includes.join(' '), opts, function (err, files) { |
| 34 |
if (err) { return callback(err); } |
| 35 |
if (relative) { return callback(err, files); } |
| 36 |
|
| 37 |
if (!realpath) { |
| 38 |
files = files.map(function (file) { return path.resolve(root, file); }); |
| 39 |
return callback(err, files); |
| 40 |
} |
| 41 |
|
| 42 |
var realPathCache = module.constructor._realpathCache || {}; |
| 43 |
|
| 44 |
async.map(files, function (file, done) { |
| 45 |
fs.realpath(path.resolve(root, file), realPathCache, done); |
| 46 |
}, callback); |
| 47 |
}); |
| 48 |
} |
| 49 |
|
| 50 |
function matcherFor(options, callback) { |
| 51 |
|
| 52 |
if (!callback && typeof options === 'function') { |
| 53 |
callback = options; |
| 54 |
options = null; |
| 55 |
} |
| 56 |
options = options || {}; |
| 57 |
options.relative = false; //force absolute paths |
| 58 |
options.realpath = true; //force real paths (to match Node.js module paths) |
| 59 |
|
| 60 |
filesFor(options, function (err, files) { |
| 61 |
var fileMap = {}, |
| 62 |
matchFn; |
| 63 |
if (err) { return callback(err); } |
| 64 |
files.forEach(function (file) { fileMap[file] = true; }); |
| 65 |
|
| 66 |
matchFn = function (file) { return fileMap[file]; }; |
| 67 |
matchFn.files = Object.keys(fileMap); |
| 68 |
return callback(null, matchFn); |
| 69 |
}); |
| 70 |
} |
| 71 |
|
| 72 |
module.exports = { |
| 73 |
filesFor: filesFor, |
| 74 |
matcherFor: matcherFor |
| 75 |
}; |
| 76 |
|