You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. 'use strict';
  2. var EventEmitter = require('events').EventEmitter;
  3. var fmt = require('util').format;
  4. var inherits = require('util').inherits;
  5. var chalk = require('chalk');
  6. var consoleStream = require('console-stream');
  7. var lpadAlign = require('lpad-align');
  8. /**
  9. * Initialize a new `Squeak`
  10. *
  11. * @param {Object} opts
  12. * @api public
  13. */
  14. function Squeak(opts) {
  15. if (!(this instanceof Squeak)) {
  16. return new Squeak(opts);
  17. }
  18. EventEmitter.call(this);
  19. this.opts = opts || {};
  20. this.align = this.opts.align !== false;
  21. this.indent = this.opts.indent || 2;
  22. this.separator = this.opts.separator || ' : ';
  23. this.stream = this.opts.stream || process.stderr || consoleStream();
  24. this.types = [];
  25. }
  26. inherits(Squeak, EventEmitter);
  27. module.exports = Squeak;
  28. /**
  29. * Write args to stream
  30. *
  31. * @api public
  32. */
  33. Squeak.prototype.write = function () {
  34. this.log([].slice.call(arguments));
  35. return this;
  36. };
  37. /**
  38. * Write args and new line to stream
  39. *
  40. * @api public
  41. */
  42. Squeak.prototype.writeln = function () {
  43. this.log([].slice.call(arguments));
  44. this.stream.write('\n');
  45. return this;
  46. };
  47. /**
  48. * Pad and write args to stream
  49. *
  50. * @api public
  51. */
  52. Squeak.prototype.writelpad = function () {
  53. var args = [].slice.call(arguments);
  54. var pad = new Array(this.indent + 1).join(' ');
  55. if (args.length) {
  56. args[0] = pad + args[0];
  57. }
  58. this.log(args);
  59. return this;
  60. };
  61. /**
  62. * Add type
  63. *
  64. * @param {String} type
  65. * @param {Object} opts
  66. * @param {Function} cb
  67. * @api public
  68. */
  69. Squeak.prototype.type = function (type, opts, cb) {
  70. if (!cb && typeof opts === 'function') {
  71. cb = opts;
  72. opts = {};
  73. }
  74. opts = opts || {};
  75. opts.color = opts.color || 'reset';
  76. opts.prefix = opts.prefix || type;
  77. this.types.push(opts.prefix);
  78. this[type] = function () {
  79. this.log([].slice.call(arguments), opts);
  80. if (cb) {
  81. cb();
  82. }
  83. };
  84. return this;
  85. };
  86. /**
  87. * End
  88. *
  89. * @param {Function} cb
  90. * @api public
  91. */
  92. Squeak.prototype.end = function (cb) {
  93. this.stream.write('\n');
  94. if (cb) {
  95. cb();
  96. }
  97. return this;
  98. };
  99. /**
  100. * Log
  101. *
  102. * @param {Array} args
  103. * @param {Object} opts
  104. * @api private
  105. */
  106. Squeak.prototype.log = function (args, opts) {
  107. opts = opts || {};
  108. var msg = [fmt.apply(null, args)];
  109. var color = opts.color;
  110. var prefix = opts.prefix;
  111. if (prefix) {
  112. var arr = this.align ? this.types : [prefix];
  113. msg.unshift(chalk[color](lpadAlign(prefix, arr, this.indent)));
  114. }
  115. this.stream.write(msg.join(this.separator));
  116. this.stream.write('\n');
  117. return this;
  118. };