This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixtures.js
54 lines (44 loc) · 1.4 KB
/
fixtures.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* global Fixtures:true */
Fixtures = this.Fixtures = function() {
'use strict';
function execute (cmd) {
Npm.require('child_process').exec(cmd, function (error, stdout, stderr) {
!!error && console.warn(error);
console.log(stdout + stderr);
});
}
function defaultArgs(args) {
if (typeof args === 'undefined') {
args = {};
}
if (!('db' in args)) {
args.db = process.env.MONGO_URL.match(/[^\/]*$/g)[0];
}
if (!('name' in args)) {
args.name = 'default';
}
return args;
}
return {
loadFixtures: function (passedArgs) {
var args = defaultArgs(passedArgs),
path = [
process.env.PWD, 'tests/fixtures', args.name, args.db].join('/');
execute('mongorestore -h 127.0.0.1:3001 --db ' + args.db + ' ' + path);
},
saveFixtures: function (passedArgs) {
var args = defaultArgs(passedArgs),
path = [process.env.PWD, 'tests/fixtures', args.name].join('/');
Npm.require('fs').lstat(path + '/' + args.db, function(error) {
if (!!error && error.code === 'ENOENT') {
// If lstat throws an ENOENT error then the path doesn't exist.
execute(
'mongodump -h 127.0.0.1:3001 --db ' + args.db + ' -o ' + path);
} else {
console.warn(
'Path already exists! Remove existing fixtures before saving.');
}
});
}
};
}();