Skip to content

Commit

Permalink
Add suport search files for pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoosilva committed Dec 23, 2016
1 parent c236666 commit d6df233
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ Adds documents to the relevant collection. If the collection doesn't exist it wi

//Directories (loads all files in the directory)
fixtures.load(__dirname + '/fixtures', callback);

//Pattern (loads all files with a patter)
fixtures.load(__dirname + '/fixtures/*.js', callback);


clear(callback)
Expand Down Expand Up @@ -192,6 +195,9 @@ Installation
Changelog
---------

###0.15.0
- Add suport search files for pattern

###0.14.0
- Update mongodb to 2.2.x

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"dependencies": {
"async": "0.1.15",
"glob": "^7.1.1",
"mongodb": "~2.2.x",
"nodeunit": "^0.9.1",
"optimist": "0.3.5",
Expand Down
24 changes: 24 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var fs = require('fs'),
mongo = require('mongodb'),
ObjectID = mongo.ObjectId,
async = require('async'),
glob = require('glob'),
_ = require('underscore'),
basePath = path.dirname(module.parent.filename);

Expand Down Expand Up @@ -390,6 +391,11 @@ var _mixedToObject = function(fixtures, cb) {
// Resolve relative paths if necessary.
fixtures = path.resolve(basePath, fixtures);

// check if is a pattern
if(glob.hasMagic(fixtures)){
return _patternToObject(glob.sync(fixtures), cb);
}

//Determine if fixtures is pointing to a file or directory
fs.stat(fixtures, function(err, stats) {
if (err) return cb(err);
Expand All @@ -402,6 +408,24 @@ var _mixedToObject = function(fixtures, cb) {
});
}

/**
* Get data from pattern file as an object
*
* @param {Array} List of full path to the file to load
* @param {Function} Optional callback(err, data)
* @api private
*/
var _patternToObject = function(files, cb) {
cb = cb || noop;

var data = {};

files.map(function(file){
Object.assign(data, require(file));
});

cb(null, data);
}

/**
* Get data from one file as an object
Expand Down
35 changes: 34 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,40 @@ exports['load'] = {
});
});
},


'pattern': function(test) {
test.expect(2);

loader.load('./fixtures/*.js', function(err) {
if (err) return test.done(err);

async.parallel([
function(next) {
loadCollection('archer', function(err, docs) {
if (err) return next(err);

var names = _.pluck(docs, 'name');

test.same(names.sort(), ['Sterling', 'Lana', 'Cheryl'].sort());

next();
});
},
function(next) {
loadCollection('southpark', function(err, docs) {
if (err) return next(err);

var names = _.pluck(docs, 'name');

test.same(names.sort(), ['Stan', 'Towelie'].sort());

next();
});
}
], test.done);
});
},

'directory': {
'default' : function(test) {
loader.load('./fixtures', function(err) {
Expand Down

0 comments on commit d6df233

Please sign in to comment.