index.js 981 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var fs = require('fs');
  2. var Writable = require('readable-stream/writable');
  3. var exists = function(path) {
  4. try {
  5. return fs.existsSync(path);
  6. } catch (err) {
  7. return false;
  8. }
  9. };
  10. module.exports = function() {
  11. var s = new Writable({highWaterMark:0});
  12. var cb;
  13. var data;
  14. var tries = 0;
  15. var offset = 0;
  16. var write = function() {
  17. fs.write(1, data, offset, data.length - offset, null, onwrite);
  18. };
  19. var onwrite = function(err, written) {
  20. if (err && err.code === 'EPIPE') return cb()
  21. if (err && err.code === 'EAGAIN' && tries++ < 30) return setTimeout(write, 10);
  22. if (err) return cb(err);
  23. tries = 0;
  24. if (offset + written >= data.length) return cb();
  25. offset += written;
  26. write();
  27. };
  28. s._write = function(_data, enc, _cb) {
  29. offset = 0;
  30. cb = _cb;
  31. data = _data;
  32. write();
  33. };
  34. s._isStdio = true;
  35. s.isTTY = process.stdout.isTTY;
  36. s.on('finish', function() {
  37. fs.close(1, function(err) {
  38. if (err) s.emit('error', err);
  39. });
  40. });
  41. return s;
  42. }();