aboutsummaryrefslogtreecommitdiff
path: root/node_modules/readdirp/examples/grep.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-05-03 15:35:00 +0200
committerFlorian Dold <florian.dold@gmail.com>2017-05-03 15:35:00 +0200
commitde98e0b232509d5f40c135d540a70e415272ff85 (patch)
treea79222a5b58484ab3b80d18efcaaa7ccc4769b33 /node_modules/readdirp/examples/grep.js
parente0c9d480a73fa629c1e4a47d3e721f1d2d345406 (diff)
node_modules
Diffstat (limited to 'node_modules/readdirp/examples/grep.js')
-rw-r--r--node_modules/readdirp/examples/grep.js71
1 files changed, 71 insertions, 0 deletions
diff --git a/node_modules/readdirp/examples/grep.js b/node_modules/readdirp/examples/grep.js
new file mode 100644
index 000000000..01d5f2954
--- /dev/null
+++ b/node_modules/readdirp/examples/grep.js
@@ -0,0 +1,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)
+ ;