Template Upload
This commit is contained in:
37
node_modules/readdirp/examples/Readme.md
generated
vendored
Normal file
37
node_modules/readdirp/examples/Readme.md
generated
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
# readdirp examples
|
||||
|
||||
## How to run the examples
|
||||
|
||||
Assuming you installed readdirp (`npm install readdirp`), you can do the following:
|
||||
|
||||
1. `npm explore readdirp`
|
||||
2. `cd examples`
|
||||
3. `npm install`
|
||||
|
||||
At that point you can run the examples with node, i.e., `node grep`.
|
||||
|
||||
## stream api
|
||||
|
||||
[stream-api.js](https://github.com/thlorenz/readdirp/blob/master/examples/stream-api.js)
|
||||
|
||||
Demonstrates error and data handling by listening to events emitted from the readdirp stream.
|
||||
|
||||
## stream api pipe
|
||||
|
||||
[stream-api-pipe.js](https://github.com/thlorenz/readdirp/blob/master/examples/stream-api-pipe.js)
|
||||
|
||||
Demonstrates error handling by listening to events emitted from the readdirp stream and how to pipe the data stream into
|
||||
another destination stream.
|
||||
|
||||
## grep
|
||||
|
||||
[grep.js](https://github.com/thlorenz/readdirp/blob/master/examples/grep.js)
|
||||
|
||||
Very naive implementation of grep, for demonstration purposes only.
|
||||
|
||||
## using callback api
|
||||
|
||||
[callback-api.js](https://github.com/thlorenz/readdirp/blob/master/examples/callback-api.js)
|
||||
|
||||
Shows how to pass callbacks in order to handle errors and/or data.
|
||||
|
10
node_modules/readdirp/examples/callback-api.js
generated
vendored
Normal file
10
node_modules/readdirp/examples/callback-api.js
generated
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
var readdirp = require('..');
|
||||
|
||||
readdirp({ root: '.', fileFilter: '*.js' }, function (errors, res) {
|
||||
if (errors) {
|
||||
errors.forEach(function (err) {
|
||||
console.error('Error: ', err);
|
||||
});
|
||||
}
|
||||
console.log('all javascript files', res);
|
||||
});
|
71
node_modules/readdirp/examples/grep.js
generated
vendored
Normal file
71
node_modules/readdirp/examples/grep.js
generated
vendored
Normal file
@ -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)
|
||||
;
|
9
node_modules/readdirp/examples/package.json
generated
vendored
Normal file
9
node_modules/readdirp/examples/package.json
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "readdirp-examples",
|
||||
"version": "0.0.0",
|
||||
"description": "Examples for readdirp.",
|
||||
"dependencies": {
|
||||
"tap-stream": "~0.1.0",
|
||||
"event-stream": "~3.0.7"
|
||||
}
|
||||
}
|
19
node_modules/readdirp/examples/stream-api-pipe.js
generated
vendored
Normal file
19
node_modules/readdirp/examples/stream-api-pipe.js
generated
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
var readdirp = require('..')
|
||||
, path = require('path')
|
||||
, through = require('through2')
|
||||
|
||||
// print out all JavaScript files along with their size
|
||||
readdirp({ root: path.join(__dirname), fileFilter: '*.js' })
|
||||
.on('warn', function (err) { console.error('non-fatal error', err); })
|
||||
.on('error', function (err) { console.error('fatal error', err); })
|
||||
.pipe(through.obj(function (entry, _, cb) {
|
||||
this.push({ path: entry.path, size: entry.stat.size });
|
||||
cb();
|
||||
}))
|
||||
.pipe(through.obj(
|
||||
function (res, _, cb) {
|
||||
this.push(JSON.stringify(res) + '\n');
|
||||
cb();
|
||||
})
|
||||
)
|
||||
.pipe(process.stdout);
|
15
node_modules/readdirp/examples/stream-api.js
generated
vendored
Normal file
15
node_modules/readdirp/examples/stream-api.js
generated
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
var readdirp = require('..')
|
||||
, path = require('path');
|
||||
|
||||
readdirp({ root: path.join(__dirname), fileFilter: '*.js' })
|
||||
.on('warn', function (err) {
|
||||
console.error('something went wrong when processing an entry', err);
|
||||
})
|
||||
.on('error', function (err) {
|
||||
console.error('something went fatally wrong and the stream was aborted', err);
|
||||
})
|
||||
.on('data', function (entry) {
|
||||
console.log('%s is ready for processing', entry.path);
|
||||
// process entry here
|
||||
});
|
||||
|
Reference in New Issue
Block a user