2 var readdirp = require('..')
3 , util = require('util')
5 , path = require('path')
6 , es = require('event-stream')
9 function findLinesMatching (searchTerm) {
11 return es.through(function (entry) {
17 return es.mapSync(function (line) {
19 return ~line.indexOf(searchTerm) ? lineno + ': ' + line : undefined;
23 function aggregate () {
25 function write (data) {
26 matchingLines.push(data);
30 // drop files that had no matches
31 if (matchingLines.length) {
32 var result = { file: entry, lines: matchingLines };
34 // pass result on to file stream
35 fileStream.emit('data', result);
42 fs.createReadStream(entry.fullPath, { encoding: 'utf-8' })
44 // handle file contents line by line
47 // keep only the lines that matched the term
50 // aggregate all matching lines and delegate control back to the file stream
56 console.log('grepping for "arguments"');
58 // create a stream of all javascript files found in this and all sub directories
59 readdirp({ root: path.join(__dirname), fileFilter: '*.js' })
61 // find all lines matching the term for each file (if none found, that file is ignored)
62 .pipe(findLinesMatching('arguments'))
64 // format the results and output
66 es.mapSync(function (res) {
67 return '\n\n' + res.file.path + '\n\t' + res.lines.join('\n\t');