Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suport search files for pattern #36

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 30 additions & 2 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 @@ -74,7 +75,7 @@ var Loader = exports.Loader = function(dbOrUri, options) {
safe: true
}, options);
}

this.options = options;
this.modifiers = [];
};
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,28 @@ 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){
var filePath = require(file);

Object.keys(filePath).map(function(key) {
data[key] = filePath[key];
});
});

cb(null, data);
}

/**
* Get data from one file as an object
Expand Down Expand Up @@ -514,4 +542,4 @@ var _buildConnectionUri = function(options) {
parts.push(options.db);

return parts.join('');
}
}
Loading