diff --git a/lib/index.js b/lib/index.js index f8b65ea..0e8e237 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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()) { @@ -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; @@ -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 @@ -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) { @@ -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; diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index fda5e21..0000000 --- a/package-lock.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "name": "mongobox", - "version": "0.1.0", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "freeport": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/freeport/-/freeport-1.0.5.tgz", - "integrity": "sha1-JV6KuEFwwzuoXZkOghrl9KGpvF0=" - }, - "lodash": { - "version": "4.17.11", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", - "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" - }, - "package": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/package/-/package-1.0.1.tgz", - "integrity": "sha1-0lofmeJQbcsn1nBLg9yooxLk7cw=" - }, - "temporary": { - "version": "0.0.8", - "resolved": "https://registry.npmjs.org/temporary/-/temporary-0.0.8.tgz", - "integrity": "sha1-oYqYHSi6jKNgJ/s8MFOMPst0CsA=", - "requires": { - "package": ">= 1.0.0 < 1.2.0" - } - } - } -} diff --git a/package.json b/package.json index 95619cd..06b4c5b 100644 --- a/package.json +++ b/package.json @@ -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" } }