Skip to content

Commit

Permalink
feat: support disabling colour output
Browse files Browse the repository at this point in the history
via cli --no-color/--no-colour and via nodemon.json colours: false
  • Loading branch information
remy committed Sep 12, 2015
1 parent 538f107 commit 6e40e9a
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 5 deletions.
1 change: 1 addition & 0 deletions doc/cli/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
-V, --verbose ............ show detail on what is causing restarts.
-I, --no-stdin ........... don't try to read from stdin.
-C, --on-change-only ..... execute script on change only, not startup
--no-colors .............. disable color output
-d, --delay n ............ debounce restart for "n" seconds.
--exitcrash .............. exit on crash, allows use of nodemon with daemon
tools like forever.js.
Expand Down
4 changes: 4 additions & 0 deletions lib/cli/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ function nodemonOption(options, arg, eatNext) {
options.ext = eatNext();
} else

if (arg === '--no-colours' || arg === '--no-colors') {
options.colours = false;
} else

if (arg === '--cwd') {
options.cwd = eatNext();

Expand Down
1 change: 1 addition & 0 deletions lib/config/defaults.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// default options for config.options
module.exports = {
restartable: 'rs',
colours: true,
execMap: {
py: 'python',
rb: 'ruby',
Expand Down
3 changes: 3 additions & 0 deletions lib/nodemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ function nodemon(settings) {
return;
}

// before we print anything, update the colour setting on logging
utils.colours = config.options.colours;

// always echo out the current version
utils.log.info(version);

Expand Down
9 changes: 9 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,13 @@ Object.defineProperty(utils, 'debug', {
get: function () {
return this.log.debug;
},
});

Object.defineProperty(utils, 'colours', {
set: function (value) {
this.log.useColours = value;
},
get: function () {
return this.log.useColours;
},
});
16 changes: 15 additions & 1 deletion lib/utils/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var util = require('util');
var colour = require('./colour');
var bus = require('./bus');
var required = false;
var useColours = true;

var coding = {
log: 'black',
Expand All @@ -13,7 +14,11 @@ var coding = {
};

function log(type, text) {
var msg = colour(coding[type], '[nodemon] ' + (text || ''));
var msg = '[nodemon] ' + (text || '');

if (useColours) {
msg = colour(coding[type], msg);
}

// always push the message through our bus
bus.emit('log', { type: type, message: text, colour: msg });
Expand Down Expand Up @@ -63,4 +68,13 @@ Logger.prototype._log = function (type, msg) {
}
};

Object.defineProperty(Logger.prototype, 'useColours', {
set: function (val) {
useColours = val;
},
get: function () {
return useColours;
},
});

module.exports = Logger;
34 changes: 30 additions & 4 deletions test/utils/log.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';
/*global describe:true, it: true */
var logger = require('../../lib/utils/log')(true),
bus = require('../../lib/utils/bus'),
colour = require('../../lib/utils/colour'),
assert = require('assert');
var Logger = require('../../lib/utils/log');
var logger = new Logger(true);
var bus = require('../../lib/utils/bus');
var colour = require('../../lib/utils/colour');
var assert = require('assert');

describe('logger', function () {
var types = {
Expand All @@ -28,6 +29,31 @@ describe('logger', function () {
});
});

it('should disable colour', function () {
var type = 'fail';
bus.once('log', function (event) {
assert.equal(event.message, type);
assert.ok(event.colour.indexOf(colour[types[type]]) !== -1);
});
logger[type](type);

logger.useColours = false;

bus.once('log', function (event) {
assert.equal(event.message, type);
assert.ok(event.colour.indexOf(colour[types[type]]) === -1);
});
logger[type](type);

logger.useColours = true;

bus.once('log', function (event) {
assert.equal(event.message, type);
assert.ok(event.colour.indexOf(colour[types[type]]) !== -1);
});
logger[type](type);
});

// it('should not log detail if debug is off', function (done) {
// logger.debug = false;

Expand Down

0 comments on commit 6e40e9a

Please sign in to comment.