paulmillr / readdirp

Recursive version of fs.readdir with streaming api.
https://paulmillr.com
MIT License
382 stars 53 forks source link

Fix stream sample for consistency with v3.0.0 changes #74

Closed rolanday closed 5 years ago

rolanday commented 5 years ago

The updated readme sample for the 2.x to 3.x change has two issues:

  1. Property accessor to stats object has been (appropriately) changed from stat to stats.
    const {path, stat: {size}} = entry; // wrong
    const {path, stats: {size}} = entry; // correct
  2. Also, to get the stats object one. must now set the alwaysStat option to true as per the change log comments.

so, like this instead...

const readdirp = require('readdirp');
readdirp('.', {
  alwaysStat: true,
})
  .on('data', (entry) => {
    const {path, stats: {size}} = entry;
    console.log(`${JSON.stringify({path, size})}`);
  })
  // Optionally call stream.destroy() in `warn()` in order to abort and cause 'close' to be emitted
  .on('warn', error => console.error('non-fatal error', error))
  .on('error', error => console.error('fatal error', error))
  .on('end', () => console.log('done'));