Skip to content
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
60 changes: 33 additions & 27 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
var fs = require('fs')
, temporary = require('temporary')
, fmt = require('util').format
, child_process = require('child_process')
, net = require('net')
, freeport = require('freeport')
, _ = require('lodash')
, utils = require('./utils');
const child_process = require('child_process')
const fs = require('fs')
const net = require('net')
const {tmpdir} = require('os')
const {join} = require('path')
const {format} = require('util')

const freeport = require('freeport')
const _ = require('lodash')

const utils = require('./utils');


var MONGOD_BIN = 'mongod';
var DEFAULT_ARGS = [
// don't flood stdout, we're not reading it
"--quiet",
// journaling on by default in 2.0 and makes it to slow
// for tests, can causes failures in jenkins
"--nojournal",
// disable unused.
"--nounixsocket",
// don't flood stdout, we're not reading it
"--quiet",
// use a smaller default file size
"--smallfiles",
// journaling on by default in 2.0 and makes it to slow
// for tests, can causes failures in jenkins
"--nojournal",
]
var START_CHECK_ATTEMPTS = 200;


var MongoBox = function(_options) {
function MongoBox(_options) {
var options = _options || {};
this.options = options;

this.options.mongodBinary = options.mongodBinary || MONGOD_BIN;

if (!this.options.mongodBinary)
throw new Error(fmt('Could not find %s in system PATH. Make sure you have mongod installed.', MONGOD_BIN));
throw new Error(format('Could not find %s in system PATH. Make sure you have mongod installed.', MONGOD_BIN));

if (fs.existsSync(this.options.databasePath)
&& !fs.statSync(this.options.databasePath).isDirectory()) {
Expand All @@ -49,8 +53,8 @@ MongoBox.prototype.start = function(callback) {
fs.mkdirSync(this.options.databasePath);
}
} else {
this._temporary = new temporary.Dir();
this.options.databasePath = this._temporary.path;
this._temporary = fs.mkdtempSync(join(tmpdir(), 'mongobox-'));
this.options.databasePath = this._temporary;
}

var self = this;
Expand All @@ -61,10 +65,10 @@ MongoBox.prototype.start = function(callback) {
doStart.bind(self)();
});
} else {
doStart();
doStart.call(this);
}

var doStart = function() {
function doStart() {
var args = _.clone(DEFAULT_ARGS);
var op

Expand All @@ -85,7 +89,9 @@ MongoBox.prototype.start = function(callback) {


this.process = child_process.spawn(this.options.mongodBinary, args);
// console.log("%s %s", this.options.mongodBinary, args.join(" "));

if (this.options.printCommand)
console.log("%s %s", this.options.mongodBinary, args.join(" "));

var self = this;
this.process.on('close', function(code) {
Expand Down Expand Up @@ -128,18 +134,18 @@ MongoBox.prototype._waitTillStarted = function(callback) {
var self = this;

function tryConnect() {
var socket = net.connect({port: self.options.port}, function() {
callback();
});
socket.on('error', function() {
net.connect({port: self.options.port}, callback)
.on('error', function() {
attempts += 1;
if (self.process && attempts < START_CHECK_ATTEMPTS)
setTimeout(tryConnect, 250);
else callback(new Error('MongoDB did not start.'))
return setTimeout(tryConnect, 250);

callback(new Error('MongoDB did not start.'))
});
}

tryConnect();
}

module.exports.MongoBox = MongoBox;
MongoBox.MongoBox = MongoBox;
module.exports = MongoBox;
31 changes: 0 additions & 31 deletions package-lock.json

This file was deleted.

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
"url": "git://github.com/theorm/mongobox.js.git"
},
"dependencies": {
"temporary": "0.0.8",
"lodash": "4.17.11",
"freeport": "1.0.5"
"lodash": "^4.17.11",
"freeport": "^1.0.5"
},
"devDependencies": {},
"main": "lib",
"engines": {
"node": ">=0.8.0"
"node": ">=5.10.0"
}
}