2013-07-17 04:30:02 +04:00
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
|
|
|
|
|
2013-08-07 03:07:17 +04:00
|
|
|
var globEach = module.exports.globEach = function(path_, ext, callback, doneCallback) {
|
2013-07-27 02:47:05 +04:00
|
|
|
var wildcard = ext === '*';
|
2013-08-09 00:13:27 +04:00
|
|
|
if (!doneCallback) {
|
|
|
|
doneCallback = function() {};
|
|
|
|
}
|
2013-07-27 02:47:05 +04:00
|
|
|
|
2013-07-17 04:30:02 +04:00
|
|
|
fs.readdir(path_, function(err, list) {
|
2013-08-07 03:07:17 +04:00
|
|
|
if (err) return doneCallback(err);
|
2013-07-17 04:30:02 +04:00
|
|
|
var pending = list.length;
|
2013-08-07 03:07:17 +04:00
|
|
|
if (!pending) return doneCallback(null);
|
2013-07-17 04:30:02 +04:00
|
|
|
list.forEach(function(file) {
|
|
|
|
file = path_ + '/' + file;
|
|
|
|
fs.stat(file, function(err, stat) {
|
|
|
|
if (stat && stat.isDirectory()) {
|
2013-08-07 03:07:17 +04:00
|
|
|
module.exports.globEach(file, ext, callback, function(err) {
|
|
|
|
if (!--pending) doneCallback(err);
|
2013-07-17 04:30:02 +04:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// If it's got the right extension, add it to the list.
|
2013-07-27 02:47:05 +04:00
|
|
|
if(wildcard || file.substr(file.length - ext.length) == ext)
|
2013-08-07 03:07:17 +04:00
|
|
|
callback(path.normalize(file));
|
|
|
|
if (!--pending) doneCallback(null);
|
2013-07-17 04:30:02 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-08-07 03:07:17 +04:00
|
|
|
module.exports.glob = function(path_, ext, done) {
|
|
|
|
var results = [];
|
2013-08-09 00:13:27 +04:00
|
|
|
globEach(path_, ext, function(data) {
|
|
|
|
results.push(data);
|
|
|
|
}, function(err) {
|
2013-08-07 03:07:17 +04:00
|
|
|
if (done) {
|
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
} else {
|
|
|
|
done(null, results);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|