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
33 changes: 17 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var util = require('util');
// External libs.
var hooker = require('hooker');
// Requiring this here modifies the String prototype!
var colors = require('colors');
var chalk = new require('chalk');
chalk.enabled = true;
// The upcoming lodash 2.5+ should remove the need for underscore.string.
var _ = require('lodash');
_.str = require('underscore.string');
Expand Down Expand Up @@ -107,12 +108,12 @@ makeSmartAccessor('muted', true);
Log.prototype.initColors = function() {
if (this.option('no-color')) {
// String color getters should just return the string.
colors.mode = 'none';
chalk.enabled = false;
// Strip colors from strings passed to console.log.
hooker.hook(console, 'log', function() {
var args = _.toArray(arguments);
return hooker.filter(this, args.map(function(arg) {
return typeof arg === 'string' ? colors.stripColors(arg) : arg;
return typeof arg === 'string' ? chalk.stripColor(arg) : arg;
}));
});
}
Expand All @@ -132,9 +133,9 @@ Log.prototype.option = function(name) {
Log.prototype._markup = function(str) {
str = str || '';
// Make _foo_ underline.
str = str.replace(/(\s|^)_(\S|\S[\s\S]+?\S)_(?=[\s,.!?]|$)/g, '$1' + '$2'.underline);
str = str.replace(/(\s|^)_(\S|\S[\s\S]+?\S)_(?=[\s,.!?]|$)/g, '$1' + chalk.underline('$2'));
// Make *foo* bold.
str = str.replace(/(\s|^)\*(\S|\S[\s\S]+?\S)\*(?=[\s,.!?]|$)/g, '$1' + '$2'.bold);
str = str.replace(/(\s|^)\*(\S|\S[\s\S]+?\S)\*(?=[\s,.!?]|$)/g, '$1' + chalk.bold('$2'));
return str;
};

Expand All @@ -156,7 +157,7 @@ Log.prototype._write = function(msg) {
msg = msg || '';
// Users should probably use the colors-provided methods, but if they
// don't, this should strip extraneous color codes.
if (this.option('no-color')) { msg = colors.stripColors(msg); }
if (this.option('no-color')) { msg = chalk.stripColor(msg); }
// Actually write to stdout.
this.options.outStream.write(this._markup(msg));
};
Expand All @@ -181,9 +182,9 @@ Log.prototype.writeln = function() {
Log.prototype.warn = function() {
var msg = this._format(arguments);
if (arguments.length > 0) {
this._writeln('>> '.red + _.trim(msg).replace(/\n/g, '\n>> '.red));
this._writeln(chalk.red('>> ') + _.trim(msg).replace(/\n/g, chalk.red('\n>> ')));
} else {
this._writeln('ERROR'.red);
this._writeln(chalk.red('ERROR'));
}
return this;
};
Expand All @@ -197,9 +198,9 @@ Log.prototype.error = function() {
Log.prototype.ok = function() {
var msg = this._format(arguments);
if (arguments.length > 0) {
this._writeln('>> '.green + _.trim(msg).replace(/\n/g, '\n>> '.green));
this._writeln(chalk.green('>> ') + _.trim(msg).replace(/\n/g, chalk.green('\n>> ')));
} else {
this._writeln('OK'.green);
this._writeln(chalk.green('OK'));
}
return this;
};
Expand All @@ -215,33 +216,33 @@ Log.prototype.oklns = function() {
};
Log.prototype.success = function() {
var msg = this._format(arguments);
this._writeln(msg.green);
this._writeln(chalk.green(msg));
return this;
};
Log.prototype.fail = function() {
var msg = this._format(arguments);
this._writeln(msg.red);
this._writeln(chalk.red(msg));
return this;
};
Log.prototype.header = function() {
var msg = this._format(arguments);
// Skip line before header, but not if header is the very first line output.
if (this.hasLogged) { this._writeln(); }
this._writeln(msg.underline);
this._writeln(chalk.underline(msg));
return this;
};
Log.prototype.subhead = function() {
var msg = this._format(arguments);
// Skip line before subhead, but not if subhead is the very first line output.
if (this.hasLogged) { this._writeln(); }
this._writeln(msg.bold);
this._writeln(chalk.bold(msg));
return this;
};
// For debugging.
Log.prototype.debug = function() {
var msg = this._format(arguments);
if (this.option('debug')) {
this._writeln('[D] ' + msg.magenta);
this._writeln('[D] ' + chalk.magenta(msg));
}
return this;
};
Expand Down Expand Up @@ -270,7 +271,7 @@ Log.prototype.writeflags = function(obj, prefix) {
return key + (val === true ? '' : '=' + JSON.stringify(val));
}));
}
this._writeln((prefix || 'Flags') + ': ' + (wordlist || '(none)'.cyan));
this._writeln((prefix || 'Flags') + ': ' + (wordlist || chalk.cyan('(none)')));
return this;
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"legacy"
],
"dependencies": {
"colors": "~1.1.2",
"chalk": "~1.1.1",
"grunt-legacy-log-utils": "~1.0.0",
"hooker": "~0.2.3",
"lodash": "~3.10.1",
Expand Down
87 changes: 44 additions & 43 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var legacyLog = require('../');
var chalk = require('chalk');
var Log = legacyLog.Log;

// Helper for testing stdout
Expand Down Expand Up @@ -82,10 +83,10 @@ exports['Log instance'] = {
test.expect(5);
var log = new Log({grunt: this.grunt});

stdoutEqual(test, function() { log.warn(); }, 'ERROR'.red + '\n');
stdoutEqual(test, function() { log.warn('foo'); }, '>> '.red + 'foo\n');
stdoutEqual(test, function() { log.warn('%s', 'foo'); }, '>> '.red + 'foo\n');
stdoutEqual(test, function() { log.warn(fooBuffer); }, '>> '.red + 'foo\n');
stdoutEqual(test, function() { log.warn(); }, chalk.red('ERROR') + '\n');
stdoutEqual(test, function() { log.warn('foo'); }, chalk.red('>> ') + 'foo\n');
stdoutEqual(test, function() { log.warn('%s', 'foo'); }, chalk.red('>> ') + 'foo\n');
stdoutEqual(test, function() { log.warn(fooBuffer); }, chalk.red('>> ') + 'foo\n');
test.equal(this.grunt.fail.errorcount, 0);

test.done();
Expand All @@ -94,10 +95,10 @@ exports['Log instance'] = {
test.expect(5);
var log = new Log({grunt: this.grunt});

stdoutEqual(test, function() { log.error(); }, 'ERROR'.red + '\n');
stdoutEqual(test, function() { log.error('foo'); }, '>> '.red + 'foo\n');
stdoutEqual(test, function() { log.error('%s', 'foo'); }, '>> '.red + 'foo\n');
stdoutEqual(test, function() { log.error(fooBuffer); }, '>> '.red + 'foo\n');
stdoutEqual(test, function() { log.error(); }, chalk.red('ERROR') + '\n');
stdoutEqual(test, function() { log.error('foo'); }, chalk.red('>> ') + 'foo\n');
stdoutEqual(test, function() { log.error('%s', 'foo'); }, chalk.red('>> ') + 'foo\n');
stdoutEqual(test, function() { log.error(fooBuffer); }, chalk.red('>> ') + 'foo\n');
test.equal(this.grunt.fail.errorcount, 4);

test.done();
Expand All @@ -106,10 +107,10 @@ exports['Log instance'] = {
test.expect(4);
var log = new Log({grunt: this.grunt});

stdoutEqual(test, function() { log.ok(); }, 'OK'.green + '\n');
stdoutEqual(test, function() { log.ok('foo'); }, '>> '.green + 'foo\n');
stdoutEqual(test, function() { log.ok('%s', 'foo'); }, '>> '.green + 'foo\n');
stdoutEqual(test, function() { log.ok(fooBuffer); }, '>> '.green + 'foo\n');
stdoutEqual(test, function() { log.ok(); }, chalk.green('OK') + '\n');
stdoutEqual(test, function() { log.ok('foo'); }, chalk.green('>> ') + 'foo\n');
stdoutEqual(test, function() { log.ok('%s', 'foo'); }, chalk.green('>> ') + 'foo\n');
stdoutEqual(test, function() { log.ok(fooBuffer); }, chalk.green('>> ') + 'foo\n');

test.done();
},
Expand All @@ -119,8 +120,8 @@ exports['Log instance'] = {

stdoutEqual(test, function() {
log.errorlns(repeat('foo', 30, ' '));
}, '>> '.red + repeat('foo', 19, ' ') +
'\n>> '.red + repeat('foo', 11, ' ') + '\n');
}, chalk.red('>> ') + repeat('foo', 19, ' ') +
chalk.red('\n>> ') + repeat('foo', 11, ' ') + '\n');
test.equal(this.grunt.fail.errorcount, 1);

test.done();
Expand All @@ -131,54 +132,54 @@ exports['Log instance'] = {

stdoutEqual(test, function() {
log.oklns(repeat('foo', 30, ' '));
}, '>> '.green + repeat('foo', 19, ' ') +
'\n>> '.green + repeat('foo', 11, ' ') + '\n');
}, chalk.green('>> ') + repeat('foo', 19, ' ') +
chalk.green('\n>> ') + repeat('foo', 11, ' ') + '\n');

test.done();
},
'success': function(test) {
test.expect(4);
var log = new Log();

stdoutEqual(test, function() { log.success(); }, ''.green + '\n');
stdoutEqual(test, function() { log.success('foo'); }, 'foo'.green + '\n');
stdoutEqual(test, function() { log.success('%s', 'foo'); }, 'foo'.green + '\n');
stdoutEqual(test, function() { log.success(fooBuffer); }, 'foo'.green + '\n');
stdoutEqual(test, function() { log.success(); }, chalk.green('') + '\n');
stdoutEqual(test, function() { log.success('foo'); }, chalk.green('foo') + '\n');
stdoutEqual(test, function() { log.success('%s', 'foo'); }, chalk.green('foo') + '\n');
stdoutEqual(test, function() { log.success(fooBuffer); }, chalk.green('foo') + '\n');

test.done();
},
'fail': function(test) {
test.expect(4);
var log = new Log();

stdoutEqual(test, function() { log.fail(); }, ''.red + '\n');
stdoutEqual(test, function() { log.fail('foo'); }, 'foo'.red + '\n');
stdoutEqual(test, function() { log.fail('%s', 'foo'); }, 'foo'.red + '\n');
stdoutEqual(test, function() { log.fail(fooBuffer); }, 'foo'.red + '\n');
stdoutEqual(test, function() { log.fail(); }, chalk.red('') + '\n');
stdoutEqual(test, function() { log.fail('foo'); }, chalk.red('foo') + '\n');
stdoutEqual(test, function() { log.fail('%s', 'foo'); }, chalk.red('foo') + '\n');
stdoutEqual(test, function() { log.fail(fooBuffer); }, chalk.red('foo') + '\n');

test.done();
},
'header': function(test) {
test.expect(5);
var log = new Log();

stdoutEqual(test, function() { log.header(); }, ''.underline + '\n');
stdoutEqual(test, function() { log.header(); }, '\n' + ''.underline + '\n');
stdoutEqual(test, function() { log.header('foo'); }, '\n' + 'foo'.underline + '\n');
stdoutEqual(test, function() { log.header('%s', 'foo'); }, '\n' + 'foo'.underline + '\n');
stdoutEqual(test, function() { log.header(fooBuffer); }, '\n' + 'foo'.underline + '\n');
stdoutEqual(test, function() { log.header(); }, chalk.underline('') + '\n');
stdoutEqual(test, function() { log.header(); }, '\n' + chalk.underline('') + '\n');
stdoutEqual(test, function() { log.header('foo'); }, '\n' + chalk.underline('foo') + '\n');
stdoutEqual(test, function() { log.header('%s', 'foo'); }, '\n' + chalk.underline('foo') + '\n');
stdoutEqual(test, function() { log.header(fooBuffer); }, '\n' + chalk.underline('foo') + '\n');

test.done();
},
'subhead': function(test) {
test.expect(5);
var log = new Log();

stdoutEqual(test, function() { log.subhead(); }, ''.bold + '\n');
stdoutEqual(test, function() { log.subhead(); }, '\n' + ''.bold + '\n');
stdoutEqual(test, function() { log.subhead('foo'); }, '\n' + 'foo'.bold + '\n');
stdoutEqual(test, function() { log.subhead('%s', 'foo'); }, '\n' + 'foo'.bold + '\n');
stdoutEqual(test, function() { log.subhead(fooBuffer); }, '\n' + 'foo'.bold + '\n');
stdoutEqual(test, function() { log.subhead(); }, chalk.bold('') + '\n');
stdoutEqual(test, function() { log.subhead(); }, '\n' + chalk.bold('') + '\n');
stdoutEqual(test, function() { log.subhead('foo'); }, '\n' + chalk.bold('foo') + '\n');
stdoutEqual(test, function() { log.subhead('%s', 'foo'); }, '\n' + chalk.bold('foo') + '\n');
stdoutEqual(test, function() { log.subhead(fooBuffer); }, '\n' + chalk.bold('foo') + '\n');

test.done();
},
Expand Down Expand Up @@ -209,13 +210,13 @@ exports['Log instance'] = {

stdoutEqual(test, function() {
log.writeflags(['a', 'b']);
}, 'Flags: ' + 'a'.cyan + ', ' + 'b'.cyan + '\n');
}, 'Flags: ' + chalk.cyan('a') + ', ' + chalk.cyan('b') + '\n');
stdoutEqual(test, function() {
log.writeflags(['a', 'b'], 'Prefix');
}, 'Prefix: ' + 'a'.cyan + ', ' + 'b'.cyan + '\n');
}, 'Prefix: ' + chalk.cyan('a') + ', ' + chalk.cyan('b') + '\n');
stdoutEqual(test, function() {
log.writeflags({a: true, b: false, c: 0, d: null}, 'Prefix');
}, 'Prefix: ' + 'a'.cyan + ', ' + 'b=false'.cyan + ', ' + 'c=0'.cyan + ', ' + 'd=null'.cyan + '\n');
}, 'Prefix: ' + chalk.cyan('a') + ', ' + chalk.cyan('b=false') + ', ' + chalk.cyan('c=0') + ', ' + chalk.cyan('d=null') + '\n');

test.done();
},
Expand Down Expand Up @@ -389,10 +390,10 @@ exports['Log instance'] = {
test.expect(4);
var log = new Log({debug: true});

stdoutEqual(test, function() { log.debug(); }, '[D] ' + ''.magenta + '\n');
stdoutEqual(test, function() { log.debug('foo'); }, '[D] ' + 'foo'.magenta + '\n');
stdoutEqual(test, function() { log.debug('%s', 'foo'); }, '[D] ' + 'foo'.magenta + '\n');
stdoutEqual(test, function() { log.debug(fooBuffer); }, '[D] ' + 'foo'.magenta + '\n');
stdoutEqual(test, function() { log.debug(); }, '[D] ' + chalk.magenta('') + '\n');
stdoutEqual(test, function() { log.debug('foo'); }, '[D] ' + chalk.magenta('foo') + '\n');
stdoutEqual(test, function() { log.debug('%s', 'foo'); }, '[D] ' + chalk.magenta('foo') + '\n');
stdoutEqual(test, function() { log.debug(fooBuffer); }, '[D] ' + chalk.magenta('foo') + '\n');

test.done();
},
Expand Down Expand Up @@ -436,15 +437,15 @@ exports['Log instance'] = {
test.expect(1);
var log = new Log({color: true});

stdoutEqual(test, function() { log.write('foo'.blue + 'bar'.underline); }, 'foo'.blue + 'bar'.underline);
stdoutEqual(test, function() { log.write(chalk.blue('foo') + chalk.underline('bar')); }, chalk.blue('foo') + chalk.underline('bar'));

test.done();
},
'options.color = false': function(test) {
test.expect(1);
var log = new Log({color: false});

stdoutEqual(test, function() { log.write('foo'.blue + 'bar'.underline); }, 'foobar');
stdoutEqual(test, function() { log.write(chalk.blue('foo') + chalk.underline('bar')); }, 'foobar');

test.done();
},
Expand Down