default_stream.js 725 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. var through = require('through');
  3. var fs = require('fs');
  4. module.exports = function () {
  5. var line = '';
  6. var stream = through(write, flush);
  7. return stream;
  8. function write(buf) {
  9. for (var i = 0; i < buf.length; i++) {
  10. var c = typeof buf === 'string'
  11. ? buf.charAt(i)
  12. : String.fromCharCode(buf[i]);
  13. if (c === '\n') {
  14. flush();
  15. } else {
  16. line += c;
  17. }
  18. }
  19. }
  20. function flush() {
  21. if (fs.writeSync && (/^win/).test(process.platform)) {
  22. try {
  23. fs.writeSync(1, line + '\n');
  24. } catch (e) {
  25. stream.emit('error', e);
  26. }
  27. } else {
  28. try {
  29. console.log(line); // eslint-disable-line no-console
  30. } catch (e) {
  31. stream.emit('error', e);
  32. }
  33. }
  34. line = '';
  35. }
  36. };