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$/) &&
10 !this.basename.match(/^filter-copy$/)
14 // this writer will only write directories
15 var w = fstream.Writer({
16 path: path.resolve(__dirname, 'filter-copy'),
19 return this.type === 'Directory'
25 r.on('entry', appears)
26 r.on('ready', function () {
27 console.error('ready to begin!', r.path)
30 function appears (entry) {
31 console.error(indent + 'a %s appears!', entry.type, entry.basename, typeof entry.basename)
33 console.error('FOGGY!')
36 console.error(p.depth, p.path, p._paused)
40 throw new Error('\u001b[mshould not have entries while foggy')
43 entry.on('data', missile(entry))
44 entry.on('end', runaway(entry))
45 entry.on('entry', appears)
49 function missile (entry) {
50 function liftFog (who) {
53 console.error('%s breaks the spell!', who && who.path)
55 console.error('the spell expires!')
57 console.error('\u001b[mthe fog lifts!\n')
60 if (entry._paused) entry.resume()
63 if (entry.type === 'Directory') {
65 entry.once('end', function () { ended = true })
67 // throw in some pathological pause()/resume() behavior
68 // just for extra fun.
69 process.nextTick(function () {
70 if (!foggy && !ended) { // && Math.random() < 0.3) {
71 console.error(indent + '%s casts a spell', entry.basename)
72 console.error('\na slowing fog comes over the battlefield...\n\u001b[32m')
74 entry.once('resume', liftFog)
75 foggy = setTimeout(liftFog, 1000)
82 var e = Math.random() < 0.5
83 console.error(indent + '%s %s for %d damage!',
85 e ? 'is struck' : 'fires a chunk',
90 function runaway (entry) {
92 var e = Math.random() < 0.5
93 console.error(indent + '%s %s',
95 e ? 'turns to flee' : 'is vanquished!')
96 indent = indent.slice(0, -1)
100 w.on('entry', attacks)
101 // w.on('ready', function () { attacks(w) })
102 function attacks (entry) {
103 console.error(indent + '%s %s!', entry.basename,
104 entry.type === 'Directory' ? 'calls for backup' : 'attacks')
105 entry.on('entry', attacks)
110 r.on('end', function () {
111 if (foggy) clearTimeout(foggy)
112 console.error("\u001b[mIT'S OVER!!")
113 console.error('A WINNAR IS YOU!')
115 console.log('ok ' + (i++) + ' A WINNAR IS YOU')
117 // now go through and verify that everything in there is a dir.
118 var p = path.resolve(__dirname, 'filter-copy')
119 var checker = fstream.Reader({ path: p })
120 checker.checker = true
121 checker.on('child', function (e) {
122 var ok = e.type === 'Directory'
123 console.log((ok ? '' : 'not ') + 'ok ' + (i++) +
124 ' should be a dir: ' +
125 e.path.substr(checker.path.length + 1))
129 process.on('exit', function () {
130 console.log((ended ? '' : 'not ') + 'ok ' + (i) + ' ended')
131 console.log('1..' + i)