Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. // run maps to lazy multistream
  2.  
  3. var execspawn = require('npm-execspawn')
  4. var duplexify = require('duplexify')
  5. var multistream = require('multistream')
  6. var parallel = require('parallel-multistream')
  7. var pumpify = require('pumpify')
  8. var duplexify = require('duplexify')
  9.  
  10. var toStream = function(cmd) {
  11. if (cmd.pipe) return cmd // already a stream
  12. var proc = execspawn(cmd)
  13. return duplexify(proc.stdin, proc.stdout)
  14. }
  15.  
  16. var runStream = function (runCommands) {
  17. var commands = runCommands.map(function (cmd) {
  18. return function () {
  19. // when multistream gets a function it creates the streams
  20. // lazily (like running commands in sequence)
  21. var s = toStream(cmd)
  22. if (s.end) s.end() // not writable
  23. return s
  24. }
  25. })
  26. return multistream(commands)
  27. }
  28.  
  29. var forkStream = function (forkCommands) {
  30. var commands = forkCommands.map(function (cmd) {
  31. // no lazyness here since we are forking
  32. var s = toStream(cmd)
  33. if (s.end) s.end() // not writable
  34. return s
  35. })
  36. return parallel(commands)
  37. }
  38.  
  39. var pipeStream = function(pipeCommands) {
  40. var commands = pipeCommands.map(function (cmd) {
  41. return toStream(cmd)
  42. })
  43.  
  44. var pipe = pumpify(commands)
  45. pipe.end() // first not writable
  46. return pipe
  47. }
  48.  
  49. // all of the above return streams (like a process)
  50.  
  51. // test.ds translated *roughly* into
  52.  
  53. // implicit runStream around the entire main pipeline
  54. var pipeline = runStream([
  55. runStream([
  56. 'echo hello',
  57. 'echo world'
  58. ]),
  59. forkStream([
  60. 'echo hello from fork',
  61. 'echo world from fork'
  62. ]),
  63. pipeStream([
  64. 'echo hello from pipe',
  65. 'cat -'
  66. ])
  67. ])
  68.  
  69. pipeline.pipe(process.stdout)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement