Skip to content

Commit 839c706

Browse files
suzuki-shunsukeSBoudrias
authored andcommitted
Allow to change the log's 'colors' parameter (yeoman#93)
1 parent 9389040 commit 839c706

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

lib/util/log.js

+26-15
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,6 @@ const logSymbols = require('log-symbols');
1111
const step = ' ';
1212
let padding = ' ';
1313

14-
// Color -> status mappings
15-
const colors = {
16-
skip: 'yellow',
17-
force: 'yellow',
18-
create: 'green',
19-
invoke: 'bold',
20-
conflict: 'red',
21-
identical: 'cyan',
22-
info: 'gray'
23-
};
24-
2514
function pad(status) {
2615
const max = 'identical'.length;
2716
const delta = max - status.length;
@@ -44,7 +33,24 @@ function formatter(msg, ctx) {
4433
return msg;
4534
}
4635

47-
module.exports = () => {
36+
const getDefaultColors = () => ({
37+
skip: 'yellow',
38+
force: 'yellow',
39+
create: 'green',
40+
invoke: 'bold',
41+
conflict: 'red',
42+
identical: 'cyan',
43+
info: 'gray'
44+
});
45+
46+
const initParams = params => {
47+
params = params || {};
48+
return Object.assign(
49+
{}, params, {
50+
colors: Object.assign(getDefaultColors(), params.colors || {})});
51+
};
52+
53+
module.exports = params => {
4854
// `this.log` is a [logref](https://github.com/mikeal/logref)
4955
// compatible logger, with an enhanced API.
5056
//
@@ -61,6 +67,9 @@ module.exports = () => {
6167
// - msg - The message to show up
6268
// - context - The optional context to escape the message against
6369
//
70+
// @param {Object} params
71+
// @param {Object} params.colors status mappings
72+
//
6473
// Returns the logger
6574
function log(msg, ctx) {
6675
msg = msg || '';
@@ -76,6 +85,8 @@ module.exports = () => {
7685

7786
_.extend(log, EventEmitter.prototype);
7887

88+
params = initParams(params);
89+
7990
// A simple write method, with formatted message.
8091
//
8192
// Returns the logger
@@ -114,7 +125,7 @@ module.exports = () => {
114125

115126
/* eslint-disable no-loop-func */
116127
// TODO: Fix this ESLint warning
117-
for (const status of Object.keys(colors)) {
128+
for (const status of Object.keys(params.colors)) {
118129
// Each predefined status has its logging method utility, handling
119130
// status color and padding before the usual `.write()`
120131
//
@@ -128,7 +139,7 @@ module.exports = () => {
128139
// .write()
129140
// .ok('This is ok');
130141
//
131-
// The list of status and mapping colors
142+
// The list of default status and mapping colors
132143
//
133144
// skip yellow
134145
// force yellow
@@ -140,7 +151,7 @@ module.exports = () => {
140151
//
141152
// Returns the logger
142153
log[status] = function () {
143-
const color = colors[status];
154+
const color = params.colors[status];
144155
this.write(chalk[color](pad(status))).write(padding);
145156
this.write(util.format.apply(util, arguments) + '\n');
146157
return this;

test/adapter.js

+19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const inquirer = require('inquirer');
44
const sinon = require('sinon');
55
const logSymbols = require('log-symbols');
66
const TerminalAdapter = require('../lib/adapter');
7+
const createLog = require('../lib/util/log');
78

89
describe('TerminalAdapter', () => {
910
beforeEach(function () {
@@ -146,4 +147,22 @@ describe('TerminalAdapter', () => {
146147
it('#info()');
147148
});
148149
});
150+
151+
describe('#log', () => {
152+
const funcs = ['write', 'writeln', 'ok', 'error', 'table'];
153+
const defaultColors = [
154+
'skip', 'force', 'create', 'invoke', 'conflict', 'identical', 'info'];
155+
it('log has functions', function () {
156+
this.adapter.log = createLog();
157+
funcs.concat(defaultColors).forEach(k => {
158+
assert.equal(typeof this.adapter.log[k], 'function');
159+
});
160+
});
161+
it('log can be added custom status', function () {
162+
this.adapter.log = createLog({colors: {merge: 'yellow'}});
163+
funcs.concat(defaultColors, ['merge']).forEach(k => {
164+
assert.equal(typeof this.adapter.log[k], 'function');
165+
});
166+
});
167+
});
149168
});

0 commit comments

Comments
 (0)