index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use strict';
  2. var ansi = require('ansi-styles');
  3. var stripAnsi = require('strip-ansi');
  4. var hasColor = require('has-color');
  5. var defineProps = Object.defineProperties;
  6. var chalk = module.exports;
  7. var styles = (function () {
  8. var ret = {};
  9. ansi.grey = ansi.gray;
  10. Object.keys(ansi).forEach(function (key) {
  11. ret[key] = {
  12. get: function () {
  13. this._styles.push(key);
  14. return this;
  15. }
  16. };
  17. });
  18. return ret;
  19. })();
  20. function init() {
  21. var ret = {};
  22. Object.keys(styles).forEach(function (name) {
  23. ret[name] = {
  24. get: function () {
  25. var obj = defineProps(function self() {
  26. var str = [].slice.call(arguments).join(' ');
  27. if (!chalk.enabled) {
  28. return str;
  29. }
  30. return self._styles.reduce(function (str, name) {
  31. var code = ansi[name];
  32. return str ? code.open + str + code.close : '';
  33. }, str);
  34. }, styles);
  35. obj._styles = [];
  36. return obj[name];
  37. }
  38. }
  39. });
  40. return ret;
  41. }
  42. defineProps(chalk, init());
  43. chalk.styles = ansi;
  44. chalk.stripColor = stripAnsi;
  45. chalk.supportsColor = hasColor;
  46. // detect mode if not set manually
  47. if (chalk.enabled === undefined) {
  48. chalk.enabled = chalk.supportsColor;
  49. }