check parent builder object for enabled status (#142)

This commit is contained in:
Josh 2017-01-30 04:10:02 -05:00 committed by GitHub
parent 5a69476142
commit 0d2144904b
2 changed files with 35 additions and 1 deletions

View file

@ -36,8 +36,20 @@ function build(_styles) {
return applyStyle.apply(builder, arguments); return applyStyle.apply(builder, arguments);
}; };
var self = this;
builder._styles = _styles; builder._styles = _styles;
builder.enabled = this.enabled;
Object.defineProperty(builder, 'enabled', {
enumerable: true,
get: function () {
return self.enabled;
},
set: function (v) {
self.enabled = v;
}
});
// __proto__ is used because we must return a function, but there is // __proto__ is used because we must return a function, but there is
// no way to create a function with a different prototype. // no way to create a function with a different prototype.
/* eslint-disable no-proto */ /* eslint-disable no-proto */

22
test.js
View file

@ -138,6 +138,28 @@ describe('chalk.enabled', function () {
assert.equal(chalk.red('foo'), 'foo'); assert.equal(chalk.red('foo'), 'foo');
chalk.enabled = true; chalk.enabled = true;
}); });
it('should enable/disable colors based on overall chalk enabled property, not individual instances', function () {
chalk.enabled = true;
var red = chalk.red;
assert.equal(red.enabled, true);
chalk.enabled = false;
assert.equal(red.enabled, chalk.enabled);
chalk.enabled = true;
});
it('should propagate enable/disable changes from child colors', function () {
chalk.enabled = true;
var red = chalk.red;
assert.equal(red.enabled, true);
assert.equal(chalk.enabled, true);
red.enabled = false;
assert.equal(red.enabled, false);
assert.equal(chalk.enabled, false);
chalk.enabled = true;
assert.equal(red.enabled, true);
assert.equal(chalk.enabled, true);
});
}); });
describe('chalk.constructor', function () { describe('chalk.constructor', function () {