into.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict'
  2. const defaults = require('./defaults')
  3. const LogDraft = require('./LogDraft')
  4. const LineCountStream = require('./LineCountStream')
  5. /*
  6. * Injects DrafLog into a console object
  7. * call with a seccond parameter as 'true' to
  8. * Mock instalation, and add only the `draft` method
  9. * as a alias to `console.log`
  10. */
  11. module.exports = function into(console, extra) {
  12. // If extra is set to `true`, it's production mode
  13. var production = (extra === true ? true : false)
  14. // If production mode, mock api
  15. if (production) {
  16. // Mock draft and set is as console.log
  17. console.draft = function draft() {
  18. // Log this
  19. console.log.apply(null, arguments)
  20. // Return usual console.log method
  21. return console.log.bind(console)
  22. }
  23. return
  24. }
  25. // Transform stdout from console to LineCounter
  26. var lineCountStream = LineCountStream(console._stdout)
  27. console._stdout = lineCountStream
  28. // Can it bind to process.stdin automatically?
  29. if (defaults.stdinAutoBind) {
  30. lineCountStream.addLineListener(process.stdin)
  31. }
  32. // Add "draft" to console
  33. console.draft = console.draft || function draft() {
  34. // Create Draft at this point in time
  35. var logDraft = new LogDraft(console, 'log')
  36. // Log first
  37. logDraft.write.apply(logDraft, arguments)
  38. // Return update function
  39. return logDraft.update.bind(logDraft)
  40. }
  41. // Return the created Transform Stream
  42. return lineCountStream
  43. }