grep.js
1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
var readdirp = require('..')
, util = require('util')
, fs = require('fs')
, path = require('path')
, es = require('event-stream')
;
function findLinesMatching (searchTerm) {
return es.through(function (entry) {
var lineno = 0
, matchingLines = []
, fileStream = this;
function filter () {
return es.mapSync(function (line) {
lineno++;
return ~line.indexOf(searchTerm) ? lineno + ': ' + line : undefined;
});
}
function aggregate () {
return es.through(
function write (data) {
matchingLines.push(data);
}
, function end () {
// drop files that had no matches
if (matchingLines.length) {
var result = { file: entry, lines: matchingLines };
// pass result on to file stream
fileStream.emit('data', result);
}
this.emit('end');
}
);
}
fs.createReadStream(entry.fullPath, { encoding: 'utf-8' })
// handle file contents line by line
.pipe(es.split('\n'))
// keep only the lines that matched the term
.pipe(filter())
// aggregate all matching lines and delegate control back to the file stream
.pipe(aggregate())
;
});
}
console.log('grepping for "arguments"');
// create a stream of all javascript files found in this and all sub directories
readdirp({ root: path.join(__dirname), fileFilter: '*.js' })
// find all lines matching the term for each file (if none found, that file is ignored)
.pipe(findLinesMatching('arguments'))
// format the results and output
.pipe(
es.mapSync(function (res) {
return '\n\n' + res.file.path + '\n\t' + res.lines.join('\n\t');
})
)
.pipe(process.stdout)
;