Skip to content

Commit 6e40e9a

Browse files
committed
feat: support disabling colour output
via cli --no-color/--no-colour and via nodemon.json colours: false
1 parent 538f107 commit 6e40e9a

File tree

7 files changed

+63
-5
lines changed

7 files changed

+63
-5
lines changed

doc/cli/help.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
-V, --verbose ............ show detail on what is causing restarts.
1212
-I, --no-stdin ........... don't try to read from stdin.
1313
-C, --on-change-only ..... execute script on change only, not startup
14+
--no-colors .............. disable color output
1415
-d, --delay n ............ debounce restart for "n" seconds.
1516
--exitcrash .............. exit on crash, allows use of nodemon with daemon
1617
tools like forever.js.

lib/cli/parse.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ function nodemonOption(options, arg, eatNext) {
178178
options.ext = eatNext();
179179
} else
180180

181+
if (arg === '--no-colours' || arg === '--no-colors') {
182+
options.colours = false;
183+
} else
184+
181185
if (arg === '--cwd') {
182186
options.cwd = eatNext();
183187

lib/config/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// default options for config.options
22
module.exports = {
33
restartable: 'rs',
4+
colours: true,
45
execMap: {
56
py: 'python',
67
rb: 'ruby',

lib/nodemon.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ function nodemon(settings) {
7171
return;
7272
}
7373

74+
// before we print anything, update the colour setting on logging
75+
utils.colours = config.options.colours;
76+
7477
// always echo out the current version
7578
utils.log.info(version);
7679

lib/utils/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,13 @@ Object.defineProperty(utils, 'debug', {
7878
get: function () {
7979
return this.log.debug;
8080
},
81+
});
82+
83+
Object.defineProperty(utils, 'colours', {
84+
set: function (value) {
85+
this.log.useColours = value;
86+
},
87+
get: function () {
88+
return this.log.useColours;
89+
},
8190
});

lib/utils/log.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var util = require('util');
22
var colour = require('./colour');
33
var bus = require('./bus');
44
var required = false;
5+
var useColours = true;
56

67
var coding = {
78
log: 'black',
@@ -13,7 +14,11 @@ var coding = {
1314
};
1415

1516
function log(type, text) {
16-
var msg = colour(coding[type], '[nodemon] ' + (text || ''));
17+
var msg = '[nodemon] ' + (text || '');
18+
19+
if (useColours) {
20+
msg = colour(coding[type], msg);
21+
}
1722

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

71+
Object.defineProperty(Logger.prototype, 'useColours', {
72+
set: function (val) {
73+
useColours = val;
74+
},
75+
get: function () {
76+
return useColours;
77+
},
78+
});
79+
6680
module.exports = Logger;

test/utils/log.test.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict';
22
/*global describe:true, it: true */
3-
var logger = require('../../lib/utils/log')(true),
4-
bus = require('../../lib/utils/bus'),
5-
colour = require('../../lib/utils/colour'),
6-
assert = require('assert');
3+
var Logger = require('../../lib/utils/log');
4+
var logger = new Logger(true);
5+
var bus = require('../../lib/utils/bus');
6+
var colour = require('../../lib/utils/colour');
7+
var assert = require('assert');
78

89
describe('logger', function () {
910
var types = {
@@ -28,6 +29,31 @@ describe('logger', function () {
2829
});
2930
});
3031

32+
it('should disable colour', function () {
33+
var type = 'fail';
34+
bus.once('log', function (event) {
35+
assert.equal(event.message, type);
36+
assert.ok(event.colour.indexOf(colour[types[type]]) !== -1);
37+
});
38+
logger[type](type);
39+
40+
logger.useColours = false;
41+
42+
bus.once('log', function (event) {
43+
assert.equal(event.message, type);
44+
assert.ok(event.colour.indexOf(colour[types[type]]) === -1);
45+
});
46+
logger[type](type);
47+
48+
logger.useColours = true;
49+
50+
bus.once('log', function (event) {
51+
assert.equal(event.message, type);
52+
assert.ok(event.colour.indexOf(colour[types[type]]) !== -1);
53+
});
54+
logger[type](type);
55+
});
56+
3157
// it('should not log detail if debug is off', function (done) {
3258
// logger.debug = false;
3359

0 commit comments

Comments
 (0)