Front end of the Slack clone application.

pipe.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. var fstream = require('../fstream.js')
  2. var path = require('path')
  3. var r = fstream.Reader({
  4. path: path.dirname(__dirname),
  5. filter: function () {
  6. return !this.basename.match(/^\./) &&
  7. !this.basename.match(/^node_modules$/) &&
  8. !this.basename.match(/^deep-copy$/)
  9. }
  10. })
  11. var w = fstream.Writer({
  12. path: path.resolve(__dirname, 'deep-copy'),
  13. type: 'Directory'
  14. })
  15. var indent = ''
  16. r.on('entry', appears)
  17. r.on('ready', function () {
  18. console.error('ready to begin!', r.path)
  19. })
  20. function appears (entry) {
  21. console.error(indent + 'a %s appears!', entry.type, entry.basename, typeof entry.basename, entry)
  22. if (foggy) {
  23. console.error('FOGGY!')
  24. var p = entry
  25. do {
  26. console.error(p.depth, p.path, p._paused)
  27. p = p.parent
  28. } while (p)
  29. throw new Error('\u001b[mshould not have entries while foggy')
  30. }
  31. indent += '\t'
  32. entry.on('data', missile(entry))
  33. entry.on('end', runaway(entry))
  34. entry.on('entry', appears)
  35. }
  36. var foggy
  37. function missile (entry) {
  38. function liftFog (who) {
  39. if (!foggy) return
  40. if (who) {
  41. console.error('%s breaks the spell!', who && who.path)
  42. } else {
  43. console.error('the spell expires!')
  44. }
  45. console.error('\u001b[mthe fog lifts!\n')
  46. clearTimeout(foggy)
  47. foggy = null
  48. if (entry._paused) entry.resume()
  49. }
  50. if (entry.type === 'Directory') {
  51. var ended = false
  52. entry.once('end', function () { ended = true })
  53. return function (c) {
  54. // throw in some pathological pause()/resume() behavior
  55. // just for extra fun.
  56. process.nextTick(function () {
  57. if (!foggy && !ended) { // && Math.random() < 0.3) {
  58. console.error(indent + '%s casts a spell', entry.basename)
  59. console.error('\na slowing fog comes over the battlefield...\n\u001b[32m')
  60. entry.pause()
  61. entry.once('resume', liftFog)
  62. foggy = setTimeout(liftFog, 10)
  63. }
  64. })
  65. }
  66. }
  67. return function (c) {
  68. var e = Math.random() < 0.5
  69. console.error(indent + '%s %s for %d damage!',
  70. entry.basename,
  71. e ? 'is struck' : 'fires a chunk',
  72. c.length)
  73. }
  74. }
  75. function runaway (entry) {
  76. return function () {
  77. var e = Math.random() < 0.5
  78. console.error(indent + '%s %s',
  79. entry.basename,
  80. e ? 'turns to flee' : 'is vanquished!')
  81. indent = indent.slice(0, -1)
  82. }
  83. }
  84. w.on('entry', attacks)
  85. // w.on('ready', function () { attacks(w) })
  86. function attacks (entry) {
  87. console.error(indent + '%s %s!', entry.basename,
  88. entry.type === 'Directory' ? 'calls for backup' : 'attacks')
  89. entry.on('entry', attacks)
  90. }
  91. var ended = false
  92. r.on('end', function () {
  93. if (foggy) clearTimeout(foggy)
  94. console.error("\u001b[mIT'S OVER!!")
  95. console.error('A WINNAR IS YOU!')
  96. console.log('ok 1 A WINNAR IS YOU')
  97. ended = true
  98. })
  99. process.on('exit', function () {
  100. console.log((ended ? '' : 'not ') + 'ok 2 ended')
  101. console.log('1..2')
  102. })
  103. r.pipe(w)