filter-pipe.js 3.4 KB

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