Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for NO_COLOR environment variable #321

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
9 changes: 9 additions & 0 deletions colorize.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const { EnvManager } = require('./env-manager');
const colors = require('@colors/colors/safe');
const { LEVEL, MESSAGE } = require('triple-beam');

Expand All @@ -24,6 +25,10 @@ class Colorizer {
this.addColors(opts.colors);
}

if (!opts.env) {
opts.env = new EnvManager();
}

this.options = opts;
}

Expand Down Expand Up @@ -65,6 +70,10 @@ class Colorizer {
message = level;
}

if (this.options.env.isColorDisabled) {
return message;
}

//
// If the color for the level is just a string
// then attempt to colorize the message with it.
Expand Down
22 changes: 22 additions & 0 deletions env-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable no-process-env */
'use strict';

class EnvManager {
constructor(
opts = {}
) {
if (opts.disableColor === undefined) {
opts.disableColor = process.env.NO_COLOR &&
process.env.NO_COLOR !== '0' &&
process.env.NO_COLOR !== 'false';
}

this.opts = opts;
}

get isColorDisabled() {
return this.opts.disableColor;
}
}

module.exports.EnvManager = EnvManager;
7 changes: 6 additions & 1 deletion test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ const {
infoify,
setupLevels
} = require('./helpers');
const { EnvManager } = require('../env-manager');
const { LEVEL, MESSAGE } = require('triple-beam');

describe('cli', () => {
before(setupLevels);

it('cli() (default) sets info[MESSAGE]', assumeFormatted(
cli(),
cli({
env: new EnvManager({
disableColor: false
})
}),
infoify({ level: 'info', message: 'whatever' }),
(info) => {
assume(info.level).is.a('string');
Expand Down
131 changes: 123 additions & 8 deletions test/colorize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ const {
infoify,
setupLevels
} = require('./helpers');
const { EnvManager } = require('../env-manager');

const ENABLED_COLOR_ENV = new EnvManager({ disableColor: false });
const DISABLED_COLOR_ENV = new EnvManager({ disableColor: true });

describe('colorize', () => {
before(setupLevels);

it('colorize() (default)', assumeFormatted(
colorize(),
colorize({ env: ENABLED_COLOR_ENV }),
infoify({ level: 'info', message: 'whatever' }),
info => {
assume(info.level).is.a('string');
Expand All @@ -26,8 +30,19 @@ describe('colorize', () => {
}
));

it('not output color colorize() (default) when NO_COLOR env var is enabled', assumeFormatted(
colorize({ env: DISABLED_COLOR_ENV }),
infoify({ level: 'info', message: 'whatever' }),
info => {
assume(info.level).is.a('string');
assume(info.message).is.a('string');
assume(info.level).equals('info');
assume(info.message).equals('whatever');
}
));

it('colorize({ level: true })', assumeFormatted(
colorize({ level: true }),
colorize({ level: true, env: ENABLED_COLOR_ENV }),
infoify({ level: 'info', message: 'whatever' }),
info => {
assume(info.level).is.a('string');
Expand All @@ -37,8 +52,19 @@ describe('colorize', () => {
}
));

it('not output color colorize({ level: true }) when NO_COLOR env var is enabled', assumeFormatted(
colorize({ level: true, env: DISABLED_COLOR_ENV }),
infoify({ level: 'info', message: 'whatever' }),
info => {
assume(info.level).is.a('string');
assume(info.message).is.a('string');
assume(info.level).equals('info');
assume(info.message).equals('whatever');
}
));

it('colorize{ message: true })', assumeFormatted(
colorize({ message: true }),
colorize({ message: true, env: ENABLED_COLOR_ENV }),
infoify({ level: 'info', message: 'whatever' }),
info => {
assume(info.level).is.a('string');
Expand All @@ -48,8 +74,19 @@ describe('colorize', () => {
}
));

it('not output color colorize{ message: true }) when NO_COLOR env var is enabled', assumeFormatted(
colorize({ message: true, env: DISABLED_COLOR_ENV }),
infoify({ level: 'info', message: 'whatever' }),
info => {
assume(info.level).is.a('string');
assume(info.message).is.a('string');
assume(info.level).equals('info');
assume(info.message).equals('whatever');
}
));

it('colorize({ level: true, message: true })', assumeFormatted(
colorize({ level: true, message: true }),
colorize({ level: true, message: true, env: ENABLED_COLOR_ENV }),
infoify({ level: 'info', message: 'whatever' }),
info => {
assume(info.level).is.a('string');
Expand All @@ -59,8 +96,19 @@ describe('colorize', () => {
}
));

it('not output color colorize({ level: true, message: true }) when NO_COLOR env var is enabled', assumeFormatted(
colorize({ level: true, message: true, env: DISABLED_COLOR_ENV }),
infoify({ level: 'info', message: 'whatever' }),
info => {
assume(info.level).is.a('string');
assume(info.message).is.a('string');
assume(info.level).equals('info');
assume(info.message).equals('whatever');
}
));

it('colorize({ all: true })', assumeFormatted(
colorize({ all: true }),
colorize({ all: true, env: ENABLED_COLOR_ENV }),
infoify({ level: 'info', message: 'whatever' }),
info => {
assume(info.level).is.a('string');
Expand All @@ -70,8 +118,19 @@ describe('colorize', () => {
}
));

it('not output color colorize({ all: true }) when NO_COLOR env var is enabled', assumeFormatted(
colorize({ all: true, env: DISABLED_COLOR_ENV }),
infoify({ level: 'info', message: 'whatever' }),
info => {
assume(info.level).is.a('string');
assume(info.message).is.a('string');
assume(info.level).equals('info');
assume(info.message).equals('whatever');
}
));

it('colorize({ all: true }) [custom message]', assumeFormatted(
colorize({ all: true }),
colorize({ all: true, env: ENABLED_COLOR_ENV }),
{
level: 'info',
[LEVEL]: 'info',
Expand All @@ -88,15 +147,47 @@ describe('colorize', () => {
}
));

it('not output color colorize({ all: true }) [custom message] when NO_COLOR env var is enabled', assumeFormatted(
colorize({ all: true, env: DISABLED_COLOR_ENV }),
{
level: 'info',
[LEVEL]: 'info',
message: 'whatever',
[MESSAGE]: '[info] whatever custom'
},
info => {
assume(info.level).is.a('string');
assume(info.message).is.a('string');
assume(info[LEVEL]).equals('info');
assume(info.level).equals('info');
assume(info.message).equals('whatever');
assume(info[MESSAGE]).equals('[info] whatever custom');
}
));

it('colorizes when LEVEL !== level', assumeFormatted(
colorize(),
colorize({
env: ENABLED_COLOR_ENV
}),
{ [LEVEL]: 'info', level: 'INFO', message: 'whatever' },
info => {
assume(info.level).is.a('string');
assume(info.message).is.a('string');
assume(info.level).equals(colors.green('INFO'));
}
));

it('not output color colorizes when LEVEL !== level and NO_COLOR env var is enabled', assumeFormatted(
colorize({
env: DISABLED_COLOR_ENV
}),
{ [LEVEL]: 'info', level: 'INFO', message: 'whatever' },
info => {
assume(info.level).is.a('string');
assume(info.message).is.a('string');
assume(info.level).not.equals(colors.green('INFO'));
}
));
});

describe('Colorizer', () => {
Expand Down Expand Up @@ -130,7 +221,9 @@ describe('Colorizer', () => {
});

describe('#colorize(LEVEL, level, message)', () => {
const instance = new Colorizer();
const instance = new Colorizer({
env: ENABLED_COLOR_ENV
});

it('colorize(level) [single color]', () => {
assume(instance.colorize('weird', 'weird')).equals(colors.cyan('weird'));
Expand All @@ -152,4 +245,26 @@ describe('Colorizer', () => {
);
});
});

describe('not output color #colorize(LEVEL, level, message) when NO_COLOR env var is enabled', () => {
const instance = new Colorizer({
env: DISABLED_COLOR_ENV
});

it('colorize(level) [single color]', () => {
assume(instance.colorize('weird', 'weird')).equals('weird');
});

it('colorize(level) [multiple colors]', () => {
assume(instance.colorize('multiple', 'multiple')).equals('multiple');
});

it('colorize(level, message) [single color]', () => {
assume(instance.colorize('weird', 'weird', 'message')).equals('message');
});

it('colorize(level, message) [multiple colors]', () => {
assume(instance.colorize('multiple', 'multiple', 'message')).equals('message');
});
});
});
46 changes: 46 additions & 0 deletions test/env-manager.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-disable no-process-env */
'use strict';

const assume = require('assume');
const { EnvManager } = require('../env-manager');

describe('EnvManager', () => {
it('should disable color if NO_COLOR is set to a truthy value', () => {
process.env.NO_COLOR = '1';
const envManager = new EnvManager();
assume(envManager.isColorDisabled).to.be.true;
delete process.env.NO_COLOR;
});

it('should not disable color if NO_COLOR is set to "0"', () => {
process.env.NO_COLOR = '0';
const envManager = new EnvManager();
assume(envManager.isColorDisabled).to.be.false;
delete process.env.NO_COLOR;
});

it('should not disable color if NO_COLOR is set to "false"', () => {
process.env.NO_COLOR = 'false';
const envManager = new EnvManager();
assume(envManager.isColorDisabled).to.be.false;
delete process.env.NO_COLOR;
});

it('should not disable color if NO_COLOR is not set', () => {
delete process.env.NO_COLOR;
const envManager = new EnvManager();
assume(envManager.isColorDisabled).to.be.false;
});

it('should use the disableColor option if provided', () => {
const envManager = new EnvManager({ disableColor: true });
assume(envManager.isColorDisabled).to.be.true;
});

it('should override the disableColor option with NO_COLOR environment variable', () => {
process.env.NO_COLOR = '0';
const envManager = new EnvManager({ disableColor: true });
assume(envManager.isColorDisabled).to.be.false;
delete process.env.NO_COLOR;
});
});
Loading
Loading