a zip code crypto-currency system good for red ONLY

grep.js 1.8KB

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