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.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict';
  2. var Console = require('console').Console;
  3. var gray = require('ansi-gray');
  4. var timestamp = require('time-stamp');
  5. var supportsColor = require('color-support');
  6. var nodeVersion = require('parse-node-version')(process.version);
  7. var colorDetectionOptions = {
  8. // If on Windows, ignore the isTTY check
  9. // This is due to AppVeyor (and thus probably common Windows platforms?) failing the check
  10. // TODO: If this is too broad, we can reduce it to an APPVEYOR env check
  11. ignoreTTY: (process.platform === 'win32'),
  12. };
  13. // Needed to add this because node 10 decided to start coloring log output randomly
  14. var console;
  15. if (nodeVersion.major >= 10) {
  16. // Node 10 also changed the way this is constructed
  17. console = new Console({
  18. stdout: process.stdout,
  19. stderr: process.stderr,
  20. colorMode: false,
  21. });
  22. } else {
  23. console = new Console(process.stdout, process.stderr);
  24. }
  25. function hasFlag(flag) {
  26. return (process.argv.indexOf('--' + flag) !== -1);
  27. }
  28. function addColor(str) {
  29. if (hasFlag('no-color')) {
  30. return str;
  31. }
  32. if (hasFlag('color')) {
  33. return gray(str);
  34. }
  35. if (supportsColor(colorDetectionOptions)) {
  36. return gray(str);
  37. }
  38. return str;
  39. }
  40. function getTimestamp() {
  41. return '[' + addColor(timestamp('HH:mm:ss')) + ']';
  42. }
  43. function log() {
  44. var time = getTimestamp();
  45. process.stdout.write(time + ' ');
  46. console.log.apply(console, arguments);
  47. return this;
  48. }
  49. function info() {
  50. var time = getTimestamp();
  51. process.stdout.write(time + ' ');
  52. console.info.apply(console, arguments);
  53. return this;
  54. }
  55. function dir() {
  56. var time = getTimestamp();
  57. process.stdout.write(time + ' ');
  58. console.dir.apply(console, arguments);
  59. return this;
  60. }
  61. function warn() {
  62. var time = getTimestamp();
  63. process.stderr.write(time + ' ');
  64. console.warn.apply(console, arguments);
  65. return this;
  66. }
  67. function error() {
  68. var time = getTimestamp();
  69. process.stderr.write(time + ' ');
  70. console.error.apply(console, arguments);
  71. return this;
  72. }
  73. module.exports = log;
  74. module.exports.info = info;
  75. module.exports.dir = dir;
  76. module.exports.warn = warn;
  77. module.exports.error = error;