1 var fstream = require('../fstream.js')
2 var path = require('path')
4 var r = fstream.Reader({
5 path: path.dirname(__dirname),
7 return !this.basename.match(/^\./) &&
8 !this.basename.match(/^node_modules$/) &&
9 !this.basename.match(/^deep-copy$/)
13 var w = fstream.Writer({
14 path: path.resolve(__dirname, 'deep-copy'),
20 r.on('entry', appears)
21 r.on('ready', function () {
22 console.error('ready to begin!', r.path)
25 function appears (entry) {
26 console.error(indent + 'a %s appears!', entry.type, entry.basename, typeof entry.basename, entry)
28 console.error('FOGGY!')
31 console.error(p.depth, p.path, p._paused)
35 throw new Error('\u001b[mshould not have entries while foggy')
38 entry.on('data', missile(entry))
39 entry.on('end', runaway(entry))
40 entry.on('entry', appears)
44 function missile (entry) {
45 function liftFog (who) {
48 console.error('%s breaks the spell!', who && who.path)
50 console.error('the spell expires!')
52 console.error('\u001b[mthe fog lifts!\n')
55 if (entry._paused) entry.resume()
58 if (entry.type === 'Directory') {
60 entry.once('end', function () { ended = true })
62 // throw in some pathological pause()/resume() behavior
63 // just for extra fun.
64 process.nextTick(function () {
65 if (!foggy && !ended) { // && Math.random() < 0.3) {
66 console.error(indent + '%s casts a spell', entry.basename)
67 console.error('\na slowing fog comes over the battlefield...\n\u001b[32m')
69 entry.once('resume', liftFog)
70 foggy = setTimeout(liftFog, 10)
77 var e = Math.random() < 0.5
78 console.error(indent + '%s %s for %d damage!',
80 e ? 'is struck' : 'fires a chunk',
85 function runaway (entry) {
87 var e = Math.random() < 0.5
88 console.error(indent + '%s %s',
90 e ? 'turns to flee' : 'is vanquished!')
91 indent = indent.slice(0, -1)
95 w.on('entry', attacks)
96 // w.on('ready', function () { attacks(w) })
97 function attacks (entry) {
98 console.error(indent + '%s %s!', entry.basename,
99 entry.type === 'Directory' ? 'calls for backup' : 'attacks')
100 entry.on('entry', attacks)
104 r.on('end', function () {
105 if (foggy) clearTimeout(foggy)
106 console.error("\u001b[mIT'S OVER!!")
107 console.error('A WINNAR IS YOU!')
109 console.log('ok 1 A WINNAR IS YOU')
113 process.on('exit', function () {
114 console.log((ended ? '' : 'not ') + 'ok 2 ended')