Initial commit
[yaffs-website] / node_modules / fstream / examples / filter-pipe.js
1 var fstream = require('../fstream.js')
2 var path = require('path')
3
4 var r = fstream.Reader({
5   path: path.dirname(__dirname),
6   filter: function () {
7     return !this.basename.match(/^\./) &&
8       !this.basename.match(/^node_modules$/) &&
9       !this.basename.match(/^deep-copy$/) &&
10       !this.basename.match(/^filter-copy$/)
11   }
12 })
13
14 // this writer will only write directories
15 var w = fstream.Writer({
16   path: path.resolve(__dirname, 'filter-copy'),
17   type: 'Directory',
18   filter: function () {
19     return this.type === 'Directory'
20   }
21 })
22
23 var indent = ''
24
25 r.on('entry', appears)
26 r.on('ready', function () {
27   console.error('ready to begin!', r.path)
28 })
29
30 function appears (entry) {
31   console.error(indent + 'a %s appears!', entry.type, entry.basename, typeof entry.basename)
32   if (foggy) {
33     console.error('FOGGY!')
34     var p = entry
35     do {
36       console.error(p.depth, p.path, p._paused)
37       p = p.parent
38     } while (p)
39
40     throw new Error('\u001b[mshould not have entries while foggy')
41   }
42   indent += '\t'
43   entry.on('data', missile(entry))
44   entry.on('end', runaway(entry))
45   entry.on('entry', appears)
46 }
47
48 var foggy
49 function missile (entry) {
50   function liftFog (who) {
51     if (!foggy) return
52     if (who) {
53       console.error('%s breaks the spell!', who && who.path)
54     } else {
55       console.error('the spell expires!')
56     }
57     console.error('\u001b[mthe fog lifts!\n')
58     clearTimeout(foggy)
59     foggy = null
60     if (entry._paused) entry.resume()
61   }
62
63   if (entry.type === 'Directory') {
64     var ended = false
65     entry.once('end', function () { ended = true })
66     return function (c) {
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')
73           entry.pause()
74           entry.once('resume', liftFog)
75           foggy = setTimeout(liftFog, 1000)
76         }
77       })
78     }
79   }
80
81   return function (c) {
82     var e = Math.random() < 0.5
83     console.error(indent + '%s %s for %d damage!',
84       entry.basename,
85       e ? 'is struck' : 'fires a chunk',
86       c.length)
87   }
88 }
89
90 function runaway (entry) {
91   return function () {
92     var e = Math.random() < 0.5
93     console.error(indent + '%s %s',
94       entry.basename,
95       e ? 'turns to flee' : 'is vanquished!')
96     indent = indent.slice(0, -1)
97   }
98 }
99
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)
106 }
107
108 var ended = false
109 var i = 1
110 r.on('end', function () {
111   if (foggy) clearTimeout(foggy)
112   console.error("\u001b[mIT'S OVER!!")
113   console.error('A WINNAR IS YOU!')
114
115   console.log('ok ' + (i++) + ' A WINNAR IS YOU')
116   ended = true
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))
126   })
127 })
128
129 process.on('exit', function () {
130   console.log((ended ? '' : 'not ') + 'ok ' + (i) + ' ended')
131   console.log('1..' + i)
132 })
133
134 r.pipe(w)