kevinbeaty / fs-promise

[DEPRECATED] Use mz or fs-extra^3 with Promise support
https://www.npmjs.com/package/fs-extra
MIT License
170 stars 13 forks source link

add readable stream support (handle walk method properly) #9

Closed pcdevil closed 8 years ago

pcdevil commented 8 years ago

This pull request adds support for the fs-extra's walk method.

Currently the fs-promise will use walk() method as any other and creates a Promise if it's called. Unfortunately this is a special method of fs-extra and returns a Readable stream. The resolver will never call either the resolve() or the reject() method and the promise will hang forever because the nature of the stream demands to register listeners to events like 'data' or 'error'.

See example:

var fse = require('fs-extra');
var fsp = require('fs-promise');
const DIR = '/tmp';

var fseItems = [];
var fseWalking = fse.walk(DIR);
// copied from fs-extra documentation
fseWalking
    .on('data', function (item) {
        fseItems.push(item.path);
    })
    .on('end', function () {
        // will print all items in the directory
        console.dir(fseItems);
    })
    .on('error', function (error) {
        // will print the error
        console.log(error);
    });

var fspWalking = fsp.walk(DIR);
fspWalking
    .then(function () {
        // never called
        console.log('.then');
        console.log(arguments.length);
        console.log(arguments);
    })
    .catch(function () {
        // never called
        console.log('.catch');
        console.log(arguments.length);
        console.log(arguments);
    });

console.log(fspWalking); // pending

With this commit the fs-promise will have a basic support for it and the example will execute as expected.

Because walk() returns the files one-by-one the wrapper method creates an array and will return the collection after the end event is dispatched. This needs to be supervised if the fs-extra package introduces a new method with stream support but right now I think this will do the trick.

kevinbeaty commented 8 years ago

Thanks!

pcdevil commented 8 years ago

It's my pleasure, thanks!

Can you please publish a new version of the package on npm as well?

kevinbeaty commented 8 years ago

Published 0.5.0 that includes this PR

pcdevil commented 8 years ago

Thank you, you are awesome!