Skip to content

Commit

Permalink
Issue 24: Possible solution to disable console.log messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhorber committed May 17, 2018
1 parent 46a0769 commit 1cafe0a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
25 changes: 19 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@ var _ = require('lodash'),

function Seeder() {
this.connected = false;
this.consoleLogEnabled = true;
}

function consoleLog(_this, message) {
if (_this.consoleLogEnabled !== undefined && _this.consoleLogEnabled === true) {
console.log(message);
}
}

Seeder.prototype.setLogOutput = function (value) {
this.consoleLogEnabled = value;
};

Seeder.prototype.connect = function(...params) {
var _this = this;
/*
Expand Down Expand Up @@ -41,7 +52,7 @@ Seeder.prototype.connect = function(...params) {

if (mongoose.connection.readyState === 1) {
_this.connected = true;
console.log('Successfully initialized mongoose-seed');
consoleLog(_this, 'Successfully initialized mongoose-seed');
cb();
} else {
if (opts) {
Expand All @@ -60,16 +71,16 @@ function afterConnect(_this, err, cb) {
// Log Error
if (err) {
console.error(chalk.red('Could not connect to MongoDB!'));
console.log(err);
consoleLog(_this, err);
} else {
_this.connected = true;
console.log('Successfully initialized mongoose-seed');
consoleLog(_this, 'Successfully initialized mongoose-seed');
cb();
}
}

Seeder.prototype.loadModels = function(modelPaths) {
console.log(modelPaths);
consoleLog(this, modelPaths);
modelPaths.forEach(function(modelPath) {
var model = require(path.resolve(modelPath));
if (model instanceof Function) {
Expand Down Expand Up @@ -100,6 +111,7 @@ Seeder.prototype.clearModels = function(models, cb) {
}

var modelNames = [];
var _this = this;

// Convert to array if not already
if (Array.isArray(models)) {
Expand All @@ -126,7 +138,7 @@ Seeder.prototype.clearModels = function(models, cb) {
console.error(chalk.red('Error: ' + err.message));
return;
}
console.log(modelName + 's collection cleared');
consoleLog(_this, modelName + 's collection cleared');
done();
});
}, function(err) {
Expand All @@ -145,6 +157,7 @@ Seeder.prototype.populateModels = function(seedData, cb) {
}

var modelNames = _.unique(_.pluck(seedData, 'model'));
var _this = this;

// Confirm that all Models have been registered in Mongoose
var invalidModels = this.invalidModelCheck(modelNames, function(err) {
Expand All @@ -162,7 +175,7 @@ Seeder.prototype.populateModels = function(seedData, cb) {
console.error(chalk.red('Error creating document [' + j + '] of ' + entry.model + ' model'));
console.error(chalk.red('Error: ' + err.message));
} else {
console.log('Successfully created document [' + j + '] of ' + entry.model + ' model');
consoleLog(_this, 'Successfully created document [' + j + '] of ' + entry.model + ' model');
}
innerCallback();
});
Expand Down
42 changes: 42 additions & 0 deletions test/teste-mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,46 @@ describe('Mongoose-Seeder', function() {

});

it('check no debug output', function(done) {
var outputDone = false;
var consoleOutput = '';
var originalLog = console.log;

console.log = function (message) {
outputDone = true;
consoleOutput += message;
};

seeder.setLogOutput(false);
seeder.connect(connection_url, function () {
expect(consoleOutput).to.equal('');
expect(outputDone).to.be.false;

seeder.setLogOutput(true);
console.log = originalLog;
done();
});

});

it('check debug output', function(done) {
var outputDone = false;
var consoleOutput = '';
var originalLog = console.log;

console.log = function (message) {
outputDone = true;
consoleOutput += message;
};

seeder.connect(connection_url, function () {
expect(outputDone).to.be.true;
expect(consoleOutput).to.equal('Successfully initialized mongoose-seed');

console.log = originalLog;
done();
});

});

});

0 comments on commit 1cafe0a

Please sign in to comment.