AvianFlu / ncp

Asynchronous recursive file copying with Node.js.
MIT License
680 stars 103 forks source link

Filter is not working #130

Open Exocomp opened 6 years ago

Exocomp commented 6 years ago

Here is my example, I've set it up based on the documentation but can't get it to work. In this example index.html from folder1 should be copied to folder2.

node -v v8.9.4

var options = {};
options.filter = new RegExp('index.html');

ncp('folder1', '../folder2', options, 
    function (err) {
        if (err) {
            return console.error(err);
        }
        console.log('done!');
    }
);
pgraham commented 6 years ago

This happens because ncp also applies the filter to directories. So, in your example, the filter will first be applied to <abs-path-to>/folder1. Since this path doesn't contain the string index.html processing stops and nothing is copied.

It would be nice to either change the behaviour of the filter to not apply to directories or to add another option (say, fileFilter) which is only applied to the basename of file paths.

Melmoth-the-Wanderer commented 5 years ago

So what is the solution here? When you add folder name to the regexp it will copy all files within. Any way to specify folder as a target and filter out certain files only?

zawor commented 4 years ago

well i managed to stumble upon the same issue and my take on that was like in answer to SO: node ncp filter not working

hope it comes useful to someone

MrMjauh commented 4 years ago

Looking at the source code it seems like the filter function takes in a function too, I am using it like this to copy only .css files

    ncp(workDir, outputPath, {filter: (source) => {
        if (fs.lstatSync(source).isDirectory()) {
            return true;
        } else {
            return source.match(/.*css/) != null;
        }
    }}, function (err) {
        if (err) {
            return console.error(err);
        }
    });