diff --git a/README.md b/README.md index ab1b820..791324c 100644 --- a/README.md +++ b/README.md @@ -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) @@ -192,6 +195,9 @@ Installation Changelog --------- +###0.15.0 +- Add suport search files for pattern + ###0.14.0 - Update mongodb to 2.2.x diff --git a/package.json b/package.json index d6abd37..4b40d1c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.js b/src/index.js index 4df8172..a7a9c72 100644 --- a/src/index.js +++ b/src/index.js @@ -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); @@ -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); @@ -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 diff --git a/test/index.test.js b/test/index.test.js index 4f978e5..fd5963d 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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) {