pipe.js 3.2 KB

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