暂无描述

index.js 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict';
  2. /*
  3. Initial code from https://github.com/gulpjs/gulp-util/blob/v3.0.6/lib/log.js
  4. */
  5. var gray = require('ansi-gray');
  6. var timestamp = require('time-stamp');
  7. var supportsColor = require('color-support');
  8. function hasFlag(flag) {
  9. return (process.argv.indexOf('--' + flag) !== -1);
  10. }
  11. function addColor(str) {
  12. if (hasFlag('no-color')) {
  13. return str;
  14. }
  15. if (hasFlag('color')) {
  16. return gray(str);
  17. }
  18. if (supportsColor()) {
  19. return gray(str);
  20. }
  21. return str;
  22. }
  23. function getTimestamp(){
  24. return '['+addColor(timestamp('HH:mm:ss'))+']';
  25. }
  26. function log(){
  27. var time = getTimestamp();
  28. process.stdout.write(time + ' ');
  29. console.log.apply(console, arguments);
  30. return this;
  31. }
  32. function info(){
  33. var time = getTimestamp();
  34. process.stdout.write(time + ' ');
  35. console.info.apply(console, arguments);
  36. return this;
  37. }
  38. function dir(){
  39. var time = getTimestamp();
  40. process.stdout.write(time + ' ');
  41. console.dir.apply(console, arguments);
  42. return this;
  43. }
  44. function warn(){
  45. var time = getTimestamp();
  46. process.stderr.write(time + ' ');
  47. console.warn.apply(console, arguments);
  48. return this;
  49. }
  50. function error(){
  51. var time = getTimestamp();
  52. process.stderr.write(time + ' ');
  53. console.error.apply(console, arguments);
  54. return this;
  55. }
  56. module.exports = log;
  57. module.exports.info = info;
  58. module.exports.dir = dir;
  59. module.exports.warn = warn;
  60. module.exports.error = error;